How do Android mediaplayers continuing playing songs when app is closed?

The media player only plays one audio track. What media players do, is listen on the onCompletion event and play the next track.

The MediaPlayer is bound to the process, not the activity, so it keeps playing as long as the process runs. The activity might be paused or destroyed, but that won't affect the internal thread that MediaPlayer uses.

I'm building an audio player to learn Android, you can see the service that plays audio files here

edit

regarding the first comment: The service keeps running on the background and keeps running after you "exit" the application, because the lifecycle of the service and Activities are different.

In order to play the next track, the service registers a callback on the MediaPlayer so the service is informed when an audio stream completed. When the audio completes, the service cleans up the resources used by the MediaPlayer, by calling MediaPlayer.release(), and then creates a fresh new media player with the next audio track to play and registers itself to be notified again when that audio track completes, ad infinitum :).

The MediaPlayer class doesn't understand playlists, so the service is responsible for playing a track after the previous track completes.

In the AudioPlayer service I've created, an activity queues tracks in the AudioPlayer and the AudioPlayer is responsible for playing them in order.

I hope it's clear and again, if you have some time, please check the code of AudioPlayer service I've put above. It's not pure beauty, but it does its job.


You can create a Service to keep the MediaPlayer playing after your app either exits or is paused. To get the MediaPlayer to play consecutive tracks you can register an onCompletionListener that will decide which track to play next. Here is a simple example service that does this:

package edu.gvsu.cis.muzak;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;

public class MuzakService extends Service {

    private static final String DEBUG_TAG = "MuzakService";
    private MediaPlayer mp;
    private String[] tracks = {
            "http://freedownloads.last.fm/download/288181172/Nocturne.mp3",
            "http://freedownloads.last.fm/download/367924875/Behemoths%2BSternentanz.mp3",
            "http://freedownloads.last.fm/download/185193341/Snowflake%2BImpromptu.mp3",
            "http://freedownloads.last.fm/download/305596593/Prel%25C3%25BAdio.mp3",
            "http://freedownloads.last.fm/download/142005075/Piano%2BSonata%2B22%2B-%2Bmovement%2B2%2B%2528Beethoven%2529.mp3",
            "http://freedownloads.last.fm/download/106179902/Piano%2BSonata%2B%25231%2B-%2Bmovement%2B%25234%2B%2528Brahms%2529.mp3",

    };
    private int currentTrack = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(DEBUG_TAG, "In onCreate.");

        try {
            Uri file = Uri.parse(tracks[this.currentTrack]);
            mp = new MediaPlayer();
            mp.setDataSource(this, file);
            mp.prepare();
            mp.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    currentTrack = (currentTrack + 1) % tracks.length;
                    Uri nextTrack = Uri.parse(tracks[currentTrack]);
                    try {
                        mp.setDataSource(MuzakService.this,nextTrack);
                        mp.prepare();
                        mp.start();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 

                }

            });

        } catch (Exception e) { 
            Log.e(DEBUG_TAG, "Player failed", e);
        }
    }


    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.d(DEBUG_TAG, "In onDestroy.");
        if(mp != null) {
            mp.stop();
        }
    }

    @Override
    public int onStartCommand(Intent intent,int flags, int startId) {
        super.onStart(intent, startId);
        Log.d(DEBUG_TAG, "In onStart.");
        mp.start();
        return Service.START_STICKY_COMPATIBILITY;
    }


    @Override
    public IBinder onBind(Intent intent) {
        Log.d(DEBUG_TAG, "In onBind with intent=" + intent.getAction());
        return null;
    }

}

You can start this Service up in an Activity as follows:

Intent serv = new Intent(this,MuzakService.class);
startService(serv);

and stop it like this:

Intent serv = new Intent(this,MuzakService.class);
stopService(serv);