gpt4 book ai didi

Android:媒体播放器因未处理的事件而消失

转载 作者:IT王子 更新时间:2023-10-28 23:39:30 26 4
gpt4 key购买 nike

我需要获取需要从应用播放的一系列语音通知的音频文件的持续时间。我已将音频文件添加为资源,它们播放得很好。下面的示例代码实际上非常适合其预期目的:它确实返回了音频文件的持续时间。

代码如下:

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;
}

这是奇怪的事情。此代码在为给定测试准备音频指令集的 Main Activity 中调用。此 Activity 中没有错误。但是一旦调用了第二个 Activity ,我就会在 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>

我已经单步执行到 Main Activity 的末尾(没有错误)和 Second Activity 的第一行。 Activity 之间肯定会抛出错误。
此外,如果我注释掉 try block 的八行(因此只返回零),则可以避免 logcat 错误。当我恢复八行时,错误又回来了。我浏览了文档并搜索了网络,我相信我正确地构造、释放和销毁了 mediaplayer 对象,所以我不明白为什么会出错。也就是说,我一定做错了什么。有什么想法吗?

谢谢,

凯文

最佳答案

只需将 mp.reset(); 放在 mp.release(); 之前。

关于Android:媒体播放器因未处理的事件而消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9609479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com