在Android设备并发出声 [英] Concurrent Sound on Android Device

查看:131
本文介绍了在Android设备并发出声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发简单的鼓垫我的电话,当它涉及到一个多点触摸事件期间一次打两个声音(即当用​​户presses倒在两个或两个以上的鼓声在已运行到块同一时间)。

有关播放的声音我一直使用的Soundpool类。我有一个散列指向一个声音一个HashMap。所以我的想法是,当一个点pssed $ P $,其对应的声音播放。

我有一种感觉,让两个声音播放一次涉及分叉或创建新的线程,但它没有工作,我想它的方式,它只是在创建一个新的线程为每一个循环每个点pressed。

在此先感谢!
-Aneem

编辑:

 的Soundpool数据库=新的Soundpool(6 AudioManager.STREAM_MUSIC,1​​00);保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);    宽度= MeasureSpec.getSize(widthMeasureSpec); //切换方便
    身高= MeasureSpec.getSize(heightMeasureSpec);
    screenFactor =(INT)((高*宽)/ 6);    //必须所有点添加到哈希地图
    widthIncrement =(int)的(宽度/ 3);
    heightIncrement =(int)的(高度/ 2);    pointMap.put(新点(0,0),database.load(的getContext(),R.raw.clv,1));
    pointMap.put(新点(widthIncrement,0),database.load(的getContext(),R.raw.pedalhh,1));
    pointMap.put(新点((widthIncrement * 2),0),database.load(的getContext(),R.raw.shake,1));
    pointMap.put(新点(0,heightIncrement),database.load(的getContext(),R.raw.analoghat,1));
    pointMap.put(新点(widthIncrement,heightIncrement),database.load(的getContext(),R.raw.kick,1));
    pointMap.put(新点((widthIncrement * 2),heightIncrement),database.load(的getContext(),R.raw.snare,1));}

公共布​​尔onTouchEvent(MotionEvent EV){

 如果(ev.getAction()== ev.ACTION_DOWN){
        AudioManager经理=(AudioManager)的getContext()getSystemService(Context.AUDIO_SERVICE)。
        INT streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        的PointF [] =接触点新的PointF [ev.getPointerCount()];
        的for(int i = 0; I< ev.getPointerCount();我++){
            如果(ev.getPointerId(ⅰ)== ev.ACTION_DOWN){
                接触点由[i] =新的PointF(ev.getX(ⅰ),ev.getY(ⅰ));
            }
        }
        INT I = 1;
        对于(最终的PointF点:接触点){
            新主题(新的Runnable接口(){
                公共无效的run(){
                    forkSound(点);
                }
            })。开始();
        }
        返回true;
    }
    返回true;
}


解决方案

的Soundpool能够同时播放多个事情。你传入数据流的数量,当您设置的Soundpool。

发表您的所有code,与创建和设置您的Soundpool,我们可以看到,为什么它不工作你想怎么交易的。

<一个href=\"http://developer.android.com/reference/android/media/SoundPool.html#SoundPool%28int,%20int,%20int%29\"相对=nofollow>的Soundpool构造

第一个参数是INT maxStreams。不管你为此参数传递将是你的Soundpool多少可以起到流一次

I have been developing simple drum pads for my phone and have run into a block when it comes to playing two sounds at once (ie during a multitouch event when the user presses down on two or more drum sounds at the same time).

For playing the sounds I have been using the SoundPool class. I have a HashMap that hashes a Point to a sound. So my thinking was that when a point is pressed, its corresponding sound will be played.

I have a feeling that getting two sounds to play at once involves forking or creating new threads, but it did not work the way I tried it, which is just creating a new thread in a "for each" loop for each point pressed.

Thanks in advance! -Aneem

EDIT:

SoundPool database = new SoundPool(6,AudioManager.STREAM_MUSIC,100);



protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    width = MeasureSpec.getSize(widthMeasureSpec); //switched for convenience
    height = MeasureSpec.getSize(heightMeasureSpec);
    screenFactor = (int)((height*width)/6);

    //have to add all points to the hash map
    widthIncrement = (int)(width/3);
    heightIncrement = (int)(height/2);

    pointMap.put(new Point(0,0), database.load(getContext(),R.raw.clv,1));
    pointMap.put(new Point(widthIncrement,0), database.load(getContext(),R.raw.pedalhh,1));
    pointMap.put(new Point((widthIncrement*2),0), database.load(getContext(),R.raw.shake,1));
    pointMap.put(new Point(0,heightIncrement), database.load(getContext(),R.raw.analoghat,1));
    pointMap.put(new Point(widthIncrement,heightIncrement), database.load(getContext(),R.raw.kick,1));
    pointMap.put(new Point((widthIncrement*2),heightIncrement), database.load(getContext(),R.raw.snare,1));

}

public boolean onTouchEvent(MotionEvent ev) {

    if(ev.getAction()==ev.ACTION_DOWN){
        AudioManager mgr = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
        int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        PointF[] touchPoints = new PointF[ev.getPointerCount()];
        for(int i = 0; i < ev.getPointerCount(); i++){
            if(ev.getPointerId(i)==ev.ACTION_DOWN){
                touchPoints[i] = new PointF(ev.getX(i),ev.getY(i));
            }
        }
        int i = 1;
        for(final PointF point : touchPoints){
            new Thread(new Runnable(){
                public void run(){                      
                    forkSound(point);       
                }
            }).start();
        }
        return true;
    }
    return true;
}

解决方案

SoundPool is capable of playing multiple things at once. You pass in the number of streams when you set up the SoundPool.

Post all of your code that deals with creating and setting up your soundpool and we can see why it is not working how you want.

SoundPool Constructor

The first parameter is int maxStreams. Whatever you pass for this parameter will be how many streams your soundpool can play at once

这篇关于在Android设备并发出声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆