How can I access playlist created by android default music app and call the music app to play it?

后端 未结 3 1712
天涯浪人
天涯浪人 2020-12-25 08:28

I am writing an android app, and I want to access the playlist created by android default music app.

In my app, the user should be able to browse the playlist and se

3条回答
  •  心在旅途
    2020-12-25 09:14

    To play the songs from above playlists, I m calling the function

    PlaySongsFromAPlaylist( PlayListID ); // 0 < PlayListID < count

    from the above onCreate method. And remaining code is as per mentioned below.

    public void PlaySongsFromAPlaylist(int playListID){
    
        String[] ARG_STRING = {MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Video.Media.SIZE,android.provider.MediaStore.MediaColumns.DATA};
        Uri membersUri = MediaStore.Audio.Playlists.Members.getContentUri("external", playListID);
        Cursor songsWithingAPlayList = mThis.managedQuery(membersUri, ARG_STRING, null, null, null);
        int theSongIDIwantToPlay = 0; // PLAYING FROM THE FIRST SONG
        if(songsWithingAPlayList != null)
        {
            songsWithingAPlayList.moveToPosition(theSongIDIwantToPlay);
            String DataStream = songsWithingAPlayList.getString(4);
            PlayMusic(DataStream);
            songsWithingAPlayList.close();
        }   
    }
    
     public static void PlayMusic(String DataStream){
        MediaPlayer mpObject = new MediaPlayer();
        if(DataStream == null)
            return;
        try {
            mpObject.setDataSource(DataStream);
            mpObject.prepare();
            mpObject.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
     }
    

    Hope this will work. :)

提交回复
热议问题