Android MediaPlayer卡在prepare()中 [英] Android MediaPlayer stuck in prepare()

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

问题描述

我遇到了一个严重的问题,媒体播放器(MP)被卡在prepare()方法上. 我的应用程序在AsyncTask中运行prepare()以避免阻塞UI,因为源来自网络.用户可以随时单击几个播放"按钮,因此我在同步方法中添加了prepare()以更好地控制MP的状态.我的应用还调用了release() onPause来释放已使用的资源.

I'm facing a serious problem with the Media Player(MP) being stuck at the prepare() method. My app runs prepare() in an AsyncTask to avoid blocking the UI, since the sources are from the web. There are several 'play' buttons that the user can click at any time, so I added the prepare() inside a synchronized method to better control the state of the MP. My app also call a release() onPause to free the used resources.

事实是,我注意到,如果在准备过程中调用了release(),则prepare()永远不会返回,因此我陷入了一个同步方法中.最坏的情况是AsyncTask线程处于死锁状态,并且每次用户在该状态下单击播放时,都会浪费另一个线程,因为它一直在等待获取永不返回prepare().很快,我所有的AsyncTasks线程都被浪费了,由于我大量使用它们,因此我的应用程序停止运行.

Thing is, I noticed that if release() is called while preparing, prepare() never returns, and so I'm stuck inside a synchronized method. The worst of it is that the AsyncTask thread is in a deadlock, and every time that the user click on play in that state another thread is wasted since it keeps waiting to acquire the monitor that is in possesion of the never-returning prepare(). Soon all my AsyncTasks threads are wasted and since I use them extensively, my app stops working.

所以我的问题是:有谁知道如何克服这个问题?我正在认真考虑重新使用MediaPlayer进行所有工作,但是我需要事先知道处理这种情况的最佳方法.

So my question is: do anyone has an idea of how overcoming this problem? I'm seriously thinking of redoing all my work with the MediaPlayer, but I need to know the best way to handle situations like this one beforehand.

推荐答案

您应该改用prepareAsync().而且,仅出于MediaPlayer准备工作就不需要AsyncTask.

You should use prepareAsync() instead. And you won't need AsyncTask just for the sake of MediaPlayer preparation.

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

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