如何解决如何检测和覆盖蓝牙耳机上的媒体按钮按键事件?
好的,所以我知道有人问过许多类似的问题,但到目前为止没有任何进展。我已经尝试了所有的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 举报,一经查实,本站将立刻删除。