MediaPlayer在Android 4.4上随机停止(19) [英] MediaPlayer randomly stops on Android 4.4 (19)

查看:88
本文介绍了MediaPlayer在Android 4.4上随机停止(19)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用在Nexus 5以外的所有设备上都可以正常播放音频.在Nexus 5上,MediaPlayer随机停止播放.不知道有关响度的更改( http://developer.android .com/about/versions/android-4.4.html#Multimedia )在4.4中已损坏.

My app is streaming audio fine on all devices except Nexus 5. On Nexus 5, the MediaPlayer randomly stops playing. Not sure if the changes with respect to Loudness (http://developer.android.com/about/versions/android-4.4.html#Multimedia) in 4.4 has broken something.

还有其他人注意到这个问题吗?似乎某些用户正在发生,但我无法在自己的Nexus 5上进行复制.

Is anyone else noticing this issue? Seems to be happening to some users, but I'm unable to reproduce on my own Nexus 5.

更新:因此,我能够在Nexus 5上重现该问题.这似乎实际上是在剪辑结尾处发生的.剪辑中还剩下1-5秒左右的时间,MediaPlayer会调用OnCompletionListener.onCompletion()方法.这仅发生在Nexus 5上,并且随机发生在某些片段上.我几乎可以在30%的时间内重现它.请注意,当剪辑提早结束时,如果我尝试返回并再次播放该剪辑,则它将第二次完全播放完该剪辑.我知道Android 4.4刚刚发布,但希望有人可以提供帮助!谢谢.

UPDATE: So I was able to reproduce the issue on my Nexus 5. It seems to actually be happening near the end of the clip. With about 1 - 5 seconds left in the clip, the OnCompletionListener.onCompletion() method is called by MediaPlayer. This is only happening on the Nexus 5 and it's happening on some clips at random. I'm able to reproduce it almost 30% of the time. Note that, when the clip finishes early, if I try to go back and play the clip again it finishes playing the clip entirely the second time. I know Android 4.4 just got released, but hopefully someone out there can help! Thanks.

更新:我已经针对Android提交了一个错误: https://code.google.com/p/android/issues/detail?id=62304

UPDATE: I've filed a bug against Android: https://code.google.com/p/android/issues/detail?id=62304

推荐答案

好的,我已经找到了解决方案.我不确定这是否是您现在都面临的问题,但是它可以解决我的问题.基本上,Android 4.4+引入了许多新的电源管理功能,其中之一包括在屏幕关闭时关闭CPU.来自Android文档的报价:

Alright, I've found the solution. I'm not sure if this is the issue you're all facing now, but it fixes mine. Basically, Android 4.4+ introduces many new power management features and one of them includes shutting the CPU down while the screen is off. Quote from Android docs:

因为Android系统尝试在设备时节省电量 处于睡眠状态,系统尝试关闭手机的任何功能 不需要,包括CPU和WiFi硬件. 但是,如果您的服务正在播放或流式播放音乐,则需要 防止系统干扰您的播放.

Because the Android system tries to conserve battery while the device is sleeping, the system tries to shut off any of the phone's features that are not necessary, including the CPU and the WiFi hardware. However, if your service is playing or streaming music, you want to prevent the system from interfering with your playback.

因此,在没有CPU唤醒锁定的情况下,MediaPlayer失去了正确播放流的能力,从而导致其在剪辑完成之前停止播放.解决方案很简单:将PARTIAL_WAKE_LOCK添加到MediaPlayer. 如Android所述:

Hence, without a CPU wake lock the MediaPlayer loses its ability to stream properly, causing it to stop playback before the clip is complete. The solution for this is simple: add a PARTIAL_WAKE_LOCK to the MediaPlayer. As documented on Android:

mMediaPlayer = new MediaPlayer();
// ... other initialization here ...
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

我想我们当中的很多人都无法在文档中看到这一点.我不记得看到过这个,所以也许它只是被添加了.无论如何,希望这可以解决每个人的问题!

I guess a bunch of us failed to see this in the docs. I don't remember seeing this, so maybe it was just added. Anyway, hopefully this fixes the issue for everyone!

这篇关于MediaPlayer在Android 4.4上随机停止(19)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆