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