0

In my app I have a service that plays audio, that service just has a single static MediaPlayer object. In onStart() from my main activity where I call MediaPlayer.isPlaying(); to determine which UI elements to show, I get an ANR message and a NullPointerException at the line where I call MediaPlayer.isPlaying();

I also call MediaPLayer.isPlaying(); when certain buttons are pressed, and that causes ANR as well, I discovered that after i took away the MediaPlayer.isPlaying(); call in onStart();, since I couldn't get past that without an ANR...

I found this similar question: MediaPlayer isPlaying() always returning false

Where someone says: "Also, while we're on the general MediaPlayer subject, note that isPlaying() can cause an ANR when called on the UI thread. In fact, Google recommends executing all MediaPlayer calls from a background handler thread. Which will also serialize execution of MediaPlayer calls."

I couldn't find anything in documentation about this. So how do I or what is the best way to create a short lived thread in these scenarios so I am able to check the status of my MediaPlayer? Or is there another way?

4

0 回答 0