75

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

Hephaestus
  • 4,337
  • 5
  • 35
  • 48

2 Answers2

230

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

Nathan
  • 11,814
  • 11
  • 50
  • 93
Andrew
  • 2,316
  • 1
  • 14
  • 2
  • 6
    mp.stop(); mp.reset(); mp.release(); mp = null; – Hanry Aug 13 '14 at 10:19
  • 1
    Didn't fix it for me. – Wytze Dec 09 '14 at 14:45
  • 2
    What this does is it resets MediaPlayer to uninitialized state; [documentation](https://developer.android.com/reference/android/media/MediaPlayer.html#reset()). This way you explicitly discard any unhandled event and when destroying object the warning will not show up. I'm not sure though why `release()` doesn't handle this. – c0dehunter May 01 '16 at 12:50
62

The holy five:

    if(mp!=null) {
        if(mp.isPlaying())
            mp.stop();
        mp.reset();
        mp.release();
        mp=null;
    }
Li3ro
  • 1,837
  • 2
  • 27
  • 35