更换播放器

在文件未包含图片的情况下,寻找cover.jpg
读取元数据失败时,用文件名作为标题
This commit is contained in:
2020-10-21 23:25:29 +08:00
parent d46b1b15cc
commit b0460fbe14
15 changed files with 1060 additions and 95 deletions

View File

@@ -11,6 +11,7 @@ public class ToolsApplication {
public static void main(String[] args) {
SpringApplication.run(ToolsApplication.class, args);
RedisTools.initRedisPoolSub();
MusicTools.getInstance().setMusicPath("Z:\\音乐\\Aimer - ARIA STRINGS");
MusicTools.getInstance().scanMusic();
}

View File

@@ -9,8 +9,9 @@ import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagException;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
@Data
public class MusicData {
@@ -25,16 +26,30 @@ public class MusicData {
private String artist_sort;//分类
private File file;//音乐文件
public byte[] readImage() {
public byte[] readImage() throws Exception {
AudioFile audioFile = null;
try {
audioFile = AudioFileIO.read(file);
Tag tag = audioFile.getTag();
return tag.getFirstArtwork().getBinaryData();
} catch (Exception e) {
e.printStackTrace();
audioFile = AudioFileIO.read(file);
Tag tag = audioFile.getTag();
if (tag.getFirstArtwork() == null) {
return readImageFile();
}
return null;
return tag.getFirstArtwork().getBinaryData();
}
private byte[] readImageFile() throws Exception {
String path = file.getAbsolutePath().replace(file.getName(), "");
File img = new File(path, "cover.jpg");
if (!img.exists()) {
img = new File(path, "Cover.jpg");
if (!img.exists()) {
img = new File(path, "COVER.jpg");
if(!img.exists()){
return null;
}
}
}
return Files.readAllBytes(Paths.get(img.getAbsolutePath()));
}
}

View File

@@ -13,6 +13,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.List;
@@ -28,12 +30,11 @@ public class MusicController {
public String getAllMusicList() {
JSONObject json = new JSONObject();
JSONObject data = new JSONObject();
json.put("code", 1);
json.put("code", 0);
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);
json.put("scan", tools.isScan());
json.put("size", tools.getLength());
json.put("data", JSONArray.toJSON(tools.getMusicList()));
return json.toJSONString();
}
@RequestMapping("list.do")
@@ -55,6 +56,19 @@ public class MusicController {
return json.toJSONString();
}
@RequestMapping("find/file.do")
@ResponseBody
public String findFile(String path){
JSONObject json=new JSONObject();
if(StringUtils.isEmpty(path)){
json.put("code",0);
json.put("msg","地址为空");
return json.toJSONString();
}
json.put("code",1);
json.put("data",MusicTools.getInstance().getMetadata(new File(base64ToString(path))));
return json.toJSONString();
}
@RequestMapping("image.do")
@ResponseBody
@@ -67,32 +81,58 @@ public class MusicController {
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", "文件不存在");
File file = new File(base64ToString(fileName));
try {
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", "");
}
}catch (Exception e){
json.put("msg", "图片读取失败");
json.put("code", 0);
json.put("data", "");
e.printStackTrace();
}
return json.toJSONString();
}
@RequestMapping("random.do")
@ResponseBody
public String random(){
List<MusicData> list=MusicTools.getInstance().getMusicList();
MusicData data=list.get(Tools.randomCommon(0,list.size()-1,1)[0]);
JSONObject json=new JSONObject();
json.put("code",1);
try {
json.put("data", URLEncoder.encode(getBase64(data.getFile().getAbsolutePath()),"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return json.toJSONString();
}
private String getBase64(String str){
return new String(Base64.getEncoder().encode(str.getBytes()));
}
private String base64ToString(String base){
base=base.replace(" ","+");
return new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes()));
}
@RequestMapping("play.do")
public ResponseEntity<FileSystemResource> play(String filePath,boolean random) {
if(!StringUtils.isEmpty(filePath)){
filePath=new String(Base64.getDecoder().decode(filePath.getBytes()));
filePath=base64ToString(filePath);
}
if(random){
List<MusicData> list=MusicTools.getInstance().getMusicList();
System.out.println("size = "+list.size());
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();
System.out.println("随机歌曲:"+data.getTitle());
}
File file = new File(filePath);
if (file.exists()) {
@@ -101,4 +141,5 @@ public class MusicController {
return null;
}
}
}

View File

@@ -3,11 +3,14 @@ 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.audio.exceptions.CannotReadException;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import org.springframework.util.StringUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Objects;
@@ -27,15 +30,14 @@ public class MusicTools {
return tools;
}
public MusicTools() {
scanMusic();
}
public void scanMusic() {
public synchronized void scanMusic() {
System.out.println("执行扫描");
musicList.clear();
new Thread(() -> {
isScan = true;
scan(new File(musicPath));
isScan = false;
System.out.println("扫描完成");
}).start();
}
@@ -78,22 +80,62 @@ public class MusicTools {
public MusicData getMetadata(File file) {
try {
if (file.getName().endsWith(".lrc") || file.getName().endsWith(".jpg")) {
return null;
}
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));
try {
data.setAlbum(tag.getFirst(FieldKey.ALBUM));
} catch (Exception e) {
}
try {
data.setArtist(tag.getFirst(FieldKey.ARTIST));
} catch (Exception e) {
}
try {
data.setArtist_sort(tag.getFirst(FieldKey.ARTIST_SORT));
} catch (Exception e) {
}
try {
data.setComment(tag.getFirst(FieldKey.COMMENT));
} catch (Exception e) {
}
try {
data.setComposer(tag.getFirst(FieldKey.COMPOSER));
} catch (Exception e) {
}
try {
data.setDisc_no(tag.getFirst(FieldKey.DISC_NO));
} catch (Exception e) {
}
try {
if (StringUtils.isEmpty(tag.getFirst(FieldKey.TITLE))) {
data.setTitle(file.getName());
} else {
data.setTitle(tag.getFirst(FieldKey.TITLE));
}
} catch (Exception e) {
data.setTitle(file.getName());
}
try {
data.setTrack(tag.getFirst(FieldKey.TRACK));
} catch (Exception e) {
}
try {
data.setYear(tag.getFirst(FieldKey.YEAR));
} catch (Exception e) {
}
data.setFile(file);
return data;
} catch (CannotReadException e) {
MusicData data = new MusicData();
data.setTitle(file.getName());
data.setFile(file);
return data;
} catch (Exception e) {
//e.printStackTrace();
e.printStackTrace();
}
return null;
}
@@ -129,6 +171,7 @@ public class MusicTools {
return list;
}
public String getMusicPath() {
return musicPath;
}
@@ -155,7 +198,9 @@ public class MusicTools {
}
public static void main(String[] args) {
String base = "Wjpc6Z-z5LmQXOOAkOmbqOWuruWkqeOAkU1PUkEg5YWo5aWXMTHlvKBIaXJlc-S4k-i-kVxbMjAyMDA5MDJdIOmbqOWuruWkqSAzcmTjgqLjg6vjg5Djg6DjgIxQYWludCBpdCwgQkxVReOAjVs5NmtIejI0Yml0XVtGTEFDXVwwMDMtRGVmaWFuY2UuZmxhYw==";
System.out.println(new String(Base64.getDecoder().decode(base)));
}
}