From 912762c72ffc55056045794e4352c1a244eaf457 Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Wed, 25 Nov 2020 21:09:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=80=B3=E6=9C=BA=E6=96=AD?= =?UTF-8?q?=E5=BC=80=E6=97=B6=E4=BC=9A=E8=87=AA=E5=8A=A8=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../containers/MusicContainer.java | 7 +++++++ .../services/MusicReceiver.java | 20 +++++++++++++++++++ .../services/MusicService.java | 18 ++++++++++++----- 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/yutou/nas_music_player/services/MusicReceiver.java 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); } }