How to play or resume music of another music player from my code

北战南征 提交于 2019-12-20 08:46:19

问题


In my Android app I want to play or resume the played music after I pause it. I got my app to pause the music by sending a broadcast, but I can't get it to play or resume the music.

Here is the code to pause:

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

回答1:


Here is what I have discovered after testing these.

I have tried and verified these commands to work just fine.

// 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);

Some more info about available commands:

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";

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




回答2:


Try to use

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

Update

Try with the following action :

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


来源:https://stackoverflow.com/questions/14910360/how-to-play-or-resume-music-of-another-music-player-from-my-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!