Java – Android music player in UI thread or background

I'm trying to create a simple Android application to play audio files in SD card

>By default, the service runs on the UI thread, doesn't it? Only when it runs in a separate process can it have another thread and context, because it is another process in the system (a branch of the virtual machine instance) > Where should the mediaplayer class be used? In the background service or UI thread For the binding service running on the UI thread, if it runs on the UI thread, is it meaningless to create a service binding? Only if we want to continue playing music in the background when the activity (or any UI component is damaged), but in this case, musicplayer will occupy the whole UI thread even if the user cannot see the UI Am I right? > I have found a lot of tutorials on how to create a simple audio player. Few of them show how to run directly from the service in a separate thread. Others only directly serve the activities using ibinder class, and ibinder class only returns the instance of service

Please explain the subject I would appreciate any help thank you.

Solution

A tricky problem behind Android mediaplayer is that many implementations have slightly different behavior Nevertheless, interfaces are common, so we can talk about this Except prepare, all mediaplayer lifecycle methods can be called from the UI thread To simplify the operation, there is a prepareasync method that can be called from the UI thread

Internally, mediaplayer should interact with the audio system in a way that decoding and playback do not occur on the calling thread under any circumstances

Mediaplayer can be created on another thread However, as described in the documentation, the thread must have its own looper Therefore, mediaplayer should not be created in asynchronous tasks

By the way, I noticed the occasional anr when the main thread called the start method (which will bring me to your post) However, these are very rare

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>