如何让用户在Android上查看和播放他们已经访问过的歌曲(例如,通过Google Play)?

gcl*_*cl1 7 android android-intent android-music-player android-contentprovider google-play

我遇到了将音乐整合到我的Android应用中的障碍.目标只是让用户通过Google Play音乐(或任何其他播放器,如Winamp,Poweramp,doubleTwist等)查看和播放他们已经访问过的歌曲.

到目前为止,我已经尝试了几种方法,但每种方法都存在问题.

方法#1:使用ContentResolver查询从Google Play音乐中获取播放列表和歌曲,然后使用MediaPlayer播放

首先,在播放列表上获取光标:

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);
Run Code Online (Sandbox Code Playgroud)

然后迭代以获取每个播放列表中的歌曲,并按如下方式读取它们:

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);
Run Code Online (Sandbox Code Playgroud)

这可以获取播放列表和歌曲,但实际上您无法播放它们(即使您将Google Play音乐告诉"保留在设备上").具体来说,以下代码不播放如上所示的给定歌曲:

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

事实上,我已经扫描了设备,并且无法在任何地方找到这些文件(尽管我已经通过完全禁用网络确认它们是本地的 - 在Galaxy Nexus和Galaxy S3上).

方法#2:使用Intent调用Google Play音乐或其他播放器

根据这篇文章,您可以使用Intents简单地调用播放器,如下所示:

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);
Run Code Online (Sandbox Code Playgroud)

但我不认为这种方法允许任何类型的控制 - 例如,传递要播放的特定歌曲,或在播放歌曲时通知.此外,@ CommonWares指出音乐应用程序及其Intent不是Android SDK的公共API的一部分,并且可能会在没有警告的情况下进行更改.

由于与iTunes紧密集成,我试图实现的基本功能存在于iOS中.我认为谷歌希望实现同样的整合,因为这可能意味着在Play商店销售更多音乐.

摘要:是否可以让用户查看和播放他们已在Android上访问过的歌曲?使用Google Play音乐或任何其他应用的最佳方式是什么?谢谢.

小智 1

方法#1 有点管用(在设备上使用本地音乐进行测试),感谢 OP 的指导。

笔记:

  • 我们在这里使用一个未记录的内部 API - 它不可靠。
  • 我无法访问 Google Play 音乐中的在线/购买的音乐,因此答案仅指本地存储的音乐。

下面的代码将迭代播放列表中的所有歌曲并播放最后一首。

请注意“SourceId”列 - 这是您根据文档使用的音频 ID 。我通过迭代游标返回的所有列找到了它。那里还有几个更有趣的列,例如“hasLocal”、“hasRemote”,它们可能(或不是)指音乐的存储位置。

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();
}
Run Code Online (Sandbox Code Playgroud)