开始整音乐播放器
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.yutou.tools;
|
||||
|
||||
import com.yutou.tools.utils.MusicTools;
|
||||
import com.yutou.tools.utils.RedisTools;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@@ -10,6 +11,7 @@ public class ToolsApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ToolsApplication.class, args);
|
||||
RedisTools.initRedisPoolSub();
|
||||
MusicTools.getInstance().scanMusic();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -59,8 +59,12 @@ public class AnimationData {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static void initData(){
|
||||
String url="https://share.dmhy.org/topics/advanced-search?team_id=0&sort_id=0&orderby=";
|
||||
String data=Tools.get(url);
|
||||
System.out.println(data);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
System.out.println(BiliBiliLiveTools.getLiveUserName("59901"));
|
||||
initData();
|
||||
}
|
||||
}
|
||||
|
||||
40
src/main/java/com/yutou/tools/home/nas/Data/MusicData.java
Normal file
40
src/main/java/com/yutou/tools/home/nas/Data/MusicData.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.yutou.tools.home.nas.Data;
|
||||
|
||||
import lombok.Data;
|
||||
import org.jaudiotagger.audio.AudioFile;
|
||||
import org.jaudiotagger.audio.AudioFileIO;
|
||||
import org.jaudiotagger.audio.exceptions.CannotReadException;
|
||||
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
|
||||
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
|
||||
import org.jaudiotagger.tag.Tag;
|
||||
import org.jaudiotagger.tag.TagException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
@Data
|
||||
public class MusicData {
|
||||
private String artist;//艺术家
|
||||
private String album;//专辑
|
||||
private String title;//标题
|
||||
private String comment;//评论
|
||||
private String year;//年份
|
||||
private String track;//音轨号
|
||||
private String disc_no;//碟片编号
|
||||
private String composer;//作曲
|
||||
private String artist_sort;//分类
|
||||
private File file;//音乐文件
|
||||
|
||||
public byte[] readImage() {
|
||||
AudioFile audioFile = null;
|
||||
try {
|
||||
audioFile = AudioFileIO.read(file);
|
||||
Tag tag = audioFile.getTag();
|
||||
return tag.getFirstArtwork().getBinaryData();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
104
src/main/java/com/yutou/tools/home/nas/MusicController.java
Normal file
104
src/main/java/com/yutou/tools/home/nas/MusicController.java
Normal file
@@ -0,0 +1,104 @@
|
||||
package com.yutou.tools.home.nas;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.tools.home.nas.Data.MusicData;
|
||||
import com.yutou.tools.utils.MusicTools;
|
||||
import com.yutou.tools.utils.Tools;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/nas/music/")
|
||||
public class MusicController {
|
||||
|
||||
private String defaultMusicPath="C:\\Users\\admin\\Music";
|
||||
private String musicPath=defaultMusicPath;
|
||||
|
||||
@RequestMapping("all.do")
|
||||
@ResponseBody
|
||||
public String getAllMusicList() {
|
||||
JSONObject json = new JSONObject();
|
||||
JSONObject data = new JSONObject();
|
||||
json.put("code", 1);
|
||||
MusicTools tools = MusicTools.getInstance();
|
||||
data.put("scan", tools.isScan());
|
||||
data.put("size", tools.getLength());
|
||||
data.put("list", JSONArray.toJSON(tools.getMusicList()));
|
||||
json.put("data", data);
|
||||
return json.toJSONString();
|
||||
}
|
||||
@RequestMapping("list.do")
|
||||
@ResponseBody
|
||||
public String getMusicListOfPath(String path){
|
||||
if(StringUtils.isEmpty(path)){
|
||||
return getAllMusicList();
|
||||
}
|
||||
path=path.replace(defaultMusicPath+File.separator,"");
|
||||
JSONObject json=new JSONObject();
|
||||
JSONObject data = new JSONObject();
|
||||
json.put("code", 1);
|
||||
MusicTools tools = MusicTools.getInstance();
|
||||
tools.setMusicPath(defaultMusicPath+File.separator+path);
|
||||
data.put("scan", tools.isScan());
|
||||
data.put("size", tools.getLength());
|
||||
data.put("list", JSONArray.toJSON(tools.getMusicPath()));
|
||||
json.put("data", data);
|
||||
return json.toJSONString();
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping("image.do")
|
||||
@ResponseBody
|
||||
public String getImage(String fileName) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("code", 1);
|
||||
if (StringUtils.isEmpty(fileName)) {
|
||||
json.put("msg", "文件为空");
|
||||
json.put("code", 0);
|
||||
json.put("data", "");
|
||||
return json.toJSONString();
|
||||
}
|
||||
File file = new File(new String(Base64.getDecoder().decode(fileName.getBytes())));
|
||||
if (file.exists()) {
|
||||
json.put("msg", "ok");
|
||||
json.put("code", 1);
|
||||
json.put("data", MusicTools.getInstance().getMetadata(file).readImage());
|
||||
} else {
|
||||
json.put("msg", "文件不存在");
|
||||
json.put("code", 0);
|
||||
json.put("data", "");
|
||||
}
|
||||
return json.toJSONString();
|
||||
}
|
||||
|
||||
@RequestMapping("play.do")
|
||||
public ResponseEntity<FileSystemResource> play(String filePath,boolean random) {
|
||||
if(!StringUtils.isEmpty(filePath)){
|
||||
filePath=new String(Base64.getDecoder().decode(filePath.getBytes()));
|
||||
}
|
||||
if(random){
|
||||
List<MusicData> list=MusicTools.getInstance().getMusicList();
|
||||
MusicData data=list.get(Tools.randomCommon(0,list.size()-1,1)[0]);
|
||||
if(data==null){
|
||||
System.out.println("随机歌曲:"+data.getTitle());
|
||||
return play(filePath, true);
|
||||
}
|
||||
filePath=data.getFile().getAbsolutePath();
|
||||
}
|
||||
File file = new File(filePath);
|
||||
if (file.exists()) {
|
||||
return Tools.getFile(file);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,11 +17,16 @@ import java.util.Enumeration;
|
||||
@Component
|
||||
@WebFilter
|
||||
public class APIFilter implements Filter {
|
||||
private static boolean isDebug=true;
|
||||
@Resource
|
||||
UKeyDao keyDao;
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
if(isDebug){
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
return;
|
||||
}
|
||||
HttpServletRequest request = (HttpServletRequest) servletRequest;
|
||||
HttpServletResponse response= (HttpServletResponse) servletResponse;
|
||||
String token = request.getParameter("token");
|
||||
|
||||
161
src/main/java/com/yutou/tools/utils/MusicTools.java
Normal file
161
src/main/java/com/yutou/tools/utils/MusicTools.java
Normal file
@@ -0,0 +1,161 @@
|
||||
package com.yutou.tools.utils;
|
||||
|
||||
import com.yutou.tools.home.nas.Data.MusicData;
|
||||
import org.jaudiotagger.audio.AudioFile;
|
||||
import org.jaudiotagger.audio.AudioFileIO;
|
||||
import org.jaudiotagger.tag.FieldKey;
|
||||
import org.jaudiotagger.tag.Tag;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class MusicTools {
|
||||
public static final int FIND_TITLE = 1;
|
||||
public static final int FIND_ARTIST = 2;
|
||||
|
||||
private static MusicTools tools;
|
||||
private String musicPath = "C:\\Users\\admin\\Music";
|
||||
private final List<MusicData> musicList = new ArrayList<>();
|
||||
private boolean isScan = false;
|
||||
|
||||
public static MusicTools getInstance() {
|
||||
if (tools == null) {
|
||||
tools = new MusicTools();
|
||||
}
|
||||
return tools;
|
||||
}
|
||||
|
||||
public MusicTools() {
|
||||
scanMusic();
|
||||
}
|
||||
|
||||
public void scanMusic() {
|
||||
new Thread(() -> {
|
||||
isScan = true;
|
||||
scan(new File(musicPath));
|
||||
isScan = false;
|
||||
}).start();
|
||||
}
|
||||
|
||||
public List<MusicData> scan(File path, boolean isScanDir) {
|
||||
if (isScanDir) {
|
||||
scan(path);
|
||||
return musicList;
|
||||
} else {
|
||||
if (path.exists() && path.isDirectory()) {
|
||||
List<MusicData> list = new ArrayList<>();
|
||||
for (File file : path.listFiles()) {
|
||||
list.add(getMetadata(file));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void scan(File path) {
|
||||
if (path.isFile()) {
|
||||
add(path);
|
||||
} else if (path.isDirectory()) {
|
||||
for (File file : Objects.requireNonNull(path.listFiles())) {
|
||||
if (file.isDirectory()) {
|
||||
scan(file);
|
||||
} else {
|
||||
add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void add(File file) {
|
||||
MusicData data = getMetadata(file);
|
||||
if (data != null) {
|
||||
musicList.add(data);
|
||||
}
|
||||
}
|
||||
|
||||
public MusicData getMetadata(File file) {
|
||||
try {
|
||||
AudioFile audioFile = AudioFileIO.read(file);
|
||||
Tag tag = audioFile.getTag();
|
||||
MusicData data = new MusicData();
|
||||
data.setAlbum(tag.getFirst(FieldKey.ALBUM));
|
||||
data.setArtist(tag.getFirst(FieldKey.ARTIST));
|
||||
data.setArtist_sort(tag.getFirst(FieldKey.ARTIST_SORT));
|
||||
data.setComment(tag.getFirst(FieldKey.COMMENT));
|
||||
data.setComposer(tag.getFirst(FieldKey.COMPOSER));
|
||||
data.setDisc_no(tag.getFirst(FieldKey.DISC_NO));
|
||||
data.setTitle(tag.getFirst(FieldKey.TITLE));
|
||||
data.setTrack(tag.getFirst(FieldKey.TRACK));
|
||||
data.setYear(tag.getFirst(FieldKey.YEAR));
|
||||
data.setFile(file);
|
||||
return data;
|
||||
} catch (Exception e) {
|
||||
//e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public List<MusicData> findOfTitle(String title) {
|
||||
return find(title, FIND_TITLE);
|
||||
}
|
||||
|
||||
public List<MusicData> findOfArtist(String by) {
|
||||
return find(by, FIND_ARTIST);
|
||||
}
|
||||
|
||||
public List<MusicData> getMusicList() {
|
||||
return musicList;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return tools.musicList.size();
|
||||
}
|
||||
|
||||
public boolean isScan() {
|
||||
return isScan;
|
||||
}
|
||||
|
||||
private List<MusicData> find(String title, int type) {
|
||||
List<MusicData> list = new ArrayList<>();
|
||||
for (MusicData data : musicList) {
|
||||
if (data.getTitle().contains(title)) {
|
||||
list.add(data);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public String getMusicPath() {
|
||||
return musicPath;
|
||||
}
|
||||
|
||||
public void setMusicPath(String musicPath) {
|
||||
this.musicPath = musicPath;
|
||||
}
|
||||
|
||||
private void saveImage(MusicData data) {
|
||||
try {
|
||||
File file = new File(data.getTitle() + ".jpg");
|
||||
if (!file.exists()) {
|
||||
if (!file.createNewFile()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
OutputStream outputStream = new FileOutputStream(file);
|
||||
outputStream.write(data.readImage());
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,10 @@ package com.yutou.tools.utils;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.yutou.tools.interfaces.DownloadInterface;
|
||||
import com.yutou.tools.nas.UpdateIp;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -258,4 +262,15 @@ public class Tools {
|
||||
downloadInterface.onError(e);
|
||||
}
|
||||
}
|
||||
public static ResponseEntity<FileSystemResource> getFile(File file){
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
System.out.println(file.getName());
|
||||
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
|
||||
headers.add("Pragma", "no-cache");
|
||||
headers.add("Expires", "0");
|
||||
headers.add("Last-Modified", new Date().toString());
|
||||
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
|
||||
return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user