Hephaestus
Hephaestus

Reputation: 5093

Android: mediaplayer went away with unhandled events

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

Upvotes: 76

Views: 41797

Answers (2)

Li3ro
Li3ro

Reputation: 1877

The holy five:

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

Upvotes: 65

Andrew
Andrew

Reputation: 2376

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

Upvotes: 236

Related Questions