1

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

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

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

提前致谢!-阿尼姆

编辑:

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

}

公共布尔 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;
}
4

1 回答 1

2

SoundPool 能够同时播放多个东西。您在设置 SoundPool 时传入流的数量。

发布您所有涉及创建和设置您的声音池的代码,我们可以看到为什么它不能按您的意愿工作。

SoundPool 构造函数

第一个参数是 int maxStreams。无论您为此参数传递的内容都是您的声音池一次可以播放多少个流

于 2012-01-04T22:15:06.553 回答