Android:如何使用KeyEvents快速调用MediaController并快退

时间:2014-02-14 11:29:21

标签: android android-mediaplayer onkeydown mediacontroller

您好我正在使用mediacontroller来控制我的媒体播放器,我想控制使用keyEvents而不是touchevents,我使用下面的代码来执行此操作,但只有Play / Pause正在运行,其中 KEYCODE_MEDIA_FAST_FORWARD 和 KEYCODE_MEDIA_REWIND

任何人都可以建议如何使用关键事件来快速前进和后退。

请帮忙。

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    mediaController.show();
    switch (keyCode) {
    case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        if (mediaPlayer.isPlaying()) {
            pause();
        } else
            start();

        return true;
    case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: {
        if (mediaPlayer.isPlaying()) {
        currentPosition = mediaPlayer.getCurrentPosition();
        seekTo(currentPosition + 2000);
        }
    }
        return true;

    case KeyEvent.KEYCODE_MEDIA_REWIND: {
        if (mediaPlayer.isPlaying()) {
        currentPosition = mediaPlayer.getCurrentPosition();
        seekTo(currentPosition - 3000);
        }
    }
        return true;

    }
    return false;
}

1 个答案:

答案 0 :(得分:0)

我创建了两个函数doForwardVideo()和doRewindVideo()并在onClick侦听器和onKeyDownEvent()中使用它

希望这会有所帮助!!

public void doForwardVideo()
    {
         if (mPlayer == null) {
             return;
         }
         int pos = mPlayer.getCurrentPosition();
         pos += 15000; // milliseconds
         mPlayer.seekTo(pos);
         setProgress();

         show(sDefaultTimeout);
    }

    public void doRewindVideo()
    {
        if (mPlayer == null) {
            return;
        }

        int pos = mPlayer.getCurrentPosition();
        pos -= 5000; // milliseconds
        mPlayer.seekTo(pos);
        setProgress();

        show(sDefaultTimeout);
    }


    //*** Create two listeners which then you will set it in 
    //private void initControllerView(View v) method

    private View.OnClickListener mRewListener = new View.OnClickListener() {
        public void onClick(View v) {
            doRewindVideo();
        }
    };

    private View.OnClickListener mFfwdListener = new View.OnClickListener() {
        public void onClick(View v) {
            doForwardVideo();
        }
    };

//****** Here is snippet for private void initControllerView(View v) method

mFfwdButton = (ImageButton) v.findViewById(R.id.ffwd);
    if (mFfwdButton != null) {
        mFfwdButton.setOnClickListener(mFfwdListener);
        if (!mFromXml) {
            mFfwdButton.setVisibility(mUseFastForward ? View.VISIBLE : View.GONE);
        }
    }

    mRewButton = (ImageButton) v.findViewById(R.id.rew);
    if (mRewButton != null) {
        mRewButton.setOnClickListener(mRewListener);
        if (!mFromXml) {
            mRewButton.setVisibility(mUseFastForward ? View.VISIBLE : View.GONE);
        }
    }

//***** onKeyDown() Event ****
 @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (mPlayer == null) {
            return true;
        }

        int keyCode = event.getKeyCode();
        final boolean uniqueDown = event.getRepeatCount() == 0
                && event.getAction() == KeyEvent.ACTION_DOWN;
        if (keyCode ==  KeyEvent.KEYCODE_HEADSETHOOK
                || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
                || keyCode == KeyEvent.KEYCODE_SPACE) {
            if (uniqueDown) {
                doPauseResume();
                show(sDefaultTimeout);
                if (mPauseButton != null) {
                    mPauseButton.requestFocus();
                }
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
            if (uniqueDown && !mPlayer.isPlaying()) {
                mPlayer.start();
                updatePausePlay();
                show(sDefaultTimeout);
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
                || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
            if (uniqueDown && mPlayer.isPlaying()) {
                mPlayer.pause();
                updatePausePlay();
                show(sDefaultTimeout);
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_FAST_FORWARD) {
            if (uniqueDown) {
                doForwardVideo();
            }
            // don't show the controls for volume adjustment
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MEDIA_REWIND) {
            if (uniqueDown) {
                doRewindVideo();
            }
            // don't show the controls for volume adjustment
            return true;
        }else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
                || keyCode == KeyEvent.KEYCODE_VOLUME_UP
                || keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) {
            // don't show the controls for volume adjustment
            return super.dispatchKeyEvent(event);
        } else if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU) {
            if (uniqueDown) {
                hide();
            }
            return true;
        }

        show(sDefaultTimeout);
        return super.dispatchKeyEvent(event);
    }