【问题标题】:Spotify Android Intent Play on LaunchSpotify Android Intent Play on Launch
【发布时间】:2015-04-15 22:32:09
【问题描述】:

我试图让 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);
        }

【问题讨论】:

  • 你说如果你指定一个艺术家,你可以让它自动播放。我不能让它工作。搜索结果出现但没有自动播放。有任何想法吗?如果我在搜索艺术家后使用下面的解决方案,它只会播放之前播放的内容。
  • 你能发布到目前为止你在代码中拥有的东西吗?
  • 添加了另一个答案,以显示艺术家如何在下面播放。

标签: android android-intent spotify


【解决方案1】:

我花了一段时间才弄清楚这一点,所以我想我会发布我使用的解决方案。我遍历了所有订阅 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 不在后台运行时它不会播放音乐。你能建议做什么吗?
【解决方案2】:

这是按艺术家搜索并在 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 Premium 看看这是否有所作为——现在它可以工作了!!愚蠢的。那好吧。现在我正在努力让播放列表工作......
  • 这是有道理的,当您不在高级版时,它们确实会锁定许多功能。真高兴你做到了。这是我获得大部分信息的网站。应该可以帮助您调整播放列表播放。 developer.android.com/guide/components/…
猜你喜欢
  • 2011-07-12
  • 2018-11-04
  • 1970-01-01
  • 2012-08-14
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
相关资源
最近更新 更多