Android Media Player Threading

2020-06-20 10:57发布

问题:

I have written a Music player app and it works great but when a flip action happens or when I return the the player view I have to stop the player and restart it at the postion that it was at when the action happened. That all works but it means a breif stop and start.

How can I run the media player in a different thread and still update my seekbar?

Thanks

回答1:

Try running the media player within a foreground service. It's a little bit of work, but it's how it should be done anyways.

The service will handle running everything in a background thread and can be set up to post updates to your UI through messages or callbacks.

In addition, a Foreground service does not need to be tied to an activity, so it can continue to run even if you leave your player screen. The service must provide an Ongoing notification to the user that will probably display the current song, artist, click to pause, etc.