播放声音和停止声音的最佳方式是什么?
我尝试使用 RingtoneManager、MediaPlayer 和 SoundPool,但无法停止声音。
使用 RingtoneManager TYPE_ALARM 时有没有办法停止声音?
请一个简单的片段。
这是我尝试的最后一件事:
SoundPool pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
List<Integer> streams = new ArrayList<Integer>();
int soundId = pool.load(getApplicationContext(), R.drawable.alarm, 1); //There are several versions of this, pick which fits your sound
try{
if(myWifiInfo.getRssi() < -55)
{
/*
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();*/
Thread.sleep(1000);
int streamId = -1;
streamId = pool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f);
streams.add(streamId);
textRssi.setText(String.valueOf(myWifiInfo.getRssi() + " WARNING"));
}
else {
Log.e("WIFI","Usao");
for (Integer stream : streams) {
pool.stop(stream);
}
streams.clear();
Log.e("WIFI","Izasao");
}