修复耳机断开时会自动播放的问题

This commit is contained in:
Yutousama 2020-11-25 21:09:04 +08:00
parent 0d446525ac
commit 912762c72f
3 changed files with 40 additions and 5 deletions

View File

@ -259,6 +259,13 @@ public class MusicContainer {
isPause = true;
mediaPlayer.pause();
}
public void receiverPause(){
for (MediaBrowserHelper helper : browserHelpers) {
if(helper.getTransportControls()!=null){
helper.getTransportControls().pause();
}
}
}
/**
* 播放下一曲

View File

@ -0,0 +1,20 @@
package com.yutou.nas_music_player.services;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import com.yutou.nas_music_player.MyApplication;
import com.yutou.nas_music_player.containers.MediaBrowserHelper;
import com.yutou.nas_music_player.containers.MusicContainer;
public class MusicReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
// Pause the playback
MusicContainer.getInstance().receiverPause();
}
}
}

View File

@ -1,6 +1,8 @@
package com.yutou.nas_music_player.services;
import android.app.Notification;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.SystemClock;
@ -119,7 +121,10 @@ public class MusicService extends MediaBrowserServiceCompat {
}
public class MediaSessionCallback extends MediaSessionCompat.Callback {
public void updateNotification(){
MusicReceiver receiver = new MusicReceiver();;
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);;
public void updateNotification() {
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat();
mediaSession.setMetadata(metadataCompat);
mediaSession.setActive(true);
@ -129,11 +134,13 @@ public class MusicService extends MediaBrowserServiceCompat {
notificationManager.getManager()
.notify(MediaNotificationManager.NOTIFICATION_ID, notification);
}
@Override
public void onPlay() {
super.onPlay();
System.out.println("接收到播放指令");
mState=PlaybackStateCompat.STATE_PLAYING;
registerReceiver(receiver, intentFilter);
mState = PlaybackStateCompat.STATE_PLAYING;
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat();
mediaSession.setMetadata(metadataCompat);
mediaSession.setActive(true);
@ -145,7 +152,7 @@ public class MusicService extends MediaBrowserServiceCompat {
@Override
public void onPause() {
super.onPause();
mState=PlaybackStateCompat.STATE_PAUSED;
mState = PlaybackStateCompat.STATE_PAUSED;
MusicContainer.getInstance().pause();
//MusicService.this.onSeekTo(-1);
}
@ -176,8 +183,9 @@ public class MusicService extends MediaBrowserServiceCompat {
@Override
public void onStop() {
super.onStop();
mState=PlaybackStateCompat.STATE_STOPPED;
mState = PlaybackStateCompat.STATE_STOPPED;
MusicContainer.getInstance().getMediaPlayer().stop();
unregisterReceiver(receiver);
}
}