Come posso accedere alla playlist creata dall'app musicale predefinita di Android e chiamare l'app musicale per riprodurla?
Domanda
Sto scrivendo un'app per Android e desidero accedere alla playlist creata dall'app musicale predefinita di Android.
Nella mia app, l'utente dovrebbe essere in grado di sfogliare la playlist e selezionare qualsiasi playlist da riprodurre.
Quindi, in pratica, voglio sapere come accedervi e quando l'utente seleziona una playlist, come passarla all'app musicale predefinita per riprodurla in background.
Ha qualcosa a che fare con ContentProvider o mediastore ??Non so davvero come accedere ai dati su altre app.
Grazie mille!
Soluzione
Per riprodurre i brani dalle playlist sopra, chiamo la funzione
PlaySongsFromAPlaylist (PlayListID); // 0
dal metodo onCreate sopra.E il codice rimanente è come indicato di seguito.
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();
}
}
Spero che funzioni.:)
Altri suggerimenti
Questa attività combina le due risposte di Ankur Pandya per elencare le playlist sul dispositivo e quindi riprodurre il primo brano dalla prima playlist.
package com.withoutstones.pandyaplaylists;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
/**
* @author Ankur Pandya
* @author Tommy Herbert
*/
public class PandyaPlaylistsActivity extends Activity {
// constants
private static final String LOGGING_TAG = "PandyaPlaylists";
// static methods
/**
* Create a MediaPlayer and play the specified audio file. Note that a full app would usually
* call stop() and release() on the player after use.
*
* @param path
* to data file
*/
public static void playAudio(final String path) {
final MediaPlayer player = new MediaPlayer();
if (path == null) {
Log.e(LOGGING_TAG, "Called playAudio with null data stream.");
return;
}
try {
player.setDataSource(path);
player.prepare();
player.start();
} catch (Exception e) {
Log.e(LOGGING_TAG, "Failed to start MediaPlayer: " + e.getMessage());
return;
}
}
// superclass overrides
@Override
public void onCreate(final Bundle savedInstanceState) {
// Create the superclass portion of the object.
super.onCreate(savedInstanceState);
// Set up the UI.
this.setContentView(R.layout.main);
// Get a cursor over all playlists.
final ContentResolver resolver = this.getContentResolver();
final Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
final String idKey = MediaStore.Audio.Playlists._ID;
final String nameKey = MediaStore.Audio.Playlists.NAME;
final String[] columns = { idKey, nameKey };
final Cursor playLists = resolver.query(uri, columns, null, null, null);
if (playLists == null) {
Log.e(LOGGING_TAG, "Found no playlists.");
return;
}
// Log a list of the playlists.
Log.i(LOGGING_TAG, "Playlists:");
String playListName = null;
for (boolean hasItem = playLists.moveToFirst(); hasItem; hasItem = playLists.moveToNext()) {
playListName = playLists.getString(playLists.getColumnIndex(nameKey));
Log.i(LOGGING_TAG, playListName);
}
// Play the first song from the first playlist.
playLists.moveToFirst();
final long playlistID = playLists.getLong(playLists.getColumnIndex(idKey));
this.playTrackFromPlaylist(playlistID);
// Close the cursor.
if (playLists != null) {
playLists.close();
}
}
// PandyaPlaylistsActivity instance methods
/**
* Play the first track on the specified playlist.
*
* @param playListID
* from the MediaStore database
*/
public void playTrackFromPlaylist(final long playListID) {
final ContentResolver resolver = this.getContentResolver();
final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playListID);
final String dataKey = MediaStore.Audio.Media.DATA;
Cursor tracks = resolver.query(uri, new String[] { dataKey }, null, null, null);
if (tracks != null) {
tracks.moveToFirst();
final int dataIndex = tracks.getColumnIndex(dataKey);
final String dataPath = tracks.getString(dataIndex);
PandyaPlaylistsActivity.playAudio(dataPath);
tracks.close();
}
}
}
Per visualizzare tutte le playlist create dall'utente nell'applicazione musicale predefinita in Android, sto utilizzando il seguente blocco di codice nella mia attività:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] proj = {"*"};
Uri tempPlaylistURI = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
// In the next line 'this' points to current Activity.
// If you want to use the same code in other java file then activity,
// then use an instance of any activity in place of 'this'.
Cursor playListCursor= this.managedQuery(tempPlaylistURI, proj, null,null,null);
if(playListCursor == null){
System.out.println("Not having any Playlist on phone --------------");
return;//don't have list on phone
}
System.gc();
String playListName = null;
System.out.println(">>>>>>> CREATING AND DISPLAYING LIST OF ALL CREATED PLAYLIST <<<<<<");
for(int i = 0; i <playListCursor.getCount() ; i++)
{
playListCursor.moveToPosition(i);
playListName = playListCursor.getString(playListCursor.getColumnIndex("name"));
System.out.println("> " + i + " : " + playListName );
}
if(playListCursor != null)
playListCursor.close();
}
Non dimenticare di includerli prima di utilizzare
import android.net.Uri;
import android.provider.MediaStore;
import android.database.Cursor;
Questo codice è stato testato e funziona correttamente su target= android-8