如何检测和覆盖蓝牙耳机上的媒体按钮按键事件?

问题描述

好的,所以我知道有人问过许多类似的问题,但到目前为止没有任何进展。我已经尝试了所有的mediaSessionCompat方法和回调,但是没有运气。我已经阅读了文档,但是尤其是关于新实践,它非常模糊。我基本上想覆盖蓝牙耳机的媒体按钮,并停止播放认的音乐播放器。

编辑:因此,在深入了解文档之后,我提出了以下内容

mediaplaybackService.java

public class mediaplaybackService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    private MediaSessionCompat.Callback mediaSessionCompatCallBack = new MediaSessionCompat.Callback()
    {
        @Override
        public void onPlay() {
            super.onPlay();
            Toast.makeText(getApplication(),"Play Button is pressed!",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPause() {
            super.onPause();
            Toast.makeText(getApplication(),"Pause Button is pressed!",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onSkipToNext() {
            super.onSkipToNext();
            Toast.makeText(getApplication(),"Next Button is pressed!",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onSkipToPrevIoUs() {
            super.onSkipToPrevIoUs();
            Toast.makeText(getApplication(),"PrevIoUs Button is pressed!",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStop() {
            super.onStop();
            Toast.makeText(getApplication(),"Stop Button is pressed!",Toast.LENGTH_SHORT).show();
        }

        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            String intentAction = mediaButtonEvent.getAction();

            if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction))
            {
                KeyEvent event = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

                if (event != null)
                {
                    int action = event.getAction();
                    if (action == KeyEvent.ACTION_DOWN) {
                        switch (event.getKeyCode()) {
                            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
                                // code for fast forward
                                return true;
                            case KeyEvent.KEYCODE_MEDIA_NEXT:
                                // code for next
                                return true;
                            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                                // code for play/pause
                                Toast.makeText(getApplication(),Toast.LENGTH_SHORT).show();
                                return true;
                            case KeyEvent.KEYCODE_MEDIA_PREVIoUS:
                                // code for prevIoUs
                                return true;
                            case KeyEvent.KEYCODE_MEDIA_REWIND:
                                // code for rewind
                                return true;
                            case KeyEvent.KEYCODE_MEDIA_STOP:
                                // code for stop
                                Toast.makeText(getApplication(),Toast.LENGTH_SHORT).show();
                                return true;

                        }
                        return false;

                    }
                    if (action == KeyEvent.ACTION_UP) {

                    }
                }
            }
            return super.onMediaButtonEvent(mediaButtonEvent);
        }
    };

    private MediaSessionCompat mediaSessionCompat;


    @Override
    public void onCreate() {
        Toast.makeText(this,"My Service Created",Toast.LENGTH_LONG).show();
        Log.e("SERVICE","onCreate");

        mediaSessionCompat = new MediaSessionCompat(this,"MEDIA");
        mediaSessionCompat.setCallback(mediaSessionCompatCallBack);
        mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        PlaybackStateCompat.Builder mStateBuilder = new PlaybackStateCompat.Builder()
                .setActions(
                        PlaybackStateCompat.ACTION_PLAY |
                                PlaybackStateCompat.ACTION_PAUSE |
                                PlaybackStateCompat.ACTION_SKIP_TO_PREVIoUS |
                                PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
                                PlaybackStateCompat.ACTION_PLAY_PAUSE);

        mediaSessionCompat.setPlaybackState(mStateBuilder.build());
        mediaSessionCompat.setActive(true);

    }

    @Override
    public void onDestroy() {
        Toast.makeText(this,"My Service Stopped","onDestroy");
        mediaSessionCompat.release();
    }

    @Override
    public int onStartCommand(Intent intent,int flags,int startId) {
        Toast.makeText(this,"My Service Started",Toast.LENGTH_LONG).show();
        Log.e("SERVICE_STARTUP","onStart");

        MediaButtonReceiver.handleIntent(mediaSessionCompat,intent);

        return super.onStartCommand(intent,flags,startId);
    }
}

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pk.mohammadadnan.tunalapp">

    <uses-permission android:name="android.permission.MODIFY_AUdio_SETTINGS" />
    <uses-feature android:name="android.hardware.bluetooth" />
    <uses-permission android:name="android.permission.BLUetoOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUetoOTH" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <receiver android:name="androidx.media.session.MediaButtonReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
            </intent-filter>
        </receiver>
        <service android:name="pk.mohammadadnan.tunalapp.mediaplaybackService" >
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
            </intent-filter>
        </service>
    </application>

</manifest>

MainActivity.java

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this,mediaplaybackService.class));

        
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)