Android: mediaplayer quit with raw events

I need to get the duration of the audio file for a series of voice announcements to be played from the application. I added audio files as resources and they play great. The sample code below really works perfectly for its purpose: it returns 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 is a strange thing. This code is called in the main action, which prepares a set of sound instructions for this test. There are no errors in this action. But as soon as the second action is called, I get a long string of errors in 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 am doing one-step until the end of the main activity (without errors) and from the first line of the second action. Errors are certainly thrown between actions.
Also, if I comment out eight lines of a try block (thus returning only zero), logcat errors can be avoided. When I restore eight lines, errors are returned. I broke through the documentation and searched the Internet, and I believe that I correctly create, release and destroy the media planner object, so I don’t understand why I get the error message. However, I have to do something wrong. Any ideas?

Thank,

Kevin

+42
android media player
Mar 07 2018-12-12T00:
source share
2 answers

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

+152
Apr 02 2018-12-12T00: 00Z
source share

Holy Five:

  if(mp!=null) { if(mp.isPlaying()) mp.stop(); mp.reset(); mp.release(); mp=null; } 
+32
Oct 11 '14 at 16:35
source share



All Articles