Where to create a wrt music player MediaBrowserServiceCompat

I want to create a MusicPlayer application that uses the functions of MediaBrowserService . Looking through the documentation of MediaBrowserServiceCompat , I realized that this is a subclass of Service , which means that it starts in the main thread of the user interface of the application.

But since the music player is a long-term task, I believe that it is best to implement it as an IntentService , and not as a Service.

So I wanted to ask:

  • Where should I implement my MusicPlayer service?

  • Should I implement it as part of the MediaBrowserServiceCompat implementation? But won't it be too heavy for the user interface thread?

  • Or should I implement it as an IntentService and call it from my MediaBrowserServiceCompat? But that seems a bit complicated.

Here is my initial structure

Please offer.

thanks

+5
source share
3 answers

The music player that I wrote uses the background service from which I play songs.

The problem with this approach is that on devices such as Xiaomi or Huawei, these services are killed in order to β€œsave” battery life. I think the best you can do is when your service is killed, you can restart it and start the song again ...

This is the solution that I came across, if someone has a better idea, I would love to hear it.

+1
source

You must use the Service as a command manager for your music player. All playback, pause, next and other playback controls should be sent to the service, and the service will delegate it accordingly. In addition, Android MediaPlayer has asynchronous methods and associated listeners for notifying subscribers. Please take a look. Please follow the sample Google code for the music player https://github.com/googlesamples/android-UniversalMusicPlayer , which describes the functionality of the basic music player very well.

+1
source

Wrong:

I realized that this is a subclass of Service, which means that it works Main UI thread.

Doc clearly says:

A Service is an application component that can perform long-term operation of an operation in the background.

And he also said that if you need some long-term task, you should implement it as a Foreground Service .

 Intent notificationIntent = new Intent(this, ExampleActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new Notification.Builder(this, CHANNEL_DEFAULT_IMPORTANCE) .setContentTitle(getText(R.string.notification_title)) .setContentText(getText(R.string.notification_message)) .setSmallIcon(R.drawable.icon) .setContentIntent(pendingIntent) .setTicker(getText(R.string.ticker_text)) .build(); startForeground(ONGOING_NOTIFICATION_ID, notification); 
0
source

Source: https://habr.com/ru/post/1272442/


All Articles