This line from Logcat is important:
Caused by: java.lang.InstantiationException: can't instantiate class com.floritfoto.apps.ave.Music; no empty constructor
For your service, you need another constructor that takes no arguments:
public Music() { super("Music"); }
EDIT
Using the service is the right approach if you want the music to play when the screen is off. However, the phone will try to sleep when the screen is off, and this may interrupt your MediaPlayer .
The most reliable solution is to use partial WakeLock to prevent a sleeping device from playing music. Be sure to release WakeLock correctly if you are not actively playing music; otherwise the battery will run out.
You can also use startForeground() , which will reduce the risk that your service will be killed if there is pressure in memory. It will also create a nice user interface, showing a constant notification when your service is launched.
Creating an instance of the Music class using Music track = Music(fileDescriptor); probably will do some harm. A better approach is to pass the file descriptor as Extra to the Intent that you pass to startService() :
Intent serviceIntent = new Intent(this, Music.class); serviceIntent.putExtra("ServiceFileDescriptor", fileDescriptor); startService(serviceIntent);
Then extract the file descriptor from the same Intent when it passed your onStartCommand() :
public int onStartCommand(Intent intent, int flags, int startId) { super.onStart(); Bundle bundle = intent.getExtras();
A few things to note here. I moved the code from the original constructor (which should be removed) to onStartCommand() . You can also remove the onStart() method, since it will only be called on devices up to 2.0. If you want to support modern versions of Android, you will need to use onStartCommand() . Finally, the return value START_STICKY ensures that the service will continue to function until you call stopService() from your activity.
EDIT 2 :
Using the service allows your users to navigate between activities without interrupting MediaPlayer . You do not have much control over how long the Activity will remain in memory, but the active Service (especially if you call startForeground() ) will not be killed unless there is very strong pressure in the memory.
To interact with MediaPlayer after starting the service, you have several options. You can pass additional commands to the service by creating an Intent and using the action line (and / or some additional functions) to tell the service what you would like to do. Just call startActivity() again with the new Intent , and onStartCommand() will be called in the service, after which you can control MediaPlayer . The second option is to use a linked service (example here ) and bind / untie each time you enter / leave an action that should bind to the service. Using a linked service โfeelsโ as if you are directly manipulating the service, but it is also more complex since you need to manage the binding and disconnection.