diff --git a/app/src/main/java/com/yutou/nas_music_player/containers/MusicContainer.java b/app/src/main/java/com/yutou/nas_music_player/containers/MusicContainer.java index a54eaf7..eb1ac5b 100644 --- a/app/src/main/java/com/yutou/nas_music_player/containers/MusicContainer.java +++ b/app/src/main/java/com/yutou/nas_music_player/containers/MusicContainer.java @@ -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(); + } + } + } /** * 播放下一曲 diff --git a/app/src/main/java/com/yutou/nas_music_player/services/MusicReceiver.java b/app/src/main/java/com/yutou/nas_music_player/services/MusicReceiver.java new file mode 100644 index 0000000..da55cd9 --- /dev/null +++ b/app/src/main/java/com/yutou/nas_music_player/services/MusicReceiver.java @@ -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(); + } + } +} diff --git a/app/src/main/java/com/yutou/nas_music_player/services/MusicService.java b/app/src/main/java/com/yutou/nas_music_player/services/MusicService.java index c213b06..41ad34c 100644 --- a/app/src/main/java/com/yutou/nas_music_player/services/MusicService.java +++ b/app/src/main/java/com/yutou/nas_music_player/services/MusicService.java @@ -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,21 +121,26 @@ 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); Notification notification = notificationManager.getNotification( - metadataCompat, builderState(0).build(), getSessionToken()); + metadataCompat, builderState(0).build(), getSessionToken()); 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); } }