最近手上的一个音乐播放的项目遇到这样一个需求:
    当AP开始播放音乐的时候,需要先去检查当前是否有别的AP,主要是android自带的Music,正在播放音乐,如果有,那么需要暂停(能暂停是最好了,做不到的话,直接停止它们也行)他们的播放。
    那么,想问下,有没有什么方法可能得到这个效果呢?即使没有通用的方法,那么android自带的Music有没有提供接口,让我能从外部停止它播放呢?

解决方案 »

  1.   

        使用AudioManager.isMusicActive()应该可以判断是否有音乐在播放,但是如何关闭呢?至少知道是哪个AP在播放啊?
      

  2.   

    private void stopUmileMusic() {
    Intent freshIntent = new Intent();
    freshIntent.setAction("com.android.music.musicservicecommand.pause");

    freshIntent.putExtra("command", "pause");
    sendBroadcast(freshIntent);
    }
    针对原生的music
      

  3.   

        经试验有效,谢谢piaozhiye!
      

  4.   

    private void pauseMusic() {
    Intent freshIntent = new Intent();
    freshIntent.setAction("com.android.music.musicservicecommand.pause");
    freshIntent.putExtra("command", "pause");
    sendBroadcast(freshIntent);
    }更确切的应该这样,music还有接收action的
      

  5.   

    具体见:
    http://blog.csdn.net/piaozhiye/archive/2011/05/13/6417318.aspx
      

  6.   

        恩,我稍稍做了修改,现在使用的是如下的版本:
             public static void togglePauseMusic(Context context)
        {
         Intent freshIntent = new Intent();
         freshIntent.setAction("com.android.music.musicservicecommand.togglepause");
         context.sendBroadcast(freshIntent);
        }
        这样的更加符合我的需求