0

我注册了一个广播接收器来接收“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”,谁能解释一下?提前谢谢。

4

1 回答 1

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;
    }
}
于 2013-01-07T04:34:17.940 回答