Concurrent Sound on Android Device

ε祈祈猫儿з 提交于 2019-12-24 08:39:23

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/8734827/concurrent-sound-on-android-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!