Spotify Android Intent Play on Launch
Spotify Android Intent Play on Launch
我试图让 Spotify 在从 intent 启动时恢复播放,但运气不佳。我想我已经接近启动 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);
}
我花了一段时间才弄明白这个问题,所以我想我会 post 我使用的解决方案。我遍历了所有订阅 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);
}
以下是按艺术家搜索并在 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);
}
}
我试图让 Spotify 在从 intent 启动时恢复播放,但运气不佳。我想我已经接近启动 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);
}
我花了一段时间才弄明白这个问题,所以我想我会 post 我使用的解决方案。我遍历了所有订阅 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);
}
以下是按艺术家搜索并在 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);
}
}