这是我用来创建通知声音的代码。但是,首次调用该功能时,媒体播放器将以较低的音量播放声音。如果立即再次调用此函数,则它将以所需的音量(即流音量)播放。
Uri notification = RingtoneManager.getActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_NOTIFICATION);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(context, notification);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mMediaPlayer.setOnPreparedListener(new
MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.setVolume(1, 1);
if(!mMediaPlayer.isPlaying())
mMediaPlayer.start();
}
});
mMediaPlayer.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mMediaPlayer.release();
}
});
mMediaPlayer.prepareAsync();
如何始终以合适的音量播放声音?