Update RandomMusicPlayer sample for new RemoteControlClient APIs, also add media button support
Change-Id: Ia6ad08dd0ec1e67f1cffa2d6cfca2120ee0a96c7
This commit is contained in:
@@ -16,16 +16,17 @@
|
||||
|
||||
package com.example.android.musicplayer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.ContentUris;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Retrieves and organizes media to play. Before being used, you must call {@link #prepare()},
|
||||
* which will retrieve all of the music on the user's device (by performing a query on a content
|
||||
@@ -57,7 +58,8 @@ public class MusicRetriever {
|
||||
|
||||
// Perform a query on the content resolver. The URI we're passing specifies that we
|
||||
// want to query for all audio media on external storage (e.g. SD card)
|
||||
Cursor cur = mContentResolver.query(uri, null, null, null, null);
|
||||
Cursor cur = mContentResolver.query(uri, null,
|
||||
MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null);
|
||||
Log.i(TAG, "Query finished. " + (cur == null ? "Returned NULL." : "Returned a cursor."));
|
||||
|
||||
if (cur == null) {
|
||||
@@ -73,9 +75,12 @@ public class MusicRetriever {
|
||||
|
||||
Log.i(TAG, "Listing...");
|
||||
|
||||
// retrieve the indices of the columns where the ID and title of the song are
|
||||
int titleColumn = cur.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
|
||||
int idColumn = cur.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
|
||||
// retrieve the indices of the columns where the ID, title, etc. of the song are
|
||||
int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
|
||||
int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
|
||||
int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM);
|
||||
int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION);
|
||||
int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID);
|
||||
|
||||
Log.i(TAG, "Title column index: " + String.valueOf(titleColumn));
|
||||
Log.i(TAG, "ID column index: " + String.valueOf(titleColumn));
|
||||
@@ -83,7 +88,12 @@ public class MusicRetriever {
|
||||
// add each song to mItems
|
||||
do {
|
||||
Log.i(TAG, "ID: " + cur.getString(idColumn) + " Title: " + cur.getString(titleColumn));
|
||||
mItems.add(new Item(cur.getLong(idColumn), cur.getString(titleColumn)));
|
||||
mItems.add(new Item(
|
||||
cur.getLong(idColumn),
|
||||
cur.getString(artistColumn),
|
||||
cur.getString(titleColumn),
|
||||
cur.getString(albumColumn),
|
||||
cur.getLong(durationColumn)));
|
||||
} while (cur.moveToNext());
|
||||
|
||||
Log.i(TAG, "Done querying media. MusicRetriever is ready.");
|
||||
@@ -99,17 +109,41 @@ public class MusicRetriever {
|
||||
return mItems.get(mRandom.nextInt(mItems.size()));
|
||||
}
|
||||
|
||||
public class Item {
|
||||
public static class Item {
|
||||
long id;
|
||||
String artist;
|
||||
String title;
|
||||
String album;
|
||||
long duration;
|
||||
|
||||
public Item(long id, String title) {
|
||||
public Item(long id, String artist, String title, String album, long duration) {
|
||||
this.id = id;
|
||||
this.artist = artist;
|
||||
this.title = title;
|
||||
this.album = album;
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getArtist() {
|
||||
return artist;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getAlbum() {
|
||||
return album;
|
||||
}
|
||||
|
||||
public long getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public long getId() { return id; }
|
||||
public String getTitle() { return title; }
|
||||
public Uri getURI() {
|
||||
return ContentUris.withAppendedId(
|
||||
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
|
||||
|
||||
Reference in New Issue
Block a user