How do I allow users to view and play songs on Android that they already have access to (for example, through Google Play)?

I launch roadblock by integrating music into my Android app. The goal is to allow users to see and play songs that they already have access to through Google Play Music (or any other player such as Winamp, Poweramp, doubleTwist, etc.).

So far I have tried several approaches, but each has a problem.

Approach # 1: Use the ContentResolver request to get playlists and songs from music on Google Play, and then use MediaPlayer

First, hover over the playlists:

String[] proj = { MediaStore.Audio.Playlists.NAME, MediaStore.Audio.Playlists._ID }; Uri playlistUri = Uri.parse("content://com.google.android.music.MusicContent/playlists"); Cursor playlistCursor = getContentResolver().query(playlistUri, proj, null, null, null); 

Then try again to get the songs in each playlist, and read them as follows:

 String[] proj2 = { MediaStore.Audio.Playlists.Members.TITLE, MediaStore.Audio.Playlists.Members._ID }; String playListRef = "content://com.google.android.music.MusicContent/playlists/" + playListId + "/members"; Uri songUri = Uri.parse(playListRef); Cursor songCursor = getContentResolver().query(songUri, proj2, null, null, null); 

This works to get playlists and songs, but then you cannot play them (even if you tell Google Play Music "Keep on device"). In particular, the following code does not play this song, as indicated above:

 mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try { mediaPlayer.setDataSource(dataPath); mediaPlayer.prepare(); mediaPlayer.start(); } catch (Exception e) {} 

In fact, I scanned the device and could not find the files anywhere (although I confirmed that they were local, completely disconnecting the network - on both the Galaxy Nexus and Galaxy S3).

Approach # 2: challenge Google Play Music or another player using intent

According to this message, you can simply call the player using intentions, for example:

 Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); File file = new File(YOUR_SONG_URI); intent.setDataAndType(Uri.fromFile(file), "audio/*"); startActivity(intent); 

But I don’t think that this approach allows you to control any type, for example, to transmit a specific song that needs to be played, or to be notified when the song will be played. In addition, @CommonWares indicates that the music application and its intentions are not part of the public Android SDK API and are subject to change without notice.

The main functionality that I am trying to achieve exists in iOS, thanks to tight integration with iTunes. I would think that Google would want to achieve the same integration, as this could mean selling a lot more music in the Play store.

In summary: Can I browse and play songs that they already have access to on Android? What is the best way to do this using Google Play Music or any other application? Thanks.

+6
source share
1 answer

Approach No. 1 (works with local music on the device), thanks OP for the direction.

Notes:

  • We play with an undocumented internal API here - this is not reliable.
  • I don’t have access to online music on Google Play Music, so the answer only applies to locally stored music.

The code below will iterate over all the songs in the playlist and play the last one.

Pay attention to the column "SourceId" - this is the identifier of the audio that you use according to the documentation . I found it, iterating over all the columns returned by the cursor. There are some more interesting columns, such as "hasLocal", "hasRemote", which may (or not) refer to where the music is stored.

 String[] proj2 = { "SourceId", MediaStore.Audio.Playlists.Members.TITLE, MediaStore.Audio.Playlists.Members._ID }; String playListRef = "content://com.google.android.music.MusicContent/playlists/" + playListId + "/members"; Uri songUri = Uri.parse(playListRef); Cursor songCursor = getContentResolver().query(songUri, proj2, null, null, null); long audioId = -1; while (songCursor.moveToNext()) { audioId = songCursor.getLong(songCursor.getColumnIndex("SourceId")); String title = songCursor.getString(songCursor.getColumnIndex(MediaStore.Audio.Playlists.Members.TITLE)); Log.v("", "audioId: " + audioId + ", title: " + title); } songCursor.close(); MediaPlayer mpObject = new MediaPlayer(); try { if (audioId > 0) { Uri contentUri = ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Long.valueOf(audioId)); mpObject.setAudioStreamType(AudioManager.STREAM_MUSIC); mpObject.setDataSource(this, contentUri); mpObject.prepare(); mpObject.start(); } } catch (Exception e) { e.printStackTrace(); } 
+1
source

All Articles