5

I am trying to create simple Android application to play audio files from sd card.
I have some problems with understanding Services. I will be concrete, here are my questions.

  1. Service by default runs on UI thread, isn't it ? Only if it runs in separate process it has another thread, as well as context, because it is another process in system (fork of virtual machine instance)
  2. Where should be MediaPlayer class used ? In background service or in UI thread. As far as bound services run on UI thread, there is no sense in creating service bound and so on if it runs on UI thread ? Only in case we want to continue playing music in background while activity (or any UI component was destroyed) but in this case MusicPlayer will occupy whole UI thread even without UI visible to user. Am I right ?
  3. I have found tons of tutorials how to create simple audio player, few of them shows how to run in separate thread directly from service, others just directly in service from activity using IBinder class which just returns instance of Service.

Please explain this topic. I would be grateful for any help. Thanks.

Community
  • 1
  • 1
  • 1
    You should read [that](http://developer.android.com/guide/components/services.html#Foreground) and if you haven't already also [that](http://developer.android.com/guide/topics/media/mediaplayer.html) since those 2 sites basically cover everything about media playback on android – David Medenjak Feb 06 '16 at 17:23
  • 1
    check this tutorial http://code.tutsplus.com/tutorials/create-a-music-player-on-android-user-controls--mobile-22787 – Sreehari Feb 06 '16 at 17:24
  • Thanks for comments but I have already read this. –  Feb 06 '16 at 17:42
  • first, the media playback can't be done on ui thread! It should be done in a Foreground-Service (normal Service, which is started with calling startForeground()). The MediaPlayer should be part of your Service and finally, the Tutorials you saw are ok, I think. I've also some example code if you want some (I'm actually building my own music-app)! – the_dani Jul 04 '16 at 18:57

1 Answers1

1

One of the tricky things behind the Android MediaPlayer is that there are many implementations with slightly different behaviors. Nonetheless, the interface is common, so we can speak about that. All of the MediaPlayer lifecycle methods can be called from the UI thread except for prepare. To make things easier, there is the prepareAsync method, which can be called from the UI thread.

Internally, the MediaPlayer should interact with the audio system in a way that decoding and playback are not occurring on the calling thread in any case.

It is possible to create a MediaPlayer on another thread. However, as noted in the documentation, that thread must have its own Looper (which the UI thread has). So a MediaPlayer should not be created in an async task.

As an aside, I have noted occasional ANRs when calling the start method on the main thread (which brought me to your post). These are quite rare, however.

Tad
  • 4,668
  • 34
  • 35