Why does the sound only play in debug mode?
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.start();
mp.reset();
mp.release();
Why does the sound only play in debug mode?
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), notification);
mp.start();
mp.reset();
mp.release();
The "playing" of a sound by the MediaPlayer is initiated by calling the mp.start()
method. However the sound is "played" asynchronously, that is, after calling, the mp.start()
method returns immediately.
So the next instruction ( mp.reset()
) is immediately executed, causing the sound to be finished.
The reason you can hear the sound in debug mode, I suppose in step by step , is because the program execution is slower, time between running mp.start()
and mp.reset()
.
To see how to use MediaPlayer, see How play a sound using Mediaplayer .
UltraSeven,
You are making some mistakes. I'll start the explanation with an image I removed from the MediaPlayer class documentation
Notice what the reset method and method release()
do, or what state they take MediaPlayer
. After executing the reset()
method, the MediaPlayer
goes to the idle state or idle state, and after executing the release()
method, goes to the end state or finalized state / closed.
What happens is that after running the audio with the start()
method, then you execute the reset()
method, leaving MediaPlayer
idle, and method release()
, ending MediaPlayer
. That is, the audio starts to run and is soon paused for being idle and finished.
I think the confusion you are making is because you believe that the reset()
and release()
method will only run when the audio finishes playing start()
, ie the start()
is stuck until the audio ends. But this is not how it works, start()
is asynchronous, that is, its execution will happen in parallel to the other lines below.
Hope it was clear, anything leave a comment.