我想在警报,媒体和铃声音量较小时播放歌曲。我已经尝试了很多代码,但是当我的闹钟铃声处于静音模式时,我们就不会响铃。如果我的媒体和铃声音量为静音,则说明该音量正常,如果我的闹钟音量为静音,则说明该音量无效。我已经尝试了下面的链接,但它不起作用。请检查并调查。
Play a ringtone when the phone is silent and keep the phone silent?
这是我的代码:
int audioFile = R.raw.ring;
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(this,
Uri.parse("android.resource://com.blh.pickupfresh.resturentapp/" + audioFile));
final AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
mMediaPlayer.setAudioStreamType(audioManager.STREAM_ALARM);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
mMediaPlayer.seekTo(0);
mMediaPlayer.start();
}
});
mMediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}