Android: mediaplayer went away with unhandled events

旧城冷巷雨未停 提交于 2019-12-31 08:32:27

问题


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


回答1:


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




回答2:


The holy five:

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


来源:https://stackoverflow.com/questions/9609479/android-mediaplayer-went-away-with-unhandled-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!