【问题标题】:Control playback of the Spotify app from another Android app?从另一个 Android 应用程序控制 Spotify 应用程序的播放?
【发布时间】:2015-02-03 02:44:19
【问题描述】:

是否可以在另一个 Android 应用中控制 Spotify 应用的播放?我只是在寻找跳轨功能(向前和向后)。

我知道 Spotify Android SDK,但它似乎只允许跳过 SDK 播放的曲目:

com.spotify.sdk.android.playback.NativeSpotifyException: Failed SpPlaybackSkipToPrev with  error code 14 (The operation is not supported if the device is not the active playback device)

澄清一下,实际的 Spotify 应用和我自己的应用都在同一台设备上运行

【问题讨论】:

    标签: android spotify


    【解决方案1】:

    这是怎么做的:

    这将尝试播放/暂停 Spotify。如果它没有运行,它将启动它并让它开始播放。

        public void nextSong() {
    
        int keyCode = KeyEvent.KEYCODE_MEDIA_NEXT;
    
        if (!isSpotifyRunning()) {
            startMusicPlayer();
        }
    
        Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        intent.setPackage("com.spotify.music");
        synchronized (this) {
            intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
            getContext().sendOrderedBroadcast(intent, null);
    
            intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, keyCode));
            getContext().sendOrderedBroadcast(intent, null);
        }
    }
    
    public void playPauseMusic() {
        int keyCode = KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE;
    
        if (!mAudioManager.isMusicActive() && !isSpotifyRunning()) {
            startMusicPlayer();
        }
    
        Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
        i.setPackage("com.spotify.music");
        synchronized (this) {
            i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
            getContext().sendOrderedBroadcast(i, null);
    
            i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, keyCode));
            getContext().sendOrderedBroadcast(i, null);
        }
    }
    
    private void startMusicPlayer() {
        Intent startPlayer = new Intent(Intent.ACTION_MAIN);
        startPlayer.setPackage("com.spotify.music");
        startPlayer.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getContext().startActivity(startPlayer);
    
        if (mMusicPlayerStartTimer != null) {
            mMusicPlayerStartTimer.cancel();
        }
    
        mMusicPlayerStartTimer = new Timer("MusicPlayerStartTimer", true);
        mMusicPlayerStartTimer.schedule(new MusicPlayerStartTimerTask(), DateUtils.SECOND_IN_MILLIS, DateUtils.SECOND_IN_MILLIS);
    }
    
    private boolean isSpotifyRunning() {
        Process ps = null;
        try {
            String[] cmd = {
                    "sh",
                    "-c",
                    "ps | grep com.spotify.music"
            };
    
            ps = Runtime.getRuntime().exec(cmd);
            ps.waitFor();
    
            return ps.exitValue() == 0;
        } catch (IOException e) {
            Log.e(DEBUG_TAG, "Could not execute ps", e);
        } catch (InterruptedException e) {
            Log.e(DEBUG_TAG, "Could not execute ps", e);
        } finally {
            if (ps != null) {
                ps.destroy();
            }
        }
    
        return false;
    }
    
    private class MusicPlayerStartTimerTask extends TimerTask {
        @Override
        public void run() {
            if (isSpotifyRunning()) {
                playPauseMusic(null);
                cancel();
            }
        }
    }
    

    编辑:添加完整的示例代码

    【讨论】:

    • 你能跳过曲目吗?
    【解决方案2】:

    是的,您可以使用 RemoteController 类控制播放,或者如果使用 Lollipop,则使用 MediaController 类,或者如果支持 L 和更早版本,则使用 MediaControllerCompat 类。

    然后使用KEYCODE_MEDIA_NEXT 执行dispatchMediaButtonEvent()。

    【讨论】:

    • 你能用 Spotify 做到这一点吗?似乎它对我不起作用,而不是 Spotify 没有给你一个 MediaSessionCompat.Token
    • 我通过获得通知侦听器权限来做不同的事情
    【解决方案3】:

    快速回答 - 不,这是不可能的。

    【讨论】:

    • 我认为这是来自 Spotify 员工的正确答案
    • 那时可能不是,但至少现在是
    • @Auras,你能跳过曲目吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 2011-03-07
    • 1970-01-01
    相关资源
    最近更新 更多