重构图片加载代码

修复通知栏响应不及时问题
This commit is contained in:
zlzw 2022-10-25 14:56:57 +08:00
parent d7d5fcf065
commit 554d9406f1
15 changed files with 243 additions and 294 deletions

View File

@ -19,7 +19,7 @@ android {
minSdkVersion 23 minSdkVersion 23
targetSdkVersion 32 targetSdkVersion 32
versionCode 1 versionCode 1
versionName "1.2" versionName "1.3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }
@ -71,7 +71,6 @@ dependencies {
implementation 'com.kaopiz:kprogresshud:1.2.0' implementation 'com.kaopiz:kprogresshud:1.2.0'
implementation 'com.github.promeg:tinypinyin:2.0.3' implementation 'com.github.promeg:tinypinyin:2.0.3'
implementation 'com.github.myinnos:AlphabetIndex-Fast-Scroll-RecyclerView:1.0.95' implementation 'com.github.myinnos:AlphabetIndex-Fast-Scroll-RecyclerView:1.0.95'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
implementation "androidx.room:room-runtime:2.4.3" implementation "androidx.room:room-runtime:2.4.3"

View File

@ -23,6 +23,7 @@ import com.yutou.nas_music_player.R;
import com.yutou.nas_music_player.containers.MediaBrowserHelper; import com.yutou.nas_music_player.containers.MediaBrowserHelper;
import com.yutou.nas_music_player.containers.MusicContainer; import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.tools.AppData; import com.yutou.nas_music_player.tools.AppData;
import com.yutou.nas_music_player.tools.ImageLoader;
import java.util.List; import java.util.List;
@ -70,36 +71,8 @@ public class AlbumsRecyclerAdapter extends RecyclerView.Adapter<AlbumsRecyclerAd
holder.artist.setVisibility(View.VISIBLE); holder.artist.setVisibility(View.VISIBLE);
// holder.icon.setImageBitmap(ThumbnailUtils.extractThumbnail(data.getImg(), 64,64)); // holder.icon.setImageBitmap(ThumbnailUtils.extractThumbnail(data.getImg(), 64,64));
if (holder.icon.getTag() == null) { if (holder.icon.getTag() == null) {
data.getImg(AppData.minImageWidth, AppData.minImageHigh, new NetworkInterface() { ImageLoader.getInstance(context).showImage(holder.icon,data.getImageUrl(),AppData.minImageWidth,AppData.minImageHigh,true);
@Override
public void httpGetData(Object data, int state) {
Glide.with(context).load((Bitmap) data)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(holder.icon);
}
@Override
public void httpError(Exception e) {
}
});
} }
holder.icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.getImg(AppData.minImageWidth,AppData.minImageHigh,new DefaultNetworkInterface(){
@Override
public void httpGetData(Object data, int state) {
super.httpGetData(data, state);
Glide.with(context).load(data)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(holder.icon);
}
});
}
});
} else { } else {
holder.message.setText(""); holder.message.setText("");
holder.play_mask.setVisibility(View.GONE); holder.play_mask.setVisibility(View.GONE);

View File

@ -22,6 +22,7 @@ import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.Interfaces.AbsObjectInterface;
import com.yutou.nas_music_player.Interfaces.DefaultNetworkInterface; import com.yutou.nas_music_player.Interfaces.DefaultNetworkInterface;
import com.yutou.nas_music_player.Interfaces.NetworkInterface; import com.yutou.nas_music_player.Interfaces.NetworkInterface;
import com.yutou.nas_music_player.MyApplication; import com.yutou.nas_music_player.MyApplication;
@ -31,6 +32,7 @@ import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.network.HttpManager; import com.yutou.nas_music_player.network.HttpManager;
import com.yutou.nas_music_player.tools.AppData; import com.yutou.nas_music_player.tools.AppData;
import com.yutou.nas_music_player.tools.AppTools; import com.yutou.nas_music_player.tools.AppTools;
import com.yutou.nas_music_player.tools.ImageLoader;
import com.yutou.nas_music_player.tools.StringUtil; import com.yutou.nas_music_player.tools.StringUtil;
import com.yutou.nas_music_player.views.AlbumsActivity; import com.yutou.nas_music_player.views.AlbumsActivity;
@ -334,17 +336,18 @@ public class MusicLibsAdapter extends RecyclerView.Adapter<MusicLibsAdapter.View
System.out.println("点击item"); System.out.println("点击item");
if (data == null) if (data == null)
return; return;
data.getImg(AppData.imageWidth, AppData.imageHigh,new DefaultNetworkInterface(){ ImageLoader.getInstance(context).getImageBitmap(data.getImageUrl(), AppData.imageWidth, AppData.imageHigh, new AbsObjectInterface<Bitmap>() {
@Override @Override
public void httpGetData(Object bitmap, int state) { public void onData(Bitmap bitmap) {
super.httpGetData(data, state); if(bitmap!=null){
Intent intent = new Intent(itemView.getContext(), AlbumsActivity.class); Intent intent = new Intent(itemView.getContext(), AlbumsActivity.class);
intent.putExtra("model", 0); intent.putExtra("model", 0);
intent.putExtra("album", data.getAlbum()); intent.putExtra("album", data.getAlbum());
intent.putExtra("image", HttpManager.NetworkAPI.HOME+data.getImageUrl()); intent.putExtra("image", data.getImageUrl());
intent.putExtra("color", AppTools.getImageColor((Bitmap) bitmap)); intent.putExtra("color", AppTools.getImageColor(bitmap));
if (clickAction != null) { if (clickAction != null) {
clickAction.onClick(intent, icon); clickAction.onClick(intent, icon);
}
} }
} }
}); });
@ -397,22 +400,6 @@ public class MusicLibsAdapter extends RecyclerView.Adapter<MusicLibsAdapter.View
// icon.setImageBitmap(ThumbnailUtils.extractThumbnail(data.getImg(), 64,64)); // icon.setImageBitmap(ThumbnailUtils.extractThumbnail(data.getImg(), 64,64));
showImage(data, this); showImage(data, this);
icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.getImg(AppData.minImageWidth, AppData.minImageHigh,new DefaultNetworkInterface(){
@Override
public void httpGetData(Object bitmap, int state) {
super.httpGetData(data, state);
Glide.with(MyApplication.application).load(bitmap)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(icon);
icon.setTag("img");
}
});
}
});
} else { } else {
message.setText(""); message.setText("");
play_mask.setVisibility(View.GONE); play_mask.setVisibility(View.GONE);
@ -487,22 +474,7 @@ public class MusicLibsAdapter extends RecyclerView.Adapter<MusicLibsAdapter.View
protected void showImage(MusicData musicData, ViewHolder holder) { protected void showImage(MusicData musicData, ViewHolder holder) {
musicData.getImg(AppData.minImageWidth, AppData.minImageHigh, new NetworkInterface() { ImageLoader.getInstance(context).showImage(holder.icon,musicData.getImageUrl(),AppData.minImageWidth, AppData.minImageHigh,true);
@Override
public void httpGetData(Object data, int state) {
Glide.with(MyApplication.application).load((Bitmap) data)
.placeholder(holder.icon.getDrawable())
.dontAnimate()
.skipMemoryCache(false)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(holder.icon);
}
@Override
public void httpError(Exception e) {
System.out.println("图片下载失败");
}
});
} }
PopupWindow popupWindow = null; PopupWindow popupWindow = null;

View File

@ -2,25 +2,8 @@ package com.yutou.nas_music_player.Datas;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.view.View; import android.view.View;
import com.alibaba.fastjson.JSONObject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.yutou.nas_music_player.Interfaces.DefaultNetworkInterface;
import com.yutou.nas_music_player.Interfaces.DownloadInterface;
import com.yutou.nas_music_player.Interfaces.NetworkInterface;
import com.yutou.nas_music_player.MyApplication;
import com.yutou.nas_music_player.R;
import com.yutou.nas_music_player.network.HttpManager;
import com.yutou.nas_music_player.tools.AppData;
import com.yutou.nas_music_player.tools.AppTools;
import com.yutou.nas_music_player.tools.NetworkTool;
import com.yutou.nas_music_player.tools.StringUtil;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.room.ColumnInfo; import androidx.room.ColumnInfo;
@ -28,7 +11,11 @@ import androidx.room.Entity;
import androidx.room.Ignore; import androidx.room.Ignore;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
import java.io.File; import com.yutou.nas_music_player.Interfaces.NetworkInterface;
import com.yutou.nas_music_player.MyApplication;
import com.yutou.nas_music_player.network.HttpManager;
import com.yutou.nas_music_player.tools.AppTools;
import com.yutou.nas_music_player.tools.StringUtil;
@Entity @Entity
public class MusicData { public class MusicData {
@ -227,7 +214,7 @@ public class MusicData {
public String getPlayUrl() { public String getPlayUrl() {
String url = HttpManager.NetworkAPI.HOME + HttpManager.NetworkAPI.MUSIC_PLAY String url =HttpManager.NetworkAPI.MUSIC_PLAY
+ "?random=false&token=" + HttpManager.NetworkAPI.HTTP_KEY + "&filePath=" + getMd5(); + "?random=false&token=" + HttpManager.NetworkAPI.HTTP_KEY + "&filePath=" + getMd5();
System.out.println("播放地址:" + url); System.out.println("播放地址:" + url);
return url; return url;
@ -239,41 +226,6 @@ public class MusicData {
} }
public Bitmap getImg() {
return bitmap;
}
public Bitmap getImg(int width, int height,@NonNull NetworkInterface networkInterface) {
if(bitmap!=null) {
networkInterface.httpGetData(bitmap,0);
return bitmap;
}
if(width==-1||height==-1){
AppTools.initImageLoader(MyApplication.application)
.loadImage(HttpManager.NetworkAPI.HOME+getImageUrl(),new SimpleImageLoadingListener(){
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
networkInterface.httpGetData(loadedImage,0);
bitmap=loadedImage;
}
});
}else{
System.out.println(HttpManager.NetworkAPI.HOME+getImageUrl());
AppTools.initImageLoader(MyApplication.application)
.loadImage(HttpManager.NetworkAPI.HOME+getImageUrl(),new ImageSize(width,height),new SimpleImageLoadingListener(){
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
networkInterface.httpGetData(loadedImage,0);
bitmap=loadedImage;
}
});
}
return bitmap;
}
public int getBitRate() { public int getBitRate() {
return bitRate; return bitRate;

View File

@ -0,0 +1,5 @@
package com.yutou.nas_music_player.Interfaces;
public abstract class AbsObjectInterface<T> {
public abstract void onData(T t);
}

View File

@ -9,6 +9,7 @@ import android.os.Bundle;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.yutou.nas_music_player.tools.ImageLoader;
import com.yutou.nas_music_player.views.OpenActivity; import com.yutou.nas_music_player.views.OpenActivity;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
@ -39,6 +40,7 @@ public class MyApplication extends Application {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
MyApplication.application = this; MyApplication.application = this;
ImageLoader.getInstance(this);
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override @Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) { public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

View File

@ -10,6 +10,7 @@ import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat; import android.support.v4.media.session.PlaybackStateCompat;
import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.Interfaces.AbsObjectInterface;
import com.yutou.nas_music_player.MyApplication; import com.yutou.nas_music_player.MyApplication;
import com.yutou.nas_music_player.services.MusicService; import com.yutou.nas_music_player.services.MusicService;
import com.yutou.nas_music_player.tools.AppData; import com.yutou.nas_music_player.tools.AppData;
@ -70,7 +71,13 @@ public class MediaBrowserHelper {
public void run() { public void run() {
for (MediaControllerCompat.Callback callback : callbacks) { for (MediaControllerCompat.Callback callback : callbacks) {
if (callback != null) { if (callback != null) {
callback.onMetadataChanged(MusicContainer.getInstance().getNowPlayMetadataCompat()); MusicContainer.getInstance().getNowPlayMetadataCompat(new AbsObjectInterface<MediaMetadataCompat>() {
@Override
public void onData(MediaMetadataCompat mediaMetadataCompat) {
callback.onMetadataChanged(mediaMetadataCompat);
}
});
} }
} }

View File

@ -4,6 +4,7 @@ import static android.media.MediaPlayer.SEEK_PREVIOUS_SYNC;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
@ -19,12 +20,14 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.Datas.PreviousPlayerList; import com.yutou.nas_music_player.Datas.PreviousPlayerList;
import com.yutou.nas_music_player.Interfaces.AbsObjectInterface;
import com.yutou.nas_music_player.Interfaces.NetworkInterface; import com.yutou.nas_music_player.Interfaces.NetworkInterface;
import com.yutou.nas_music_player.MyApplication; import com.yutou.nas_music_player.MyApplication;
import com.yutou.nas_music_player.network.HttpManager; import com.yutou.nas_music_player.network.HttpManager;
import com.yutou.nas_music_player.tools.AppData; import com.yutou.nas_music_player.tools.AppData;
import com.yutou.nas_music_player.tools.AppTools; import com.yutou.nas_music_player.tools.AppTools;
import com.yutou.nas_music_player.tools.ConfigTools; import com.yutou.nas_music_player.tools.ConfigTools;
import com.yutou.nas_music_player.tools.ImageLoader;
import com.yutou.nas_music_player.tools.StringUtil; import com.yutou.nas_music_player.tools.StringUtil;
import java.io.IOException; import java.io.IOException;
@ -267,7 +270,7 @@ public class MusicContainer {
ConfigTools.getPreferences().edit().putString(ConfigTools.previous_music, JSONObject.toJSONString(tmp)).apply(); ConfigTools.getPreferences().edit().putString(ConfigTools.previous_music, JSONObject.toJSONString(tmp)).apply();
MediaNotificationManager manager = MediaNotificationManager.getInstance(); MediaNotificationManager manager = MediaNotificationManager.getInstance();
if (manager != null) { if (manager != null) {
// manager.getService().updateNotification(); // manager.getService().updateNotification();
} }
tmp = null; tmp = null;
} catch (IOException e) { } catch (IOException e) {
@ -288,11 +291,12 @@ public class MusicContainer {
private Timer playTimer; private Timer playTimer;
public MediaMetadataCompat getNowPlayMetadataCompat() { public void getNowPlayMetadataCompat(AbsObjectInterface<MediaMetadataCompat> objectInterface) {
if (nowPlayData == null) { if (nowPlayData == null) {
return null; objectInterface.onData(null);
} else {
getMetadataCompat(nowPlayData, objectInterface);
} }
return getMetadataCompat(nowPlayData);
} }
public Long getYear(String year) { public Long getYear(String year) {
@ -313,27 +317,33 @@ public class MusicContainer {
private MediaMetadataCompat metadataCompat; private MediaMetadataCompat metadataCompat;
private String metaDataFile = ""; private String metaDataFile = "";
public MediaMetadataCompat getMetadataCompat(MusicData data) { public void getMetadataCompat(MusicData data, AbsObjectInterface<MediaMetadataCompat> objectInterface) {
if (data.getMd5().equals(metaDataFile)) { if (data.getMd5().equals(metaDataFile)) {
return metadataCompat; objectInterface.onData(metadataCompat);
return;
} }
metaDataFile = data.getMd5(); metaDataFile = data.getMd5();
MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(); ImageLoader.getInstance().getImageBitmap(data.getImageUrl(), -1, -1, new AbsObjectInterface<Bitmap>() {
builder.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, data.getTitle()); @Override
builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, data.getArtist()); public void onData(Bitmap bitmap) {
builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, data.getTitle()); MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, data.getAlbum()); builder.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, data.getTitle());
builder.putString(MediaMetadataCompat.METADATA_KEY_COMPOSER, data.getComposer()); builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, data.getArtist());
builder.putLong(MediaMetadataCompat.METADATA_KEY_YEAR, getYear(data.getYear())); builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, data.getTitle());
builder.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, Long.parseLong(data.getTrack())); builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, data.getAlbum());
builder.putLong(MediaMetadataCompat.METADATA_KEY_DISC_NUMBER, Long.parseLong(data.getDisc_no())); builder.putString(MediaMetadataCompat.METADATA_KEY_COMPOSER, data.getComposer());
builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, ((int) (data.getDurationAsDouble() * 1000))); builder.putLong(MediaMetadataCompat.METADATA_KEY_YEAR, getYear(data.getYear()));
if (data.getImg() != null) { builder.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, Long.parseLong(data.getTrack()));
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, data.getImg()); builder.putLong(MediaMetadataCompat.METADATA_KEY_DISC_NUMBER, Long.parseLong(data.getDisc_no()));
} builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, ((int) (data.getDurationAsDouble() * 1000)));
builder.putString("md5", data.getMd5()); if (bitmap != null) {
metadataCompat = builder.build(); builder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, bitmap);
return metadataCompat; }
builder.putString("md5", data.getMd5());
metadataCompat = builder.build();
objectInterface.onData(metadataCompat);
}
});
} }
public static MusicLibs getLibs() { public static MusicLibs getLibs() {
@ -346,8 +356,9 @@ public class MusicContainer {
isPause = true; isPause = true;
mediaPlayer.pause(); mediaPlayer.pause();
} }
public void play(){
isPause=false; public void play() {
isPause = false;
mediaPlayer.start(); mediaPlayer.start();
} }
@ -393,8 +404,8 @@ public class MusicContainer {
*/ */
public void playPrevious() { public void playPrevious() {
playIndex--; playIndex--;
if(playIndex<0){ if (playIndex < 0) {
playIndex=0; playIndex = 0;
} }
MusicData data = previousPlayerList.getMusicData(); MusicData data = previousPlayerList.getMusicData();
if (data != null) { if (data != null) {

View File

@ -44,7 +44,7 @@ public class HttpManager {
public static class NetworkAPI { public static class NetworkAPI {
public static final String HTTP_KEY = "OHreoKn2CSlDipWV6phEupAqPIX2LJFl"; public static final String HTTP_KEY = "OHreoKn2CSlDipWV6phEupAqPIX2LJFl";
public static String HOME = ""; public static String HOME = "http://nas.cnmglz.com:8000/nas";
public static String MUSIC_LIST = HOME + "/music/list.do"; public static String MUSIC_LIST = HOME + "/music/list.do";
public static String MUSIC_Album = HOME + "/music/getAlbum.do"; public static String MUSIC_Album = HOME + "/music/getAlbum.do";
public static String MUSIC_Collection_Dir_List = HOME + "/music/favorite/dir/list.do"; public static String MUSIC_Collection_Dir_List = HOME + "/music/favorite/dir/list.do";
@ -64,7 +64,7 @@ public class HttpManager {
private HttpManager() { private HttpManager() {
System.out.println("初始化NetWork"); System.out.println("初始化NetWork");
httpGet("http://192.168.31.88:8000/public/version.do", new NetworkInterface() { /*httpGet("http://192.168.31.88:8000/public/version.do", new NetworkInterface() {
@Override @Override
public void httpGetData(Object data, int state) { public void httpGetData(Object data, int state) {
System.out.println("state = " + state); System.out.println("state = " + state);
@ -102,7 +102,7 @@ public class HttpManager {
} }
}); });
} }
}); });*/
} }
public static HttpManager getInstance() { public static HttpManager getInstance() {

View File

@ -19,6 +19,7 @@ import androidx.annotation.Nullable;
import androidx.media.MediaBrowserServiceCompat; import androidx.media.MediaBrowserServiceCompat;
import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.Interfaces.AbsObjectInterface;
import com.yutou.nas_music_player.containers.MediaBrowserHelper; 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;
@ -94,10 +95,15 @@ public class MusicService extends MediaBrowserServiceCompat {
} }
mState = state.getState(); mState = state.getState();
MusicData data = MusicContainer.getInstance().getNowPlayData(); MusicData data = MusicContainer.getInstance().getNowPlayData();
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getMetadataCompat(data); MusicContainer.getInstance().getMetadataCompat(data, new AbsObjectInterface<MediaMetadataCompat>() {
Notification notification = notificationManager.getNotification(metadataCompat, builderState(state.getPosition()).build(), getSessionToken()); @Override
notificationManager.getManager() public void onData(MediaMetadataCompat mediaMetadataCompat) {
.notify(MediaNotificationManager.NOTIFICATION_ID, notification); Notification notification = notificationManager.getNotification(mediaMetadataCompat, builderState(state.getPosition()).build(), getSessionToken());
notificationManager.getManager()
.notify(MediaNotificationManager.NOTIFICATION_ID, notification);
}
});
} }
}); });
System.out.println("注册receiver"); System.out.println("注册receiver");
@ -119,25 +125,35 @@ public class MusicService extends MediaBrowserServiceCompat {
@Override @Override
public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) { public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>(); List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();
MediaMetadataCompat compat = MusicContainer.getInstance().getNowPlayMetadataCompat(); MusicContainer.getInstance().getNowPlayMetadataCompat(new AbsObjectInterface<MediaMetadataCompat>() {
System.out.println(compat); @Override
if (compat != null) { public void onData(MediaMetadataCompat compat) {
MediaBrowserCompat.MediaItem mediaItem = new MediaBrowserCompat.MediaItem(compat.getDescription(), MediaBrowserCompat.MediaItem.FLAG_PLAYABLE); System.out.println(compat);
mediaItems.add(mediaItem); if (compat != null) {
} MediaBrowserCompat.MediaItem mediaItem = new MediaBrowserCompat.MediaItem(compat.getDescription(), MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);
result.sendResult(mediaItems); mediaItems.add(mediaItem);
}
result.sendResult(mediaItems);
}
});
} }
public void seekToNotification(long pos) { public void seekToNotification(long pos) {
MusicData data = MusicContainer.getInstance().getNowPlayData(); MusicData data = MusicContainer.getInstance().getNowPlayData();
if (!playMd5.equals(data.getMd5())) { if (!playMd5.equals(data.getMd5())) {
playMd5 = data.getMd5(); playMd5 = data.getMd5();
mediaSession.setMetadata(MusicContainer.getInstance().getNowPlayMetadataCompat()); MusicContainer.getInstance().getNowPlayMetadataCompat(new AbsObjectInterface<MediaMetadataCompat>() {
updateNotification(); @Override
public void onData(MediaMetadataCompat mediaMetadataCompat) {
mediaSession.setMetadata(mediaMetadataCompat);
updateNotification();
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
.setActions(MEDIA_SESSION_ACTIONS)
.setState(mState, pos, 1.0f, SystemClock.elapsedRealtime()).build());
}
});
} }
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
.setActions(MEDIA_SESSION_ACTIONS)
.setState(mState, pos, 1.0f, SystemClock.elapsedRealtime()).build());
} }
public PlaybackStateCompat.Builder builderState(long position) { public PlaybackStateCompat.Builder builderState(long position) {
@ -180,15 +196,20 @@ public class MusicService extends MediaBrowserServiceCompat {
public void updateNotification() { public void updateNotification() {
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat(); MusicContainer.getInstance().getNowPlayMetadataCompat(new AbsObjectInterface<MediaMetadataCompat>() {
System.out.println("更新通知栏:" + metadataCompat.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID)); @Override
//mediaSession.setMetadata(metadataCompat); public void onData(MediaMetadataCompat metadataCompat) {
mediaSession.setActive(true); System.out.println("更新通知栏:" + metadataCompat.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID));
Notification notification = //mediaSession.setMetadata(metadataCompat);
notificationManager.getNotification( mediaSession.setActive(true);
metadataCompat, builderState(0).build(), getSessionToken()); Notification notification =
notificationManager.getManager() notificationManager.getNotification(
.notify(MediaNotificationManager.NOTIFICATION_ID, notification); metadataCompat, builderState(0).build(), getSessionToken());
notificationManager.getManager()
.notify(MediaNotificationManager.NOTIFICATION_ID, notification);
}
});
} }
public class MediaSessionCallback extends MediaSessionCompat.Callback { public class MediaSessionCallback extends MediaSessionCompat.Callback {
@ -203,12 +224,17 @@ public class MusicService extends MediaBrowserServiceCompat {
isRegReceiver = true; isRegReceiver = true;
} }
mState = PlaybackStateCompat.STATE_PLAYING; mState = PlaybackStateCompat.STATE_PLAYING;
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat(); MusicContainer.getInstance().getNowPlayMetadataCompat(new AbsObjectInterface<MediaMetadataCompat>() {
mediaSession.setMetadata(metadataCompat); @Override
mediaSession.setActive(true); public void onData(MediaMetadataCompat metadataCompat) {
Notification notification = notificationManager.getNotification(metadataCompat, builderState(0).build(), getSessionToken()); mediaSession.setMetadata(metadataCompat);
startForeground(MediaNotificationManager.NOTIFICATION_ID, notification); mediaSession.setActive(true);
MusicContainer.getInstance().play(MusicContainer.getInstance().getNowPlayData()); Notification notification = notificationManager.getNotification(metadataCompat, builderState(0).build(), getSessionToken());
startForeground(MediaNotificationManager.NOTIFICATION_ID, notification);
MusicContainer.getInstance().play(MusicContainer.getInstance().getNowPlayData());
}
});
} }
@Override @Override

View File

@ -11,10 +11,6 @@ import android.widget.Toast;
import androidx.palette.graphics.Palette; import androidx.palette.graphics.Palette;
import com.kaopiz.kprogresshud.KProgressHUD; import com.kaopiz.kprogresshud.KProgressHUD;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.yutou.nas_music_player.MyApplication; import com.yutou.nas_music_player.MyApplication;
import com.yutou.nas_music_player.R; import com.yutou.nas_music_player.R;
@ -104,32 +100,6 @@ public class AppTools {
return stringBuilder.toString(); return stringBuilder.toString();
} }
private static DisplayImageOptions initDisplayOptions() {
DisplayImageOptions.Builder displayImageOptionsBuilder = new DisplayImageOptions.Builder();
displayImageOptionsBuilder.cacheInMemory(false);
displayImageOptionsBuilder.cacheOnDisk(true);
displayImageOptionsBuilder.showImageForEmptyUri(R.drawable.ic_launcher_background);
displayImageOptionsBuilder.showImageOnFail(R.mipmap.ic_launcher);
return displayImageOptionsBuilder.build();
}
public static ImageLoader initImageLoader(Context context) {
if(imageLoader!=null){
return imageLoader;
}
imageLoader = ImageLoader.getInstance();
ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(context);
File cachePath = new File(context.getFilesDir().getAbsolutePath() + "/cache/");
if (!cachePath.exists()) {
cachePath.exists();
}
builder.diskCache(new UnlimitedDiskCache(cachePath));
builder.defaultDisplayImageOptions(initDisplayOptions());
imageLoader.init(builder.build());
return imageLoader;
}
public static String getUrlBase64(String text) { public static String getUrlBase64(String text) {
return new String(Base64.encode(text.getBytes(), Base64.DEFAULT)).replace("\n", ""); return new String(Base64.encode(text.getBytes(), Base64.DEFAULT)).replace("\n", "");

View File

@ -0,0 +1,94 @@
package com.yutou.nas_music_player.tools;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.yutou.nas_music_player.Interfaces.AbsObjectInterface;
import java.util.concurrent.ExecutionException;
import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
public class ImageLoader {
private static ImageLoader manager;
private Context context;
private ImageLoader(Context context) {
this.context = context;
}
public static ImageLoader getInstance(Context context) {
if (manager == null) {
manager = new ImageLoader(context);
}
return manager;
}
public static ImageLoader getInstance() {
return manager;
}
public void showImage(ImageView imageView, String url, int imageWidth, int imageHigh, boolean isRounded, boolean isBlurTransformation) {
Log.w("图片地址", "url: " + url);
RequestBuilder<Drawable> builder = Glide.with(context)
.load(url)
.placeholder(imageView.getDrawable());
if (imageWidth != -1 && imageHigh != -1) {
builder = builder.override(imageWidth, imageHigh);
}
if (isRounded) {
builder = builder.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)));
}
if (isBlurTransformation) {
builder = builder.apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3)));
}
builder.into(imageView);
}
public void showImage(ImageView imageView, String url) {
showImage(imageView, url, -1, -1, false, false);
}
public void showImage(ImageView imageView, String url, int imageWidth, int imageHigh) {
showImage(imageView, url, imageWidth, imageHigh, false, false);
}
public void showImage(ImageView imageView, String url, int imageWidth, int imageHigh, boolean isRounded) {
showImage(imageView, url, imageWidth, imageHigh, isRounded, false);
}
public void getImageBitmap(String url, int imageWidth, int imageHigh, AbsObjectInterface<Bitmap> objectInterface) {
RequestBuilder<Bitmap> builder = Glide.with(context)
.asBitmap()
.load(url);
if (imageWidth != -1 && imageHigh != -1) {
builder = builder.override(imageWidth, imageHigh);
}
builder.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
objectInterface.onData(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
}

View File

@ -19,7 +19,6 @@ import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.yutou.nas_music_player.Adapters.AlbumsRecyclerAdapter; import com.yutou.nas_music_player.Adapters.AlbumsRecyclerAdapter;
import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.Interfaces.NetworkInterface; import com.yutou.nas_music_player.Interfaces.NetworkInterface;
@ -27,7 +26,7 @@ import com.yutou.nas_music_player.R;
import com.yutou.nas_music_player.containers.MediaBrowserHelper; import com.yutou.nas_music_player.containers.MediaBrowserHelper;
import com.yutou.nas_music_player.containers.MusicContainer; import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.tools.AppData; import com.yutou.nas_music_player.tools.AppData;
import com.yutou.nas_music_player.tools.AppTools; import com.yutou.nas_music_player.tools.ImageLoader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -64,18 +63,8 @@ public class AlbumsActivity extends AppCompatActivity {
} }
String imageUrl = getIntent().getStringExtra("image"); String imageUrl = getIntent().getStringExtra("image");
if (imageUrl != null) { if (imageUrl != null) {
AppTools.initImageLoader(this).displayImage(imageUrl, album_image); ImageLoader.getInstance(this).showImage(album_image, imageUrl, -1, -1, true);
AppTools.initImageLoader(this) ImageLoader.getInstance(this).showImage(image_background, imageUrl, -1, -1, false,true);
.loadImage(imageUrl, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
Glide.with(AlbumsActivity.this).load(loadedImage)
.apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3)))
.into(image_background);
}
});
} else { } else {
album_image.setImageResource(R.mipmap.ic_launcher); album_image.setImageResource(R.mipmap.ic_launcher);
Glide.with(AlbumsActivity.this).load(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) Glide.with(AlbumsActivity.this).load(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
@ -105,20 +94,7 @@ public class AlbumsActivity extends AppCompatActivity {
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
if (list.isEmpty()) if (list.isEmpty())
return; return;
list.get(0).getImg(AppData.imageWidth, AppData.imageHigh, new NetworkInterface() { ImageLoader.getInstance(AlbumsActivity.this).showImage(image_background, list.get(0).getImageUrl(), AppData.imageWidth, AppData.imageHigh, true);
@Override
public void httpGetData(Object data, int state) {
album_image.setImageBitmap((Bitmap) data);
Glide.with(AlbumsActivity.this).load(data)
.apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3)))
.into(image_background);
}
@Override
public void httpError(Exception e) {
}
});
} }
@Override @Override

View File

@ -42,6 +42,7 @@ import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.tools.AppData; import com.yutou.nas_music_player.tools.AppData;
import com.yutou.nas_music_player.tools.AppTools; import com.yutou.nas_music_player.tools.AppTools;
import com.yutou.nas_music_player.tools.ConfigTools; import com.yutou.nas_music_player.tools.ConfigTools;
import com.yutou.nas_music_player.tools.ImageLoader;
import com.yutou.popdialog.POPDialog; import com.yutou.popdialog.POPDialog;
import java.util.List; import java.util.List;
@ -112,7 +113,7 @@ public class MainActivity extends AppCompatActivity {
} }
setPlayModelButton(model + 1, true); setPlayModelButton(model + 1, true);
}); });
playListener=new PlayListener(); playListener = new PlayListener();
browserHelper = MediaBrowserHelper.getInstance(); browserHelper = MediaBrowserHelper.getInstance();
browserHelper.regPlayListener(playListener); browserHelper.regPlayListener(playListener);
@ -197,28 +198,8 @@ public class MainActivity extends AppCompatActivity {
private void setImage(MusicData data) { private void setImage(MusicData data) {
// setBarColor(data.getImageColor()); // setBarColor(data.getImageColor());
data.getImg(AppData.imageWidth, AppData.imageHigh,new DefaultNetworkInterface(){ ImageLoader.getInstance(this).showImage(album_image, data.getImageUrl(), AppData.imageWidth, AppData.imageHigh, true);
@Override ImageLoader.getInstance(this).showImage(background_image, data.getImageUrl(),AppData.imageWidth, AppData.imageHigh, false, true);
public void httpGetData(Object bitmap, int state) {
super.httpGetData(data, state);
Glide.with(MainActivity.this).load(bitmap)
.placeholder(album_image.getDrawable())
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(album_image);
Glide.with(MainActivity.this).load(bitmap)
.placeholder(background_image.getDrawable())
.apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3)))
.into(background_image);
}
});
/* data.getImg(AppData.minImageWidth, AppData.minImageHigh,new DefaultNetworkInterface(){
@Override
public void httpGetData(Object bitmap, int state) {
super.httpGetData(data, state);
}
});*/
} }
private void initView() { private void initView() {
@ -315,8 +296,8 @@ public class MainActivity extends AppCompatActivity {
List<MusicData> playList = MusicContainer.getInstance().getPlayList(); List<MusicData> playList = MusicContainer.getInstance().getPlayList();
POPDialog dialog = new POPDialog(this); POPDialog dialog = new POPDialog(this);
RecyclerView recyclerView = null; RecyclerView recyclerView = null;
Button clear=new Button(this); Button clear = new Button(this);
LinearLayout layout=new LinearLayout(this); LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL); layout.setOrientation(LinearLayout.VERTICAL);
clear.setText("清空列表"); clear.setText("清空列表");
dialog.enableExitButton(true); dialog.enableExitButton(true);
@ -326,13 +307,13 @@ public class MainActivity extends AppCompatActivity {
} else { } else {
dialog.setTitle("播放列表:" + playList.size()); dialog.setTitle("播放列表:" + playList.size());
MusicLibsAdapter adapter = new MusicLibsAdapter(this); MusicLibsAdapter adapter = new MusicLibsAdapter(this);
// ItemTouchHelper touchHelper=new ItemTouchHelper(new MusicItemTouchAdapterCallback()); // ItemTouchHelper touchHelper=new ItemTouchHelper(new MusicItemTouchAdapterCallback());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
recyclerView = new RecyclerView(this); recyclerView = new RecyclerView(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setLayoutParams(params); recyclerView.setLayoutParams(params);
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
// touchHelper.attachToRecyclerView(recyclerView); // touchHelper.attachToRecyclerView(recyclerView);
adapter.setData(playList); adapter.setData(playList);
adapter.setBrowserHelper(browserHelper); adapter.setBrowserHelper(browserHelper);
clear.setOnClickListener(view -> { clear.setOnClickListener(view -> {

View File

@ -31,6 +31,7 @@ import com.yutou.nas_music_player.R;
import com.yutou.nas_music_player.containers.MediaBrowserHelper; import com.yutou.nas_music_player.containers.MediaBrowserHelper;
import com.yutou.nas_music_player.containers.MusicContainer; import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.tools.AppData; import com.yutou.nas_music_player.tools.AppData;
import com.yutou.nas_music_player.tools.ImageLoader;
import com.yutou.nas_music_player.views.Fragments.MusicLibsFragment; import com.yutou.nas_music_player.views.Fragments.MusicLibsFragment;
import java.util.ArrayList; import java.util.ArrayList;
@ -177,12 +178,6 @@ public class PlayLibsActivity extends AppCompatActivity {
} }
private void setImage(Bitmap data) {
Glide.with(this).load(data)
.placeholder(album_image.getDrawable())
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(album_image);
}
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private void setPlayData(final MusicData data) { private void setPlayData(final MusicData data) {
@ -192,17 +187,7 @@ public class PlayLibsActivity extends AppCompatActivity {
artist.setText(data.getArtist()); artist.setText(data.getArtist());
progressBar.setMax((int) (data.getDurationAsDouble() * 1000)); progressBar.setMax((int) (data.getDurationAsDouble() * 1000));
progressBar.setProgress(bar_pos); progressBar.setProgress(bar_pos);
data.getImg(AppData.minImageWidth, AppData.minImageHigh, new NetworkInterface() { ImageLoader.getInstance(this).showImage(album_image, data.getImageUrl(), AppData.minImageWidth, AppData.minImageHigh, true);
@Override
public void httpGetData(Object data, int state) {
setImage((Bitmap) data);
}
@Override
public void httpError(Exception e) {
}
});
} }
@Override @Override
@ -254,9 +239,5 @@ public class PlayLibsActivity extends AppCompatActivity {
super.onQueueChanged(queue); super.onQueueChanged(queue);
System.out.println("不知道是啥变换了"); System.out.println("不知道是啥变换了");
} }
public void setImage() {
}
} }
} }