移除所有歌曲列表

随机播放改成用API而不再本地random
队列功能可用了
优化列表内存占用问题(大概)
似乎列表频繁切换有内存泄露
This commit is contained in:
yutou 2020-11-26 16:25:14 +08:00
parent 912762c72f
commit 198d092f94
9 changed files with 329 additions and 113 deletions

View File

@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -27,9 +28,11 @@ import com.yutou.nas_music_player.tools.NetworkTool;
import com.yutou.nas_music_player.tools.StringUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -46,23 +49,29 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
public static final int LIBS_MODEL_Tmp = 3;
public static final int LIBS_MODEL_PLAY_LIST = 4;
public static final int LIBS_MODEL_PLAY_Collection = 5;
private MusicLibsAdapter adapter;
List<MusicData> list;
Map<Integer, Bitmap> icons;
Map<Integer, List<MusicData>> listMap;
Map<String, Bitmap> icons;
public MusicLibsAdapter(Context context, int model) {
super(context, R.layout.layout_music_libs_item);
list = new ArrayList<>();
icons = new HashMap<>();
listMap = new HashMap<>();
adapter = this;
initData(model);
}
private int model = -1;
private String musicPath = "root";
public void initData(int model) {
this.model = model;
list.clear();
System.out.println("填充序列 设置model " + model);
System.out.println("填充序列 设置model " + model + " " + this.model + " &");
Map<String, List<MusicData>> map;
switch (model) {
case LIBS_MODEL_Album:
@ -70,6 +79,7 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
for (String title : map.keySet()) {
MusicData data = new MusicData();
data.setTitle(title);
data.setAlbum(map.get(title).get(0).getAlbum());
data.setFile(map.get(title).get(0).getFile());
data.setArtist("");
list.add(data);
@ -81,27 +91,57 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
MusicData data = new MusicData();
data.setTitle(title);
data.setFile(map.get(title).get(0).getFile());
data.setArtist("");
data.setArtist(map.get(title).get(0).getArtist());
list.add(data);
}
break;
case LIBS_MODEL_Tmp:
list = MusicContainer.getInstance().getTmpPlayList();
list.addAll(MusicContainer.getInstance().getTmpPlayList());
break;
case LIBS_MODEL_PLAY_LIST:
list = MusicContainer.getInstance().getPlayList();
list.addAll(MusicContainer.getInstance().getPlayList());
break;
case LIBS_MODEL_PLAY_Collection:
list = new ArrayList<>();
break;
case LIBS_MODEL_ALL:
default:
list.addAll(MusicContainer.getLibs().getMainData());
new Thread(new Runnable() {
@Override
public void run() {
if (musicPath.equals("root")) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
MusicContainer.getLibs().onDirMusicData(musicPath, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
list.clear();
if (data != null && state == 0)
list.addAll((List<MusicData>) data);
notifyDataSetChanged();
}
@Override
public void httpError(Exception e) {
}
});
}
}).start();
}
listMap.put(model, list);
}
@Override
public int getCount() {
super.getCount();
int size = list.size();
System.out.println("列表数量:" + size);
return list.size();
}
@ -117,6 +157,7 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
this.scrollStatus = scrollStatus;
}
@SuppressLint("SetTextI18n")
@NonNull
@Override
@ -132,40 +173,23 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
MusicData data = list.get(position);
holder.title.setText(data.getTitle());
holder.artist.setText(data.getArtist());
holder.icon.setImageResource(R.drawable.ic_dir);
if (!data.isDir()) {
if (model != LIBS_MODEL_Album && model != LIBS_MODEL_Artist) {
holder.message.setText(data.getBitRate() + "kbps | " + data.getSampleRate() + "hz | " + data.getEncodingType());
holder.message.setVisibility(View.VISIBLE);
holder.artist.setVisibility(View.VISIBLE);
} else {
} else {//为专辑或艺术家
holder.message.setVisibility(View.GONE);
holder.artist.setVisibility(View.GONE);
}
// holder.icon.setImageBitmap(ThumbnailUtils.extractThumbnail(data.getImg(), 64,64));
if (scrollStatus == 0) {
if (icons.containsKey(position)) {
Glide.with(getContext()).load(icons.get(position))
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(holder.icon);
} else {
holder.icon.setImageResource(R.drawable.ic_dir);
}
if (icons.containsKey(data.getFile())) {
Glide.with(getContext()).load(icons.get(data.getFile()))
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(holder.icon);
} else {
Bitmap bitmap;
if (icons.containsKey(position)) {
bitmap = icons.get(position);
} else {
bitmap = data.getImg(64, 64);
if (bitmap == null) {
holder.icon.setImageResource(R.drawable.ic_dir);
} else {
icons.put(position, bitmap);
}
}
if (bitmap != null)
Glide.with(getContext()).load(bitmap)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(holder.icon);
showImage(data, holder);
}
holder.icon.setOnClickListener(new View.OnClickListener() {
@Override
@ -180,13 +204,6 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
}
}
});
if (!StringUtil.isEmpty(data.getMd5())) {
if (data.getMd5().equals(MusicContainer.getInstance().getNowPlayData().getMd5())) {
holder.play_mask.setVisibility(View.VISIBLE);
} else {
holder.play_mask.setVisibility(View.GONE);
}
}
} else {
holder.message.setText("");
holder.play_mask.setVisibility(View.GONE);
@ -195,12 +212,11 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
holder.top.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("model = "+model);
System.out.println("model = " + model);
if (model == LIBS_MODEL_Album) {
addAlbumList(data.getTitle());
} else if (data.isDir()) {
//showAddTmpListDialog(data);
showAddTmpListDialog(data);
} else {
MusicContainer.getInstance().addTmpList(data);
Toast.makeText(getContext(), "已添加到队列中", Toast.LENGTH_LONG).show();
@ -211,10 +227,10 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
}
private void addAlbumList(String album) {
System.out.println("添加专辑:"+album);
System.out.println("添加专辑:" + album);
List<MusicData> list = MusicContainer.getInstance().getAllAlbumMapList().get(album);
for (MusicData musicData : list) {
System.out.println(musicData.getTitle()+"| 专辑名 ="+musicData.getAlbum());
System.out.println(musicData.getTitle() + "| 专辑名 =" + musicData.getAlbum());
MusicContainer.getInstance().addTmpList(musicData);
}
Toast.makeText(getContext(), "已添加到队列中", Toast.LENGTH_LONG).show();
@ -230,20 +246,19 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
@Override
public void onClick(DialogInterface dialog, int which) {
hud = AppTools.showLoading(getContext());
MusicContainer.getLibs().getMusicList(data.getFileBase64(), true, new NetworkInterface() {
JSONObject json=new JSONObject();
json.put("path", data.getFileBase64());
json.put("type",true);
MusicContainer.getLibs().onDirMusicData(json, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
List<?> list = (List<?>) data;
for (Object musicData : list) {
MusicContainer.getInstance().addTmpList((MusicData) musicData);
List<MusicData> list = (List<MusicData>) data;
for (MusicData musicData : list) {
System.out.println("加入到临时列表:"+musicData.getTitle());
MusicContainer.getInstance().addTmpList(musicData);
hud.dismiss();
}
AppData.handler.post(new Runnable() {
@Override
public void run() {
hud.dismiss();
Toast.makeText(getContext(), "已添加到队列中", Toast.LENGTH_LONG).show();
}
});
}
@Override
@ -251,7 +266,6 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
}
});
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@ -266,6 +280,46 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
return convertView;
}
private void showImage(MusicData musicData, ViewHolder holder) {
musicData.getImg(64, 64, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
icons.put(musicData.getFile(), (Bitmap) data);
Glide.with(getContext()).load(icons.get(musicData.getFile()))
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(holder.icon);
System.out.println("展示图片:" + state);
}
@Override
public void httpError(Exception e) {
System.out.println("图片下载失败");
}
});
}
public void onDestroy() {
for (String integer : icons.keySet()) {
Objects.requireNonNull(icons.get(integer)).recycle();
}
for (Integer integer : listMap.keySet()) {
for (MusicData data : Objects.requireNonNull(listMap.get(integer))) {
data.recycleImg();
}
Objects.requireNonNull(listMap.get(integer)).clear();
}
listMap.clear();
icons.clear();
}
public void setData(List<MusicData> list) {
this.list = list;
}
public void setPath(String path) {
this.musicPath = path;
}
static class ViewHolder extends RecyclerView.ViewHolder {
MarqueeTextView title, artist, message;
LinearLayout buttons;

View File

@ -11,6 +11,7 @@ import com.alibaba.fastjson.JSONObject;
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.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;
@ -163,32 +164,47 @@ public class MusicData {
private boolean isDownloadImg = false;
public String getImageUrl(){
return NetworkTool.NetworkAPI.MUSIC_IMAGE+"?fileName="+ getFileBase64();
public String getImageUrl() {
return NetworkTool.NetworkAPI.MUSIC_IMAGE + "?fileName=" + getFileBase64();
}
public Bitmap getImg() {
return img;
}
public Bitmap getImg(int width, int height) {
return getImg(width, height, null);
}
public Bitmap getImg(int width, int height, NetworkInterface networkInterface) {
if (img != null) {
if(width==-1&&height==-1){
if (width == -1 && height == -1) {
if (networkInterface != null)
networkInterface.httpGetData(img, 0);
return img;
}
return ThumbnailUtils.extractThumbnail(img,width,height);
if (networkInterface != null)
networkInterface.httpGetData(ThumbnailUtils.extractThumbnail(img, width, height), width+height);
return ThumbnailUtils.extractThumbnail(img, width, height);
}
if (isDownloadImg) {
return null;
}
img = AppTools.getSaveBitmap(getFileBase64());
if (img != null) {
if(width==-1&&height==-1){
if (width == -1 && height == -1) {
if (networkInterface != null)
networkInterface.httpGetData(img, 0);
return img;
}
return ThumbnailUtils.extractThumbnail(img,width,height);
if (networkInterface != null)
networkInterface.httpGetData(ThumbnailUtils.extractThumbnail(img, width, height), width+height);
return ThumbnailUtils.extractThumbnail(img, width, height);
}
img = null;
isDownloadImg = true;
JSONObject json = new JSONObject();
json.put("fileName", getFileBase64());
System.out.println("--->"+ getFileBase64());
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_IMAGE, json, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
@ -198,6 +214,17 @@ public class MusicData {
byte[] bytes = json.getBytes("data");
img = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
AppTools.saveBitmap(img, getFileBase64());
if (networkInterface != null) {
AppData.handler.post(new Runnable() {
@Override
public void run() {
if (width == -1 && height == -1) {
networkInterface.httpGetData(img, 0);
} else
networkInterface.httpGetData(ThumbnailUtils.extractThumbnail(img, width, height), width+height);
}
});
}
}
} catch (Exception e) {
e.printStackTrace();
@ -210,12 +237,18 @@ public class MusicData {
isDownloadImg = false;
}
});
if(width==-1&&height==-1){
if (width == -1 && height == -1) {
return img;
}
return ThumbnailUtils.extractThumbnail(img,width,height);
return ThumbnailUtils.extractThumbnail(img, width, height);
}
public void recycleImg() {
if (img != null) {
img.recycle();
img = null;
}
}
public int getImageColor() {
if (img == null) {
@ -298,9 +331,9 @@ public class MusicData {
@Override
public boolean equals(@Nullable Object obj) {
if(obj instanceof MusicData){
MusicData data= (MusicData) obj;
if(StringUtil.isEmpty(md5)||StringUtil.isEmpty(data.getMd5())){
if (obj instanceof MusicData) {
MusicData data = (MusicData) obj;
if (StringUtil.isEmpty(md5) || StringUtil.isEmpty(data.getMd5())) {
return false;
}
return md5.equals(data.getMd5());

View File

@ -11,6 +11,7 @@ import android.support.v4.media.session.PlaybackStateCompat;
import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.services.MusicService;
import com.yutou.nas_music_player.tools.AppData;
import java.util.ArrayList;
import java.util.List;
@ -54,6 +55,21 @@ public class MediaBrowserHelper {
return controllerCompat.getTransportControls();
}
public void updateMusicData() {
AppData.handler.post(new Runnable() {
@Override
public void run() {
for (MediaControllerCompat.Callback callback : callbacks) {
if (callback != null) {
callback.onMetadataChanged(MusicContainer.getInstance().getNowPlayMetadataCompat());
}
}
}
});
}
private List<MediaControllerCompat.Callback> callbacks = new ArrayList<>();
public void regPlayListener(MediaControllerCompat.Callback playListener) {

View File

@ -9,14 +9,19 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.Datas.PreviousPlayerList;
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.tools.AppData;
import com.yutou.nas_music_player.tools.CollectionTools;
import com.yutou.nas_music_player.tools.ConfigTools;
import com.yutou.nas_music_player.tools.NetworkTool;
import com.yutou.nas_music_player.tools.StringUtil;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
@ -145,10 +150,51 @@ public class MusicContainer {
playIndex = new Random().nextInt(libs.mainData.size());
}
private void updateMusicData() {
for (MediaBrowserHelper browserHelper : browserHelpers) {
if (browserHelper != null)
browserHelper.updateMusicData();
}
}
public void playOfAllRandom() {
System.out.println("随机下一首");
randomMusic();
play(libs.mainData.get(playIndex));
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_RANDOM, new JSONObject(), new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
JSONObject json = JSONObject.parseObject((String) data);
if (json.getInteger("code") == 0) {
String baseData = json.getString("data");
try {
json.put("path", URLDecoder.decode(baseData, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
json.put("path", baseData);
}
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_METADATA, json, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
JSONObject json = JSONObject.parseObject((String) data);
if (json.getInteger("code") == 0) {
nowPlayData = JSONArray.parseObject(json.getString("data"), MusicData.class);
play(nowPlayData);
updateMusicData();
}
}
@Override
public void httpError(Exception e) {
}
});
}
}
@Override
public void httpError(Exception e) {
}
});
}
public void play(MusicData data) {
@ -259,9 +305,10 @@ public class MusicContainer {
isPause = true;
mediaPlayer.pause();
}
public void receiverPause(){
public void receiverPause() {
for (MediaBrowserHelper helper : browserHelpers) {
if(helper.getTransportControls()!=null){
if (helper.getTransportControls() != null) {
helper.getTransportControls().pause();
}
}
@ -284,7 +331,11 @@ public class MusicContainer {
break;
case PLAY_MODEL_RANDOM:
default:
playOfAllRandom();
if (tmpList.size() != 0) {//有插队列表优先播放
play(tmpList.get(0));//先插入的先播
tmpList.remove(0);
} else
playOfAllRandom();
}
}
@ -308,7 +359,7 @@ public class MusicContainer {
*/
private void playOrder(boolean isLoop) {
if (tmpList.size() != 0) {//有插队列表优先播放
play(tmpList.get(0));//后插入先播放
play(tmpList.get(0));//先插入的先播
tmpList.remove(0);
} else if (playList.size() == 0) {//播放列表为空以所有歌列表顺序播放
if (libs.mainData.size() > (playIndex + 1)) {
@ -439,6 +490,39 @@ public class MusicContainer {
});
}
public void onDirMusicData(JSONObject json, NetworkInterface networkInterface) {
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_LIST, json, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
JSONObject json = JSONObject.parseObject((String) data);
AppData.handler.post(new Runnable() {
@Override
public void run() {
if (json.getInteger("code") == 0) {
networkInterface.httpGetData(JSONArray.parseArray(json.getString("data"), MusicData.class), 0);
} else {
networkInterface.httpGetData(new ArrayList<>(), 0);
}
}
});
}
@Override
public void httpError(Exception e) {
networkInterface.httpGetData(new ArrayList<>(), 0);
}
});
}
public void onDirMusicData(String path, NetworkInterface networkInterface) {
if (StringUtil.isEmpty(path))
path = "root";
JSONObject json = new JSONObject();
json.put("path", path);
onDirMusicData(json, networkInterface);
}
public void getMusicList(String path, boolean type, NetworkInterface networkInterface) {
JSONObject json = new JSONObject();
json.put("path", path);

View File

@ -139,6 +139,7 @@ public class MusicService extends MediaBrowserServiceCompat {
public void onPlay() {
super.onPlay();
System.out.println("接收到播放指令");
registerReceiver(receiver, intentFilter);
mState = PlaybackStateCompat.STATE_PLAYING;
MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat();

View File

@ -40,6 +40,7 @@ public class NetworkTool {
public static String MUSIC_METADATA = HOME + "/music/find/file.do";
public static String MUSIC_IMAGE = HOME + "/music/image.do";
public static String MUSIC_PLAY = HOME + "/music/play.do";
public static String MUSIC_RANDOM = HOME + "/music/random.do";
}

View File

@ -12,6 +12,8 @@ import android.widget.ListView;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
import com.yutou.nas_music_player.Adapters.MusicLibsAdapter;
import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.Interfaces.NetworkInterface;
import com.yutou.nas_music_player.R;
import com.yutou.nas_music_player.containers.MediaBrowserHelper;
import com.yutou.nas_music_player.containers.MusicContainer;
@ -22,6 +24,7 @@ import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import in.myinnos.alphabetsindexfastscrollrecycler.IndexFastScrollRecyclerView;
@ -49,6 +52,7 @@ public class MusicLibsFragment extends Fragment {
public void setModel(int model) {
this.model = model;
System.out.println("debug 设置model=" + model);
adapter.initData(model);
adapter.notifyDataSetChanged();
}
@ -56,12 +60,15 @@ public class MusicLibsFragment extends Fragment {
@Override
public void onDestroy() {
super.onDestroy();
browserHelper=null;
browserHelper = null;
adapter.onDestroy();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(view!=null)
return view;
view = inflater.inflate(R.layout.fragment_music_libs, null);
int id = FragmentPagerItem.getPosition(getArguments());
if (map == null)
@ -69,7 +76,8 @@ public class MusicLibsFragment extends Fragment {
if (!map.containsKey(id)) {
map.put(id, this);
}
initViews();
if (adapter == null || recyclerView == null)
initViews();
return view;
}
@ -99,10 +107,18 @@ public class MusicLibsFragment extends Fragment {
recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (browserHelper != null) {
browserHelper.play(adapter.getItem(position));
adapter.notifyDataSetChanged();
MusicData musicData = adapter.getItem(position);
if (musicData != null) {
if (musicData.isDir()) {
changeList(musicData.getFileBase64());
} else {
if (browserHelper != null) {
browserHelper.play(adapter.getItem(position));
adapter.notifyDataSetChanged();
}
}
}
}
});
adapter.setScrollStatus(1);
@ -121,4 +137,22 @@ public class MusicLibsFragment extends Fragment {
public MusicLibsAdapter getAdapter() {
return adapter;
}
private void changeList(String path) {
MusicContainer.getLibs().onDirMusicData(path, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
List<MusicData> list = (List<MusicData>) data;
adapter.setPath(path);
adapter.setData(list);
adapter.notifyDataSetChanged();
}
@Override
public void httpError(Exception e) {
}
});
}
}

View File

@ -163,19 +163,12 @@ public class MainActivity extends AppCompatActivity {
playModel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*int model = (int) playModel.getTag();
int model = (int) playModel.getTag();
if (model == MusicContainer.PLAY_MODEL_LOOP) {
setPlayModelButton(MusicContainer.PLAY_MODEL_RANDOM);
return;
}
setPlayModelButton(model + 1);*/
LinkedHashMap<String,List<MusicData>> map=MusicContainer.getInstance().getAllAlbumMapList();
for (String key : map.keySet()) {
System.out.println("专辑名:"+key);
for (MusicData data : map.get(key)) {
System.out.println(" |单曲:"+data.getTitle()+" 所属专辑:"+data.getAlbum());
}
}
setPlayModelButton(model + 1);
}
});
getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {
@ -371,7 +364,7 @@ public class MainActivity extends AppCompatActivity {
}
if (md5 != null) {
setPlayData(MusicContainer.getLibs().findMusic(md5, MusicContainer.getInstance().getPlayList()));
setPlayData(MusicContainer.getInstance().getNowPlayData());
}
}

View File

@ -1,12 +1,14 @@
package com.yutou.nas_music_player.views;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
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.PlaybackStateCompat;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
@ -20,6 +22,7 @@ import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.yutou.nas_music_player.Adapters.MusicLibsAdapter;
import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.Interfaces.NetworkInterface;
import com.yutou.nas_music_player.R;
import com.yutou.nas_music_player.containers.MediaBrowserHelper;
import com.yutou.nas_music_player.containers.MusicContainer;
@ -130,17 +133,20 @@ public class PlayLibsActivity extends AppCompatActivity {
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (!init) {
MusicLibsFragment fragment = MusicLibsFragment.map.get(position);
fragment.setPlayContainer(browserHelper);
init = true;
if (fragment != null) {
fragment.setPlayContainer(browserHelper);
init = true;
}
}
}
@Override
public void onPageSelected(int position) {
System.out.println("debug 切换页面:" + position);
index = position;
MusicLibsFragment fragment = MusicLibsFragment.map.get(position);
fragment.setPlayContainer(browserHelper);
if (fragment != null) {
fragment.setPlayContainer(browserHelper);
switch (position) {
case 0:
fragment.setModel(MusicLibsAdapter.LIBS_MODEL_ALL);
@ -152,6 +158,8 @@ public class PlayLibsActivity extends AppCompatActivity {
fragment.setModel(MusicLibsAdapter.LIBS_MODEL_Tmp);
break;
}
} else {
System.out.println("debug 切换页面 没得页面");
}
}
@ -161,9 +169,8 @@ public class PlayLibsActivity extends AppCompatActivity {
});
}
private void setImage(MusicData data) {
album_image.setImageBitmap(data.getImg(-1, -1));
Glide.with(this).load(data.getImg(64, 64))
private void setImage(Bitmap data) {
Glide.with(this).load(data)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3)))
.into(album_image);
}
@ -176,25 +183,18 @@ public class PlayLibsActivity extends AppCompatActivity {
artist.setText(data.getArtist());
progressBar.setMax((int) (data.getDurationAsDouble() * 1000));
progressBar.setProgress(bar_pos);
if (data.getImg(-1, -1) != null) {
setImage(data);
} else {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (data.getImg(-1, -1) != null) {
AppData.handler.post(new Runnable() {
@Override
public void run() {
setImage(data);
}
});
cancel();
}
}
}, 0, 100);
}
if (MusicLibsFragment.map!=null&&MusicLibsFragment.map.containsKey(index))
data.getImg(-1, -1, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
setImage((Bitmap) data);
}
@Override
public void httpError(Exception e) {
}
});
if (MusicLibsFragment.map != null && MusicLibsFragment.map.containsKey(index))
MusicLibsFragment.map.get(index).getAdapter().notifyDataSetChanged();
}
@ -238,7 +238,7 @@ public class PlayLibsActivity extends AppCompatActivity {
}
if (md5 != null) {
setPlayData(MusicContainer.getLibs().findMusic(md5, MusicContainer.getInstance().getPlayList()));
setPlayData(MusicContainer.getInstance().getNowPlayData());
}
}