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
android media player
Hephaestus Mar 07 2018-12-12T00: 00Z
source share