Access playlist music Android

Asked

Viewed 1,558 times

0

I’m developing an APP for Android, that I need to access the playlist of songs, in case it would be as follows would load a home screen with the playlists it has and a category with all songs, and clicking on the desired playlist displays the songs from that selected playlist, and the APP itself has to be allowed to perform songs on itself.

Does anyone have any idea how to do, an example code or an exemplary mini project? My doubt is how to access the songs that already exist on my phone and how to access the playlists already created on the phone. An example of App that does this is Nike. And how to Perform the Songs?

  • Leonardo, from a look at these sites, they have some examples of how to create a player: [1] http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/ [2] http://alucard1990.hubpages.com/hub/How-to-Make-a-Simple-Media-Player-for-Android

  • I think his doubt is to access the playlists of the Play Music app from google, is that it? Or just create a playlist?

  • @Wakim my question is how to access the songs that already exist on my phone and how to access the playlists already created on the phone. An example of App that does this is from Nike.

  • @Marabita need to access the songs that already exist on mobile and not those that are added to the App.

  • @Leonardopatricio You can get the songs from the sdcard, but the songs from the 'play music' I find more complicated because they are probably protected by DRM.

  • But it’s possible somehow because the Nike app or the 4shared app can access them.

Show 1 more comment

1 answer

3


I believe what you are looking for is exactly the API of MediaStorage android.

It has so many ways to access any type of media that is in the device’s internal and external storage.

To get all the music from the apparatus, take a look at the MediaStorage.Audio and to get the playlists recorded on the device a look at MediaStorage.Audio.PlayLists.

I found a complete tutorial to build an app Music Player using this API, in link.

You will have to use a Contentresolver to make a Query about music and playlists. It would have something like:

ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

// Iterar sobre o cursor...

Below the code to play a song using the MediaPlayer from the Cursor, from this question of ONLY:

public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
    int music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);

    musicCursor.moveToPosition(position);

    String filename = musicCursor.getString(music_column_index);

    try {
          if (mMediaPlayer.isPlaying())  {
                mMediaPlayer.reset();
          }

          mMediaPlayer.setDataSource(filename);
          mMediaPlayer.prepare();
          mMediaPlayer.start();

          mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
              @Override
              public void onCompletion(MediaPlayer mp) {
                  mp.release();
                  mp = null;                                   
              }
          });     
    } catch (Exception e) {}
}

In this code, a query is made for all songs on the device.

To check the playlists, I found a snippet, without reliable source that follows the same principle of music query:

public void checkforplaylists(Context context) {
    ContentResolver cr = context.getContentResolver();

    final Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
    final String id = MediaStore.Audio.Playlists._ID;
    final String name = MediaStore.Audio.Playlists.NAME;
    final String[] columns = {id, name};

    final Cursor playlists = cr.query(uri, columns, null, null, null);

    if(playlists == null) {
        Log.e(TAG,"Found no playlists.");
        return;
    }

    Log.e(TAG,"Found playlists.");

    // Iterar sobre o cursor...
}
  • Vlw Fera, really this helped I was able to visualize the songs stored in cel, but the tutorial does not teach how to perform the songs. But you’re being very helpful.

  • Oh yes, performing the songs is another point. You would have to see it better.

  • Could you include that in the question?

  • is because when I click on a song from the list the app is stopping working. In case it is missing a methods called songPicked.

  • Post in question the code you have.

  • @Leonardopatricio, includes another excerpt to play the song.

  • What type of this variable "mMediaPlayer"?

  • It is an instance of the class MediaPlayer, I put a link in the reply.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.