Android: mediaplayer went away with unhandled events

AndroidMedia Player

Android Problem Overview


I need to get the duration of an audio file for a series of voice announcements that need to play from an app. I have added the audio files as resources and they do play just fine. The sample code below actually works perfect for its intended purpose: it does return the duration of the audio files.

Here is the code:

float getDurationOfAudioResource(LocationEnum loc, Context context){
	float  duration = 0;
	try {
		MediaPlayer mp; 
		mp = MediaPlayer.create(context, getAudioResource(loc));
   	    duration = mp.getDuration();
    	mp.release();
    	mp = null;
	}
	catch (IllegalStateException e) {e.printStackTrace(); logError(25, "TestDescItem:Fault::Could not open mediaplayer object with audio resource.");} 
	return duration;
}

Here's the weird thing. This code is called in a Main activity that prepares the set of audio instructions for a given test. There are no errors within this activity. But as soon as the Second activity is called, I get a long string of errors on logcat.

03-07 13:23:43.820: I/ActionLogger(21435): GenTest_Info_Test #0 successfully created.
03-07 13:23:43.830: I/ActionLogger(21435): GenTest_Info_Test #1 successfully created.
03-07 13:23:43.840: I/ActionLogger(21435): GenTest_Info_Test #2 successfully created.
03-07 13:23:43.850: I/ActionLogger(21435): GenTest_Info_Test #3 successfully created.
<snip>
03-07 13:23:43.910: I/ActionLogger(21435): GenTest_Info_all tests successfully created.
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events
03-07 13:23:47.260: W/MediaPlayer(21435): mediaplayer went away with unhandled events
03-07 13:23:47.270: W/MediaPlayer(21435): mediaplayer went away with unhandled events
<snip>

I've single-stepped upto the end of the Main activity (no errors) and from the first line of the Second activity. The errors definitely are thrown between the activities.
Also, if I comment out the eight lines of the try block (thus returning only zero) the logcat errors are avoided. When I restore the eight lines the errors come back. I've dug through the documentation and searched the web, and I believe that I am correctly constructing, releasing and destroying the mediaplayer object, so I can't see why i am getting an error. That said, I must be doing something wrong. Any ideas?

Thanks,

Kevin

Android Solutions


Solution 1 - Android

Just put mp.reset(); before mp.release();.

Solution 2 - Android

The holy five:

    if(mp!=null) {
		if(mp.isPlaying())
			mp.stop();
		mp.reset();
    	mp.release();
    	mp=null;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHephaestusView Question on Stackoverflow
Solution 1 - AndroidAndrewView Answer on Stackoverflow
Solution 2 - AndroidLi3roView Answer on Stackoverflow