修复耳机断开时会自动播放的问题
This commit is contained in:
parent
0d446525ac
commit
912762c72f
@ -259,6 +259,13 @@ public class MusicContainer {
|
|||||||
isPause = true;
|
isPause = true;
|
||||||
mediaPlayer.pause();
|
mediaPlayer.pause();
|
||||||
}
|
}
|
||||||
|
public void receiverPause(){
|
||||||
|
for (MediaBrowserHelper helper : browserHelpers) {
|
||||||
|
if(helper.getTransportControls()!=null){
|
||||||
|
helper.getTransportControls().pause();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 播放下一曲
|
* 播放下一曲
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package com.yutou.nas_music_player.services;
|
package com.yutou.nas_music_player.services;
|
||||||
|
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
import android.media.AudioManager;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
@ -119,21 +121,26 @@ public class MusicService extends MediaBrowserServiceCompat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class MediaSessionCallback extends MediaSessionCompat.Callback {
|
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();
|
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat();
|
||||||
mediaSession.setMetadata(metadataCompat);
|
mediaSession.setMetadata(metadataCompat);
|
||||||
mediaSession.setActive(true);
|
mediaSession.setActive(true);
|
||||||
Notification notification =
|
Notification notification =
|
||||||
notificationManager.getNotification(
|
notificationManager.getNotification(
|
||||||
metadataCompat, builderState(0).build(), getSessionToken());
|
metadataCompat, builderState(0).build(), getSessionToken());
|
||||||
notificationManager.getManager()
|
notificationManager.getManager()
|
||||||
.notify(MediaNotificationManager.NOTIFICATION_ID, notification);
|
.notify(MediaNotificationManager.NOTIFICATION_ID, notification);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPlay() {
|
public void onPlay() {
|
||||||
super.onPlay();
|
super.onPlay();
|
||||||
System.out.println("接收到播放指令");
|
System.out.println("接收到播放指令");
|
||||||
mState=PlaybackStateCompat.STATE_PLAYING;
|
registerReceiver(receiver, intentFilter);
|
||||||
|
mState = PlaybackStateCompat.STATE_PLAYING;
|
||||||
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat();
|
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat();
|
||||||
mediaSession.setMetadata(metadataCompat);
|
mediaSession.setMetadata(metadataCompat);
|
||||||
mediaSession.setActive(true);
|
mediaSession.setActive(true);
|
||||||
@ -145,7 +152,7 @@ public class MusicService extends MediaBrowserServiceCompat {
|
|||||||
@Override
|
@Override
|
||||||
public void onPause() {
|
public void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
mState=PlaybackStateCompat.STATE_PAUSED;
|
mState = PlaybackStateCompat.STATE_PAUSED;
|
||||||
MusicContainer.getInstance().pause();
|
MusicContainer.getInstance().pause();
|
||||||
//MusicService.this.onSeekTo(-1);
|
//MusicService.this.onSeekTo(-1);
|
||||||
}
|
}
|
||||||
@ -176,8 +183,9 @@ public class MusicService extends MediaBrowserServiceCompat {
|
|||||||
@Override
|
@Override
|
||||||
public void onStop() {
|
public void onStop() {
|
||||||
super.onStop();
|
super.onStop();
|
||||||
mState=PlaybackStateCompat.STATE_STOPPED;
|
mState = PlaybackStateCompat.STATE_STOPPED;
|
||||||
MusicContainer.getInstance().getMediaPlayer().stop();
|
MusicContainer.getInstance().getMediaPlayer().stop();
|
||||||
|
unregisterReceiver(receiver);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user