安卓:媒体播放器走了与未处理事件 [英] Android: mediaplayer went away with unhandled events
问题描述
我需要一个音频文件的持续时间为一系列需要从一个应用程序来播放语音通知的。我已经添加了音频文件作为资源和他们做游戏就好了。实际上下面的示例code可以完美达到预期的目的:它并返回音频文件的持续时间。
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.
下面是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;
}
下面是奇怪的事情。这code被称为在prepares了一套给定的测试音频指令的主要活动。有此活动中没有错误。但是当第二个活动是所谓的,我得到的错误一长串的logcat中。
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>
我已经单步高达主活性(没有错误)的端部,并从第二活动的第一行。这些错误肯定是在活动之间抛出。
另外,如果我注释掉八线try块的(因此只回零)避免了logcat的错误。当我恢复了八行中的错误回来。
我已经通过了文档挖出并在网上搜索,我相信我是正确的构建,发布和销毁MediaPlayer对象,所以我不明白为什么我得到一个错误。
这就是说,我必须做一些错误的。任何想法?
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?
谢谢
凯文
推荐答案
只要把 mp.reset();
在 mp.release() ;。
这篇关于安卓:媒体播放器走了与未处理事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!