首页 文章

耳机按钮控制

提问于
浏览
0

现在发生的事情是它还在控制其他音乐播放器 . 如果我在启动后有一个默认音乐播放器,如果我开始使用我的音乐播放器播放然后我按下耳机中的播放/暂停按钮它控制默认音乐播放器 . 我已经实现了像这样的heaset broadcastreceiver,

switch (keyEvent.getKeyCode()) {
                case KeyEvent.KEYCODE_HEADSETHOOK:
                    Log.d("TAG", "TAG: KEYCODE_HEADSETHOOK");
                    if (MusicService.isPlaying()) {
                        MusicService.player.pause();
                    } else {
                        MusicService.player.start();
                    }
                    new MusicService().showNotification();
                    break;
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                    Log.d("TAG", "TAG: KEYCODE_MEDIA_PLAY");
                    if (MusicService.isPlaying()) {
                        MusicService.player.pause();
                    } else {
                        MusicService.player.start();
                    }
                    new MusicService().showNotification();
                    break;
                case KeyEvent.KEYCODE_MEDIA_PLAY:
                    break;
                case KeyEvent.KEYCODE_MEDIA_PAUSE:
                    Log.d("TAG", "TAG: KEYCODE_MEDIA_PAUSE");
                    break;
                case KeyEvent.KEYCODE_MEDIA_STOP:
                    break;
                case KeyEvent.KEYCODE_MEDIA_NEXT:
                    Log.d("TAG", "TAG: KEYCODE_MEDIA_NEXT");
                    if (MusicService.player.isPlaying()) {
                        MusicService.player.stop();
                    }
                    if (MusicService.songPosn < (MusicService.song.size() - 1)) {
                        MusicService.songPosn = MusicService.songPosn + 1;
                        if (SingleInstance.getInstance().getIndexCount()>= 1)
                            SingleInstance.getInstance().setIndexCount(SingleInstance.getInstance().getIndexCount()-1);
                    } else {
                        // play first song
                        MusicService.songPosn = 0;
                    }
                    new MusicService().playSong(true);
                    break;
                case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                    Log.d("TAG", "TAG: KEYCODE_MEDIA_PREVIOUS");
                    if (MusicService.player.isPlaying()) {
                        MusicService.player.stop();
                    }
                    if (MusicService.songPosn > 0) {
                        MusicService.songPosn = MusicService.songPosn - 1;
                    } else {
                        // play last song
                        MusicService.songPosn = MusicService.song.size() - 1;
                    }
                    new MusicService().playSong(true);
                    break;
            }

1 回答

  • 0

    如果您想在活动中获取hedsethook事件,请遵循以下代码:

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_HEADSETHOOK:
                    // Headset key detected
                   // Do your stuff here
                    return true;// To let the os know that the event is consumed here 
            }
        }
        return super.dispatchKeyEvent(event);
    }
    

    确保使用true来消耗事件 . 要在服务中完成它,请使用BroadCast接收器,如下所示:

    IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);//"android.intent.action.MEDIA_BUTTON"
    MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
    filter.setPriority(1000); //Sets receiver priority
    registerReceiver(r, filter);
    

相关问题