【问题标题】:How to play or resume music of another music player from my code如何从我的代码播放或恢复另一个音乐播放器的音乐
【发布时间】:2013-02-01 08:27:24
【问题描述】:

在我的 Android 应用中,我想在暂停后播放或恢复播放的音乐。我将我的应用程序发送到 pause the music by sending a broadcast,但我无法让它播放或恢复音乐。

这里是暂停的代码:

Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
sendBroadcast(i);

【问题讨论】:

    标签: android android-audiomanager resume android-music-player


    【解决方案1】:

    这是我在测试后发现的。

    我已经尝试并验证了这些命令可以正常工作。

    // pause
    Intent i = new Intent("com.android.music.musicservicecommand");
    i.putExtra("command", "pause");
    sendBroadcast(i);
    
    // play
    Intent i = new Intent("com.android.music.musicservicecommand");
    i.putExtra("command", "play");
    sendBroadcast(i);
    
    // next
    Intent i = new Intent("com.android.music.musicservicecommand");
    i.putExtra("command", "next");
    sendBroadcast(i);
    
    // previous
    Intent i = new Intent("com.android.music.musicservicecommand");
    i.putExtra("command", "previous");
    sendBroadcast(i);
    

    更多关于可用命令的信息:

    public static final String SERVICECMD = "com.android.music.musicservicecommand";
    public static final String CMDNAME = "command";
    public static final String CMDTOGGLEPAUSE = "togglepause";
    public static final String CMDSTOP = "stop";
    public static final String CMDPAUSE = "pause";
    public static final String CMDPLAY = "play";
    public static final String CMDPREVIOUS = "previous";
    public static final String CMDNEXT = "next";
    

    取自: https://android.googlesource.com/platform/packages/apps/Music/+/master/src/com/android/music/MediaPlaybackService.java

    【讨论】:

    • 这些命令只有在默认音乐播放器播放歌曲时才有效。否则将无法正常工作
    【解决方案2】:

    尝试使用

    Intent i = new Intent("com.android.music.musicservicecommand");
    i.putExtra("command", "togglepause");
    sendBroadcast(i);
    

    更新

    尝试以下操作:

    Intent i = new Intent("com.android.music.musicservicecommand.togglepause");
    i.putExtra("command", "togglepause");
    sendBroadcast(i);
    

    【讨论】:

    • 它什么也没做。 i.putExtra("command", "pause"); 暂停音乐。我想恢复它
    • 如果您使用togglepause 不会暂停/恢复它?这很奇怪。我将深入挖掘并检查其他方法。
    • 可以“togglepause”暂停和恢复音乐吗?
    • 使用 i.putExtra("command", "play"); “暂停”之后。这将从暂停的地方恢复音乐。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多