【问题标题】:Starting a Song from Spotify Intent从 Spotify Intent 开始歌曲
【发布时间】:2012-09-23 18:53:11
【问题描述】:

有没有办法从它的 URI 开始一个 Spotify 音轨?

我尝试了以下方法,但都没有奏效。当 Spotify 打开时,它始终位于播放列表页面,而不是曲目的播放器。

String spotifyTrackURI = "spotify:track:1cC9YJ8sQjC0T5H1fXMUT2";
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.spotify.mobile.android.ui");

// I've tried with Intent#putExtra()..
launchIntent.putExtra( SearchManager.QUERY, spotifyTrackURI );
// or with setData
launchIntent.setData(Uri.parse(spotifyTrackURI))

context.startActivity(launchIntent);

谢谢

【问题讨论】:

    标签: android android-intent spotify


    【解决方案1】:

    在 Freenode (irc) 的 #spotify 频道上找到了答案。谢谢大家!

    我把它放在这里让其他人知道:

    // right click on a track in Spotify to get the URI, or use the Web API.
    String uri = "spotify:track:<spotify uri>"; 
    Intent launcher = new Intent( Intent.ACTION_VIEW, Uri.parse(uri) );
    startActivity(launcher);
    

    【讨论】:

    • 这不再有效 - Spotify 应用程序打开,但音乐不会自动播放。有谁知道如何让它工作?
    【解决方案2】:

    第一个是旧的 Spotify 版本。 对于新版本,您可以使用第二个。

    这样,它会首先尝试旧版本,如果失败并出现异常,它会尝试第二个:)

    try {
                    final Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
                    intent.setComponent(new ComponentName("com.spotify.mobile.android.ui", "com.spotify.mobile.android.ui.Launcher"));
                    intent.putExtra(SearchManager.QUERY, artistName + " " + trackName );
                    context.startActivity(intent);
                } catch ( ActivityNotFoundException e ) {
                    final Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
                    intent.setComponent(new ComponentName("com.spotify.mobile.android.ui", "com.spotify.mobile.android.ui.activity.MainActivity"));
                    intent.putExtra(SearchManager.QUERY, artistName + " " + trackName );
                    context.startActivity(intent);
                }
    

    【讨论】:

    • 我不确定我是否理解,这段代码中传递的 URI 是在哪里?
    • 我认为 spotify 不会在他的搜索意图中使用 uri。在我的示例中,他正在使用带有曲目名称和艺术家姓名的搜索。
    • 好的,我现在明白了,不幸的是我没有尝试进行搜索,我已经有了我想在应用程序上播放的歌曲的 URI。我想知道,您是如何发现 Spotify 支持这种搜索意图类型的。是否知道 Spotify(或任何 Android 应用程序)支持哪些 Intent 类型?
    • 大多数优秀的应用程序都支持这种意图 :) 你只需要找到要调用的包和类:P
    【解决方案3】:

    如果您在 Spotify 应用上获得艺术家的播放列表。您需要开始遵循意图。您不需要打包 Spotify 的名称或活动。

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("spotify:artist:" + "ARTIST SPOTIFY LINK"));
                    startActivity(intent);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多