Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to create music player service w.r.t. MediaBrowserServiceCompat

I want to create a MusicPlayer application which leverages the features of MediaBrowserService. While going thru documentation of MediaBrowserServiceCompat, I realized it is a subclass of Service, which means it runs on Application's main UI thread.

But since music player is a long running task, I suppose its best to implement it as an IntentService, rather than as Service.

So I wanted to ask:

  1. Where should I implement my MusicPlayer service?

  2. Should I implement it within MediaBrowserServiceCompat implementation? But will it not make too heavy on UI thread?

  3. Or should I implement it as an IntentService & call it from my MediaBrowserServiceCompat? But it seems bit complex.

Here is my initial code structure

Please suggest.

Thank You

like image 757
reiley Avatar asked Oct 08 '17 20:10

reiley


2 Answers

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

The problem with this approach is that on devices like Xiaomi or Huawei those services are killed in order to "save" battery life. I guess the best you can do is when your service gets killed you could restart it and start the song again ...

That's the solution I came up with, if anyone has a better idea I would love to hear it.

like image 190
wexeteme Avatar answered Oct 28 '22 18:10

wexeteme


You should use Service as a command manager for your music player app. All the play, pause, next and other playback controls should be sent to service, and service will delegate it accordingly. Also Android MediaPlayer has asynchronous methods and associated listeners to notify callers. Please do have a look at it. Please follow google sample code for music player https://github.com/googlesamples/android-UniversalMusicPlayer which addresses basic music player functionalities very well.

like image 45
abhishesh Avatar answered Oct 28 '22 19:10

abhishesh