Adn*_*had 5 android keyevent media-player android-bluetooth android-mediasession
好吧,我知道已经有人问过很多类似的问题,但到目前为止没有任何效果。我已经尝试了所有 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(),"Play Button is pressed!",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(),"Stop Button is pressed!",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", Toast.LENGTH_LONG).show();
Log.e("SERVICE", "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);
}
}
Run Code Online (Sandbox Code Playgroud)
清单.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>
Run Code Online (Sandbox Code Playgroud)
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));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2226 次 |
| 最近记录: |