无法在Google音乐播放器中播放歌曲

时间:2012-04-02 21:05:24

标签: android audio-player android-music-player

有没有办法从我的应用程序开始在Google音乐播放器应用中播放歌曲?我正在尝试关注代码,但谷歌音乐播放器只会打开搜索结果&实际上并没有播放这首歌。

    Intent intent = new Intent();
    intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
    intent.putExtra(SearchManager.QUERY, "It's my life");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setPackage("com.google.android.music");
    activity.startActivity(intent);

INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH的文档说明如下 - 意图执行搜索音乐媒体并尽可能自动播放结果中的内容。

因此,根据文档,它应该能够播放这首歌。但是,它只会打开搜索结果而不会播放。我在这里缺少什么想法?

感谢您提供的任何帮助。

3 个答案:

答案 0 :(得分:2)

Argh,终于通过获取Play Music流程的堆转储来解决这个问题。你需要添加

intent.putExtra("queryComplete", "It's my life");

一切都会奏效。该值必须与SearchManager.QUERY额外值相同。

答案 1 :(得分:0)

我找到了一种方法。

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);

答案 2 :(得分:0)

Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.google.android.music");
if (i == null)
     throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
相关问题