我有这个警报代码
private void playSound(Context context, Uri alert, Boolean alarm) {
try {
final AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0
&& alarm == true) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
alarmisplaying = true;
} else {
mMediaPlayer.stop();
alarmisplaying = false;
}
} catch (IOException e) {
System.out.println("OOPS");
}
作为上下文,您可以传递 getApplicationContext(),如果您想停止播放,只需传递 false 作为警报(如果您开始播放声音,传递 true)。