Search code examples
androidmedia-player

Android MediaPlayer stuck in prepare()


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.

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.

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.


Solution

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