我正在创建一个应用程序,您可以在其中选择1到4种不同的声音,并在同一时间播放它们。一切正常,直到我想停止声音,功能停止只能同时为一个声音,这里是代码(仅适用于soundpool)
sound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
s1 = sound.load(Main.this, R.raw.mosq1, 1);
s2 = sound.load(Main.this, R.raw.mosq2, 1);
s3 = sound.load(Main.this, R.raw.mosq3, 1);
s4 = sound.load(Main.this, R.raw.mosq4, 1);
.
.
.
.
case R.id.play:
if (l1==true){sound.play(s1, 1, 1, 0, rep, 1); if(vib==true) wibracja.vibrate(4500);}
if (l2==true){sound.play(s2, 1, 1, 0, rep, 1); if(vib==true) wibracja.vibrate(6500);}
if (l3==true){sound.play(s3, 1, 1, 0, rep, 1); if(vib==true) wibracja.vibrate(2500);}
if (l4==true){sound.play(s4, 1, 1, 0, rep, 1); if(vib==true) wibracja.vibrate(4500);}
break;
case R.id.stop:
sound.stop(s1);
sound.stop(s2);
sound.stop(s3);
sound.stop(s4);
wibracja.cancel();
break;
如何正确使用停止功能?谢谢你的帮助
答案 0 :(得分:9)
对于那些使用soundpool的人来说,这是一个常见的问题 第一次。如果你仔细查看soundpool的文档,play会使用参数“soundID”,而stop会使用参数“streamID”。很容易忽略这一点,并使用load命令返回的soundID作为代码。我甚至看过android教程示例搞错了。而是使用这些命令:
s1 = sound.load(Main.this, R.raw.mosq1, 1);
if (l1==true){p1=sound.play(s1, 1, 1, 0, rep, 1);
sound.stop(p1);// NOT s1