Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 耳机按钮控制安卓5.0_Android_Android Mediasession - Fatal编程技术网

Android 耳机按钮控制安卓5.0

Android 耳机按钮控制安卓5.0,android,android-mediasession,Android,Android Mediasession,我正在写一个程序,捕捉耳机按钮按下,然后根据你按下的时间和次数做不同的事情。进入api级别21。安卓5.0。 所以我可以捕捉按键,但当启动mp3播放器时,它就会捕捉按键。我怎样才能防止呢? 第二个问题如何恢复暂停的mp3,其和其他程序而不是播放mp3的我的程序 audioSession = new MediaSession(getApplicationContext(), "TAG11"); MsCallback= new MediaSession.Callback() {

我正在写一个程序,捕捉耳机按钮按下,然后根据你按下的时间和次数做不同的事情。进入api级别21。安卓5.0。 所以我可以捕捉按键,但当启动mp3播放器时,它就会捕捉按键。我怎样才能防止呢? 第二个问题如何恢复暂停的mp3,其和其他程序而不是播放mp3的我的程序

 audioSession = new MediaSession(getApplicationContext(), "TAG11");
    MsCallback=     new MediaSession.Callback() {

        @Override
        public boolean onMediaButtonEvent(final Intent mediaButtonIntent) {
            String intentAction = mediaButtonIntent.getAction();
            Log.i("onMediaButtonEvent", intentAction );
            if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
                KeyEvent event = mediaButtonIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

                if (event != null) {
                    if(toast!=null){
                        toast.cancel();
                    }
                    toast = Toast.makeText(getApplicationContext(), "headsetbutton press", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
            return super.onMediaButtonEvent(mediaButtonIntent);
        }


    };
    audioSession.setCallback(MsCallback);

    PlaybackState state = new PlaybackState.Builder()
            .setActions(PlaybackState.ACTION_PLAY_PAUSE)
            .setState(PlaybackState.STATE_PLAYING, 0, 0, 0)
            .build();
    audioSession.setPlaybackState(state);
    audioSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);

    audioSession.setActive(true);

为了监听媒体密钥事件,在清单文件中声明一个广播接收器,如下所示

<receiver android:name=".RemoteControlReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>

每个耳机上的基本按钮通常是按钮

,用于收听媒体按键事件,在清单文件中声明广播接收器,如下所示

<receiver android:name=".RemoteControlReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>

每个耳机上的基本按钮通常是按钮

,可能无法阻止其他程序将听众放在按钮上。你也许可以监听新的侦听器,然后在它们连接时删除它们,但我不知道这是否可行。Idk如果按下按钮是个好主意。可能会勾选将其用于音乐的用户。Thx用于回答。诀窍可能是你需要把它重新钩住。但是你怎么发现它被偷了呢?用户可以设置按钮的功能,将其设置为播放音乐,然后它将播放音乐。这有点像这个计划,但想让更多的选择。有没有办法获得一个可变的侦听器列表?可能问题是当mp3播放器开始付费时,它的MediaSession对象获得了优先级,并且有自己的回调。因此,我应该设置MediaSession对象以恢复优先级。但是不知道怎么做。你真的想调用
super.onMediaButtonEvent()
并返回它的值吗?调用可能什么也不做,但它返回false,而文档说明如果按钮事件被处理,它应该返回true。可能无法阻止其他程序在按钮上放置侦听器。你也许可以监听新的侦听器,然后在它们连接时删除它们,但我不知道这是否可行。Idk如果按下按钮是个好主意。可能会勾选将其用于音乐的用户。Thx用于回答。诀窍可能是你需要把它重新钩住。但是你怎么发现它被偷了呢?用户可以设置按钮的功能,将其设置为播放音乐,然后它将播放音乐。这有点像这个计划,但想让更多的选择。有没有办法获得一个可变的侦听器列表?可能问题是当mp3播放器开始付费时,它的MediaSession对象获得了优先级,并且有自己的回调。因此,我应该设置MediaSession对象以恢复优先级。但是不知道怎么做。你真的想调用
super.onMediaButtonEvent()
并返回它的值吗?调用可能什么也不做,但它返回false,而文档说明如果按钮事件被处理,它应该返回true。您需要使用mAudioManager.registerDiaButtoneVentReceiver(mRemoteControlResponder);您应该包括如何钩住RemoteControlResponder以获得目的。像这样的`mAudioManager=(AudioManager)getSystemService(Context.AUDIO\u SERVICE);mRemoteControlResponder=新组件名(getPackageName(),RemoteControlReceiver.class.getName());mAudioManager.RegisterMediaButtonVentreceiver(mRemoteControlResponder);`它只能在5.0以下工作。甚至不确定它在4.4上是否可以工作。在这里,您使用MediaSessionCompat。您需要使用mAudioManager.RegisterDiaButtoneVentReceiver(mRemoteControlResponder);您应该包括如何钩住RemoteControlResponder以获得目的。像这样的`mAudioManager=(AudioManager)getSystemService(Context.AUDIO\u SERVICE);mRemoteControlResponder=新组件名(getPackageName(),RemoteControlReceiver.class.getName());mAudioManager.RegisterMediaButtonVentreceiver(mRemoteControlResponder);`它只能在5.0以下工作。甚至不确定它是否能在4.4版上运行,因为您使用的是MediaSessionCompat。