Android设备上的并发声音

时间:2012-01-04 22:11:39

标签: android concurrency audio multi-touch soundpool

我一直在为我的手机开发简单的鼓垫,并且在一次播放两种声音时遇到障碍(即在用户同时按下两个或更多鼓声时的多点触控事件中)

播放我一直在使用SoundPool类的声音。我有一个HashMap,它将一个Point点到声音上。所以我的想法是,当按下一个点时,将播放相应的声音。

我有一种感觉,即一次播放两个声音涉及分叉或创建新线程,但它不能像我尝试的那样工作,这只是为每个点的“for each”循环创建一个新线程按压。

提前致谢! -Aneem

编辑:

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;
}

1 个答案:

答案 0 :(得分:2)

SoundPool能够一次播放多个内容。在设置SoundPool时传入流的数量。

发布所有涉及创建和设置soundpool的代码,我们可以看到为什么它无法正常工作。

SoundPool Constructor

第一个参数是int maxStreams。无论你传递给这个参数的是什么,你的soundpool可以一次播放多少个流