karthiks karthiks - 1 year ago 108
Android Question

Understanding Android Service by example

I have an app that plays an song displaying its lyrics on the screen.

Two situations:

  1. When the song is loaded from a local mp3 file.

  2. When the song is loaded from remote location via internet

My understanding is that Android Service comes to rescue when the song is streamed from remote location via internet. Android Service helps here because of the i/o overheads involved. In the first case, where the song is played form local location, services is perhaps an overkill. I can simply play the music loading it in the activity and use MediaPlayer API to play it.

Is this understanding correct? Am I missing anything?

Update: The song size is over 10MB. So when you stream it via a slow internet over phone there might be some buffering and stuff to be accommodated for better user experience.

Thanks in advance.

Answer Source

There are different Services for different use cases. For a Media Player it depends if you want to have the playback continuously playing in the back- or foreground.

So both cases are applicable for a Media Player Service. But the Android Developer Guide Media Player Service outshines my answer

Using a Service with MediaPlayer
If you want your media to play in the background even when your application is not onscreen—that is, you want it to continue playing while the user is interacting with other applications—then you must start a Service and control the MediaPlayer instance from there. You should be careful about this setup, because the user and the system have expectations about how an application running a background service should interact with the rest of the system. If your application does not fulfil those expectations, the user may have a bad experience. This section describes the main issues that you should be aware of and offers suggestions about how to approach them

Your second case is more a question about networking and in this case you could use a IntentService to download the .mp3 and pass it to the MediaPlayer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download