Access playlist music Android

0

I'm developing an APP for Android , which I need to access the playlist of songs, in this case it would load a home screen with the playlists it owns and a category with all songs, and clicking in the desired playlist displays the songs from that selected playlist, and the APP itself must be allowed to play music on itself.

Does anyone have any idea how to do it, an example code or an exemplary mini project? My question is how to access the songs that already exist on my phone and how to access the playlists already created in the phone itself. An example App that does this is the Nike. And how do you perform the music?

    
asked by anonymous 04.06.2014 / 14:40

1 answer

3

I believe what you're looking for is exactly the MediaStorage Android API.

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 songs on the device, check out the MediaStorage.Audio and to get playlists recorded on the device at a glance at MediaStorage.Audio.PlayLists .

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

You'll have to use a ContentResolver to make a Query about songs 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 is the code for playing a song using MediaPlayer from Cursor , from this SO question:

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 a 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...
}
    
04.06.2014 / 15:33