This commit is contained in:
yutou 2020-11-18 18:22:15 +08:00
parent 68a510638d
commit 5d1114a2cb
5 changed files with 66 additions and 20 deletions

View File

@ -7,17 +7,22 @@ import com.alibaba.fastjson.JSONObject;
import com.yutou.nas_music_player.Interfaces.NetworkInterface;
import com.yutou.nas_music_player.tools.NetworkTool;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MusicContainer {
private static MusicContainer container;
private MusicLibs musicLibs;
private MusicLibs libs;
private MediaPlayer mediaPlayer;
private int playIndex=0;
private MusicContainer() {
mediaPlayer=new MediaPlayer();
musicLibs=new MusicLibs();
libs =new MusicLibs();
}
public static MusicContainer getInstance() {
@ -26,23 +31,47 @@ public class MusicContainer {
}
return container;
}
public void randomMusic(){
playIndex=new Random().nextInt(libs.mainData.size());
}
public void playOfAllRandom(){
randomMusic();
play(libs.mainData.get(playIndex));
}
public void play(MusicData data){
String url= NetworkTool.NetworkAPI.HOME+ NetworkTool.NetworkAPI.MUSIC_PLAY
+"?random=false&token="+ NetworkTool.NetworkAPI.HTTP_KEY+"&filePath="+data.getPlayFile();
try {
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.reset();
}
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public static MusicLibs getLibs(){
return getInstance().musicLibs;
return getInstance().libs;
}
public static class MusicLibs{
private List<MusicData> mainData;
private MusicLibs(){
mainData=new ArrayList<>();
initData();
}
private void initData() {
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_LIST, new JSONObject(), new NetworkInterface() {
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_ALL, new JSONObject(), new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
JSONObject json=JSONObject.parseObject(data.toString());
if(json.getInteger("code")==0){
List<MusicData> list= JSONArray.parseArray(json.getJSONArray("data").toJSONString(),MusicData.class);
System.out.println(list.size());
for (MusicData musicData : list) {
mainData= JSONArray.parseArray(json.getJSONArray("data").toJSONString(),MusicData.class);
System.out.println(mainData.size());
for (MusicData musicData : mainData) {
System.out.println(musicData.getTitle());
}
}

View File

@ -1,11 +1,7 @@
package com.yutou.nas_music_player.containers;
import java.io.*;
import android.util.Base64;
public class MusicData {
private String artist;//艺术家
@ -18,6 +14,7 @@ public class MusicData {
private String composer;//作曲
private String artist_sort;//分类
private String lastDir;//上一个文件夹
private String file;
private boolean isDir = false;
public MusicData() {
@ -111,4 +108,14 @@ public class MusicData {
public void setDir(boolean dir) {
isDir = dir;
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public String getPlayFile(){
return new String(Base64.encode(file.getBytes(),Base64.DEFAULT));
}
}

View File

@ -33,12 +33,13 @@ import static com.yutou.nas_music_player.tools.NetworkTool.NetworkAPI.HOME;
public class NetworkTool {
public static class NetworkAPI {
private static final String HTTP_KEY = "PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk";
public static final String HTTP_KEY = "PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk";
public static String HOME = "";
public static String MUSIC_LIST = HOME + "/music/list.do";
public static String MUSIC_METADATA = HOME + "/find/file.do";
public static String MUSIC_IMAGE = HOME + "/image.do";
public static String MUSIC_PLAY = HOME + "/play.do";
public static String MUSIC_ALL = HOME + "/music/all.do";
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";
}

View File

@ -3,16 +3,25 @@ package com.yutou.nas_music_player.views;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import com.yutou.nas_music_player.R;
import com.yutou.nas_music_player.containers.MusicContainer;
public class MainActivity extends AppCompatActivity {
private ImageButton play,previous,next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MusicContainer.getInstance();
play=findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MusicContainer.getInstance().playOfAllRandom();
}
});
}
}

View File

@ -41,7 +41,7 @@
app:layout_constraintTop_toBottomOf="@+id/frameLayout">
<ImageButton
android:id="@+id/imageButton"
android:id="@+id/previous"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
@ -49,14 +49,14 @@
android:src="@android:drawable/ic_media_previous" />
<ImageButton
android:id="@+id/imageButton2"
android:id="@+id/play"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@android:drawable/ic_media_play" />
<ImageButton
android:id="@+id/imageButton3"
android:id="@+id/next"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_gravity="center"