暂停/播放媒体播放器控件不起作用

时间:2019-04-06 10:35:49

标签: java android controls

我对Android编程非常陌生,它试图为Android构建媒体播放器应用。我在使用耳机控制音频时遇到问题,+ vol和–vol工作正常,但暂停/播放无效。感谢您的协助。

这正在注册接收者:

public void onResume() {
    super.onResume();
    audioManager.registerMediaButtonEventReceiver(myRemoteControlResponder);
    Toast.makeText(getApplicationContext(), "Registering", Toast.LENGTH_LONG).show();
}

这正在取消接收者的注册:

 @Override
    protected void onDestroy() {
        cleanUp();
        super.onDestroy();
        unregisterReceiver(); 
        audioManager.unregisterMediaButtonEventReceiver(
        myRemoteControlResponder);

这是我正在使用的课程:

public class RemoteControlReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(getApplicationContext(), "Implementing", Toast.LENGTH_LONG).show();
        if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
            final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            if (event.getAction() != KeyEvent.ACTION_DOWN) return;
                MediaPlayer mp = new MediaPlayer();
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_MEDIA_STOP:
                    // stop music
                    break;
                case KeyEvent.KEYCODE_HEADSETHOOK:
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                    // pause music                       
                    if (mp != null) {
                        mp.pause();
                        paused = true;
                        blink();
                        btnPlay.setBackgroundResource(R.drawable.ic_play_arrow_black_24dp);
                    }
                    break;
                case KeyEvent.KEYCODE_MEDIA_NEXT:
                    // next track
                    btnNxt.performClick();
                    break;
                case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                    // previous track
                    btnPv.performClick();
                    break;
                case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
                    // code for fast forward
                    btnFF.performClick();
                    break;
                case KeyEvent.KEYCODE_MEDIA_REWIND:
                    // code for rewind
                    btnFB.performClick();
                   break;
            }
        }
    }
}

这在onCreate中定义了接收者:

audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    myRemoteControlResponder = new ComponentName(getPackageName(),
            RemoteControlReceiver.class.getName());

我希望使用耳机控制在暂停和播放之间切换。

0 个答案:

没有答案