修复图片无法载入问题

修复通知栏一直弹的问题
This commit is contained in:
Yutousama 2022-04-22 13:57:55 +08:00
parent 20b80d380a
commit 17272ec4d3
3 changed files with 28 additions and 14 deletions

View File

@ -276,9 +276,9 @@ public class MusicData {
isDownloadImg = true; isDownloadImg = true;
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
if (StringUtil.isEmpty(getAlbum())) { if (StringUtil.isEmpty(getAlbum())) {
json.put("fileName", getFile()); json.put("fileName", getMd5());
} else { } else {
json.put("fileName", AppTools.getUrlBase64(getAlbum())); json.put("fileName", getMd5());
json.put("type", "album"); json.put("type", "album");
} }
System.out.println("下载图片:" + HttpManager.NetworkAPI.MUSIC_IMAGE + "?" + NetworkTool.toGetSplice(json)); System.out.println("下载图片:" + HttpManager.NetworkAPI.MUSIC_IMAGE + "?" + NetworkTool.toGetSplice(json));

View File

@ -10,6 +10,7 @@ import android.os.Bundle;
import android.os.SystemClock; import android.os.SystemClock;
import android.support.v4.media.MediaBrowserCompat; import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat; import android.support.v4.media.session.PlaybackStateCompat;
@ -17,6 +18,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.media.MediaBrowserServiceCompat; import androidx.media.MediaBrowserServiceCompat;
import com.yutou.nas_music_player.containers.MediaBrowserHelper;
import com.yutou.nas_music_player.containers.MediaNotificationManager; import com.yutou.nas_music_player.containers.MediaNotificationManager;
import com.yutou.nas_music_player.containers.MusicContainer; import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.Datas.MusicData;
@ -52,11 +54,24 @@ public class MusicService extends MediaBrowserServiceCompat {
.setActions(MEDIA_SESSION_ACTIONS); .setActions(MEDIA_SESSION_ACTIONS);
mediaSession.setPlaybackState(stateBuilder.build()); mediaSession.setPlaybackState(stateBuilder.build());
mediaSession.setCallback(new MediaSessionCallback()); mediaSession.setCallback(new MediaSessionCallback());
Intent intent=new Intent(this, MainActivity.class); Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi=PendingIntent.getActivity(this,233,intent,PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pi = PendingIntent.getActivity(this, 233, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mediaSession.setSessionActivity(pi); mediaSession.setSessionActivity(pi);
setSessionToken(mediaSession.getSessionToken()); setSessionToken(mediaSession.getSessionToken());
MusicContainer.getInstance().addCompletionListener(new PlayCompletionListener()); MusicContainer.getInstance().addCompletionListener(new PlayCompletionListener());
MediaBrowserHelper helper = new MediaBrowserHelper(this);
helper.regPlayListener(new MediaControllerCompat.Callback() {
@Override
public void onMetadataChanged(MediaMetadataCompat metadata) {
super.onMetadataChanged(metadata);
if(metadata==null){
return;
}
Notification notification = notificationManager.getNotification(metadata, builderState(0).build(), getSessionToken());
notificationManager.getManager()
.notify(MediaNotificationManager.NOTIFICATION_ID, notification);
}
});
} }
@Nullable @Nullable
@ -78,11 +93,7 @@ public class MusicService extends MediaBrowserServiceCompat {
} }
public void seekToNotification(long pos) { public void seekToNotification(long pos) {
MusicData data = MusicContainer.getInstance().getNowPlayData();
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getMetadataCompat(data);
Notification notification = notificationManager.getNotification(metadataCompat, builderState(pos).build(), getSessionToken());
notificationManager.getManager()
.notify(MediaNotificationManager.NOTIFICATION_ID, notification);
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder() mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
.setActions(MEDIA_SESSION_ACTIONS) .setActions(MEDIA_SESSION_ACTIONS)
.setState(mState, pos, 1.0f, SystemClock.elapsedRealtime()).build()); .setState(mState, pos, 1.0f, SystemClock.elapsedRealtime()).build());
@ -125,10 +136,11 @@ public class MusicService extends MediaBrowserServiceCompat {
} }
return actions; return actions;
} }
public void updateNotification() { public void updateNotification() {
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat(); MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat();
System.out.println("更新通知栏:"+metadataCompat.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID)); System.out.println("更新通知栏:" + metadataCompat.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID));
mediaSession.setMetadata(metadataCompat); mediaSession.setMetadata(metadataCompat);
mediaSession.setActive(true); mediaSession.setActive(true);
Notification notification = Notification notification =
@ -137,10 +149,12 @@ public class MusicService extends MediaBrowserServiceCompat {
notificationManager.getManager() notificationManager.getManager()
.notify(MediaNotificationManager.NOTIFICATION_ID, notification); .notify(MediaNotificationManager.NOTIFICATION_ID, notification);
} }
public class MediaSessionCallback extends MediaSessionCompat.Callback {
MusicReceiver receiver = new MusicReceiver();;
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);;
public class MediaSessionCallback extends MediaSessionCompat.Callback {
MusicReceiver receiver = new MusicReceiver();
;
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
;
@Override @Override

View File

@ -7,7 +7,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:7.1.1' classpath 'com.android.tools.build:gradle:7.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"