我找到了使用事件的简单解决方案 - 确认答案,因为它解决了您的问题:)
mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
@Override
public void onSeekComplete(MediaPlayer mediaPlayer) {
mediaPlayer.stop();
}
});
http://developer.android.com/reference/android/media/MediaPlayer.html
记住release()。
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
});
您想要使用的完整代码需要将铃声设置为您想要的东西。
AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
int volume = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(view.getContext());
Uri notificationSoundUri = Uri.parse(prefs.getString("notification_sound", "DEFAULT_SOUND"));
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(view.getContext(), notificationSoundUri);
} catch (Exception e1) {
e1.printStackTrace();
mediaPlayer.release();
return;
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
});
try {
mediaPlayer.prepare();
} catch (Exception e1) {
e1.printStackTrace();
mediaPlayer.release();
return;
}
mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
@Override
public void onSeekComplete(MediaPlayer mediaPlayer) {
// TODO Auto-generated method stub
mediaPlayer.stop();
mediaPlayer.start();
}
});
mediaPlayer.setVolume(volume, volume);
mediaPlayer.start();