为什么不能收到“com.android.music.musicservicecommand”?

时间:2012-09-14 08:56:24

标签: android android-mediaplayer

我注册广播接收器以接收“com.android.music.playstatechanged”和“com.android.music.musicservicecommand”

public void registerMusicServiceCommandReceiver() {
        if (mMusicCommandListener == null) {
                mMusicCommandListener = new BroadcastReceiver() {
                        @Override
                        public void onReceive(Context context, Intent intent) {
                                String action = intent.getAction();

                                if (action.equals("com.android.music.playstatechanged")) {
                                        Log.d(TAG, "Music play state changed");
                                }
                                if (action.equals("com.android.music.musicservicecommand")) {
                                        String cmd = intent.getStringExtra("command");
                                        Log.d(TAG, "Music Service command : "+cmd+ " received");
                                        if (cmd != null && cmd.equals("play")) {
                                                Log.d(TAG, "received music service cmd, play");
                                        }
                                        if (cmd != null && cmd.equals("pause")) {
                                                Log.d(TAG, "received music service cmd, pause");
                                        }
                                }
                        }
                };
                IntentFilter commandFilter = new IntentFilter();
                commandFilter.addAction("com.android.music.musicservicecommand");
                commandFilter.addAction("com.android.music.playstatechanged");
                registerReceiver(mMusicCommandListener, commandFilter);
        }
}

但我只能收到“com.android.music.playstatechanged”,无法收到“com.android.music.musicservicecommand”, 任何人都可以解释一下吗? Thx提前。

1 个答案:

答案 0 :(得分:0)

试试这个

    private void openVideo() {
    if (mUri == null || mSurfaceHolder == null)
        return;

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

    release(false);
    try {
        mDuration = -1;
        mCurrentBufferPercentage = 0;
        mMediaPlayer = new MediaPlayer(mContext);
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
                   mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);  

        mMediaPlayer.setOnInfoListener(mInfoListener);
        mMediaPlayer.setOnSeekCompleteListener(mSeekCompleteListener);
        mMediaPlayer.setOnSubtitleUpdateListener(mSubtitleUpdateListener);
        mMediaPlayer.setDataSource(mContext, mUri);
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.prepareAsync();
        mCurrentState = STATE_PREPARING;
        attachMediaController();
    } catch (IOException ex) {
        Log.e("Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer,
                    MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);  
        return;
    } catch (IllegalArgumentException ex) {
        Log.e("Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer,
                    MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);                 
        return;
    }
}