Kaloer
Kaloer

Reputation: 3773

Play playlist with MediaPlayer

I'm trying to play a playlist I get using the MediaStore provider. However, when I try playing a playlist nothing happens. Can a MediaPlayer play a playlist (m3u file) and do I need to set the first track to play ?

This is my test code in the onCreate() method:

        Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
    if(uri == null) {
        Log.e("Uri = null");
    }
    String[] projection = new String[] { MediaStore.Audio.Playlists._ID, MediaStore.Audio.Playlists.NAME, MediaStore.Audio.Playlists.DATA };
    Cursor c = managedQuery(uri, projection, null, null, null);
    if(c == null) {
        Toast.makeText(getApplicationContext(), R.string.alarm_tone_picker_error, Toast.LENGTH_LONG).show();
        return;
    }
    if(!c.moveToFirst()) {
        c.close();
        Toast.makeText(getApplicationContext(), R.string.alarm_tone_picker_no_music, Toast.LENGTH_LONG).show();
        return;
    }
    c.moveToFirst();
    try {
        MediaPlayer player = new MediaPlayer();
        player.setDataSource(c.getString(2));
        player.start();
    } catch(Exception e) {
        e.printStackTrace();
    }

I have turned on every volume stream.

Thanks you,

Kaloer

Upvotes: 12

Views: 11801

Answers (1)

jeffpfoster
jeffpfoster

Reputation: 66

I don't think media player can play a playlist. I think it will only play a track. You will have to keep track of the tracks from the playlist and pass them to the media player. It might help you to check out the android source code for the music player and how it handles it.

Upvotes: 5

Related Questions