我试图让Spotify在从意图启动但没有太多运气的情况下恢复播放。我想我已经接近了,我可以让Spotify推出,如果我指定一个艺术家的搜索,它会自动播放,但我真的只想让它恢复我上次播放的内容,但我尚未开始工作。这个网站似乎有可能,但到目前为止,Spotify只是启动并进入搜索屏幕。 http://developer.android.com/guide/components/intents-common.html#PlaySearch
到目前为止,这是我的代码:
final Intent intent1 = new Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
intent1.setComponent(new ComponentName("com.spotify.music", "com.spotify.music.MainActivity"));
intent1.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, "vnd.android.cursor.item/*");
intent1.putExtra(SearchManager.QUERY, "");
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (intent1.resolveActivity(getPackageManager()) != null) {
startActivity(intent1);
}
答案 0 :(得分:7)
我花了一些时间来弄清楚这一点,所以我想我会发布我用过的解决方案。我遍历了订阅Intent.ACTION_MEDIA_BUTTON的所有软件包,那时我发现了我需要的组件名称才能使其工作:
private void playPlayMusic() {
Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
i.setComponent(new ComponentName("com.spotify.music", "com.spotify.music.internal.receiver.MediaButtonReceiver"));
i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY));
sendOrderedBroadcast(i, null);
i = new Intent(Intent.ACTION_MEDIA_BUTTON);
i.setComponent(new ComponentName("com.spotify.music", "com.spotify.music.internal.receiver.MediaButtonReceiver"));
i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY));
sendOrderedBroadcast(i, null);
}
答案 1 :(得分:1)
以下是艺术家搜索并在Spotify中播放的例程:
public void playSearchArtist(String artist) {
Intent intent = new Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
intent.setComponent(new ComponentName("com.spotify.music", "com.spotify.music.MainActivity"));
intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE);
intent.putExtra(MediaStore.EXTRA_MEDIA_ARTIST, artist);
intent.putExtra(SearchManager.QUERY, artist);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}