Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play playlist with MediaPlayer

Tags:

android

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

like image 971
Kaloer Avatar asked Jan 08 '10 20:01

Kaloer


1 Answers

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.

like image 192
jeffpfoster Avatar answered Sep 18 '22 15:09

jeffpfoster