新增播放整个文件夹功能

新增下一首功能
This commit is contained in:
yutou 2020-10-26 18:32:35 +08:00
parent 76c064b14c
commit 806b257e81
3 changed files with 130 additions and 67 deletions

View File

@ -41,9 +41,16 @@ public class MusicController {
@RequestMapping("list.do") @RequestMapping("list.do")
@ResponseBody @ResponseBody
public String getMusicListOfPath(String path){ public String getMusicListOfPath(String path){
if(StringUtils.isEmpty(path)||path.equals("root")){ if(!StringUtils.isEmpty(path)&&!new File(path).exists()){
path=base64ToString(path);
}
if(StringUtils.isEmpty(path)
||path.equals("root")
||!path.contains(defaultMusicPath)
){
path=defaultMusicPath; path=defaultMusicPath;
} }
//path=path.replace(defaultMusicPath+File.separator,""); //path=path.replace(defaultMusicPath+File.separator,"");
JSONObject json=new JSONObject(); JSONObject json=new JSONObject();
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();

View File

@ -20,7 +20,7 @@ public class MusicTools {
private static MusicTools tools; private static MusicTools tools;
private String musicPath = "C:\\Users\\admin\\Music"; private String musicPath = "C:\\Users\\admin\\Music";
private final List<MusicData> musicList = new ArrayList<>(); private final List<MusicData> musicList = new ArrayList<>();
private HashMap<String,List<MusicData>> musicMap=new HashMap<String,List<MusicData>>(); private HashMap<String, List<MusicData>> musicMap = new HashMap<String, List<MusicData>>();
private boolean isScan = false; private boolean isScan = false;
public static MusicTools getInstance() { public static MusicTools getInstance() {
@ -29,12 +29,13 @@ public class MusicTools {
} }
return tools; return tools;
} }
private MusicTools(){
private MusicTools() {
scanMusic(); scanMusic();
} }
public synchronized void scanMusic() { public synchronized void scanMusic() {
if(isScan){ if (isScan) {
return; return;
} }
System.out.println("执行扫描"); System.out.println("执行扫描");
@ -76,12 +77,32 @@ public class MusicTools {
} }
} }
} }
public List<MusicData> getPath(String path){
if(musicMap.containsKey(path)){ public void getPathOrDir(String path, List<MusicData> list) {
List<MusicData> list=new ArrayList<>(); File files = new File(path);
MusicData tmp=musicMap.get(path).isEmpty()?null:musicMap.get(path).get(0); for (File file : files.listFiles()) {
if(tmp!=null){ if (file.isFile()) {
if(!tmp.getFile().getParent().equals(MusicController.defaultMusicPath)) { list.add(getMetadata(file));
} else {
getPathOrDir(file.getAbsolutePath(), list);
}
}
}
public List<MusicData> getPath(String path) {
List<MusicData> list = new ArrayList<>();
if (new File(path).isDirectory()&&!path.equals(MusicController.defaultMusicPath)) {
for (String key : musicMap.keySet()) {
if(key.startsWith(path)){
list.addAll(musicMap.get(key));
}
}
return list;
}
if (musicMap.containsKey(path)) {
MusicData tmp = musicMap.get(path).isEmpty() ? null : musicMap.get(path).get(0);
if (tmp != null) {
if (!tmp.getFile().getParent().equals(MusicController.defaultMusicPath)) {
MusicData t2 = new MusicData(); MusicData t2 = new MusicData();
t2.setTitle("返回"); t2.setTitle("返回");
t2.setFile(new File(tmp.getLastDir())); t2.setFile(new File(tmp.getLastDir()));
@ -92,13 +113,14 @@ public class MusicTools {
getDirList(path, list); getDirList(path, list);
list.addAll(musicMap.get(path)); list.addAll(musicMap.get(path));
return list; return list;
}else{ } else {
if(path.contains(MusicController.defaultMusicPath)){ if (path.contains(MusicController.defaultMusicPath)) {
List<MusicData> list=new ArrayList<>();
MusicData t2 = new MusicData(); MusicData t2 = new MusicData();
t2.setTitle("返回"); t2.setTitle("返回");
t2.setFile(new File(path).getParentFile()); t2.setFile(new File(path).getParentFile());
list.add(t2); if (!path.equals(MusicController.defaultMusicPath)) {
list.add(t2);
}
getDirList(path, list); getDirList(path, list);
return list; return list;
} }
@ -108,10 +130,10 @@ public class MusicTools {
} }
private void getDirList(String path, List<MusicData> list) { private void getDirList(String path, List<MusicData> list) {
File file=new File(path); File file = new File(path);
for (File listFile : file.listFiles()) { for (File listFile : file.listFiles()) {
if(listFile.isDirectory()){ if (listFile.isDirectory()) {
MusicData data=new MusicData(); MusicData data = new MusicData();
data.setTitle(listFile.getName()); data.setTitle(listFile.getName());
data.setFile(listFile); data.setFile(listFile);
list.add(data); list.add(data);
@ -123,15 +145,15 @@ public class MusicTools {
MusicData data = getMetadata(file); MusicData data = getMetadata(file);
if (data != null) { if (data != null) {
musicList.add(data); musicList.add(data);
String path=file.getAbsolutePath().replace(File.separator+file.getName(),""); String path = file.getAbsolutePath().replace(File.separator + file.getName(), "");
List<MusicData> list; List<MusicData> list;
if(musicMap.containsKey(path)){ if (musicMap.containsKey(path)) {
list=musicMap.get(path); list = musicMap.get(path);
}else{ } else {
list=new ArrayList<>(); list = new ArrayList<>();
} }
list.add(data); list.add(data);
musicMap.put(path,list); musicMap.put(path, list);
} }
} }
@ -257,9 +279,9 @@ public class MusicTools {
} }
} }
public static void main(String[] args) throws Exception{ public static void main(String[] args) throws Exception {
String base = "bXVzaWMuaHRtbDo2MyBaOlzpn7PkuZBc55m75bGx5bCR5aWzXFtzM11bRURd6Imy6YGV44GE44Gu57%2B8XDAyLiAwMiDoibLpgZXjgYTjga7nv7wgKOOBguOBiuOBhOODkOODvOOCuOODp%2BODsykud2F2Cg%3D%3D"; String base = "bXVzaWMuaHRtbDo2MyBaOlzpn7PkuZBc55m75bGx5bCR5aWzXFtzM11bRURd6Imy6YGV44GE44Gu57%2B8XDAyLiAwMiDoibLpgZXjgYTjga7nv7wgKOOBguOBiuOBhOODkOODvOOCuOODp%2BODsykud2F2Cg%3D%3D";
byte[] bytes=getInstance().getMetadata(new File("Z:\\音乐\\BanG Dream!\\Poppin'on!\\B.O.F.flac")).readImage(); byte[] bytes = getInstance().getMetadata(new File("Z:\\音乐\\BanG Dream!\\Poppin'on!\\B.O.F.flac")).readImage();
System.out.println(bytes.length); System.out.println(bytes.length);
} }

View File

@ -7,7 +7,6 @@
<title>来点Music~</title> <title>来点Music~</title>
<link rel="stylesheet" href="/layui/css/layui.css"> <link rel="stylesheet" href="/layui/css/layui.css">
</head> </head>
<link href="/css/video-js.min.css" rel="stylesheet">
<body> <body>
<div class="layui-layout layui-layout-admin"> <div class="layui-layout layui-layout-admin">
<div id="header"></div> <div id="header"></div>
@ -20,11 +19,13 @@
<img src="/assets/defaultPlayImg.jpg" id="img" style="height: 200px; width: 200px"/> <img src="/assets/defaultPlayImg.jpg" id="img" style="height: 200px; width: 200px"/>
<div style="display:inline-block;"> <div style="display:inline-block;">
<div id="playerNowList">播放列表:</div>
<div id="title">标题:</div> <div id="title">标题:</div>
<div id="artist">艺术家:</div> <div id="artist">艺术家:</div>
<div id="album">专辑:</div> <div id="album">专辑:</div>
<div id="composer">作曲家</div> <div id="composer">作曲家</div>
<a class="layui-btn layui-btn-normal" id="download">下载</a> <a class="layui-btn layui-btn-normal" id="download">下载</a>
<a class="layui-btn layui-btn-normal" id="next">下一首</a>
</div> </div>
<div id="audioWrap"></div> <div id="audioWrap"></div>
@ -43,59 +44,68 @@
<link rel="stylesheet" href="/css/AudioPlayer.css"> <link rel="stylesheet" href="/css/AudioPlayer.css">
<script src="/js/AudioPlayer.js"></script> <script src="/js/AudioPlayer.js"></script>
<script id="listTemplet"> <script id="listTemplet">
{{# if(d.dir){ }} {{# if(d.dir){ }}
<div><i class="layui-icon">&#xe656; </i>{{d.title}}</div> <div><i class="layui-icon">&#xe656; </i>{{d.title}}</div>
{{# } else { }} {{# } else { }}
<div><i class="layui-icon">&#xe6fc; </i>{{d.title}}</div> <div><i class="layui-icon">&#xe6fc; </i>{{d.title}}</div>
{{# } }} {{# } }}
</script> </script>
<script type="text/html" id="music"> <script type="text/html" id="music">
<a class="layui-btn layui-btn-xs" lay-event="download">下载</a> <a class="layui-btn layui-btn-xs" lay-event="download">下载</a>
<a class="layui-btn layui-btn-xs" lay-event="play">播放</a>
</script> </script>
<script> <script>
let localhost = ""; let localhost = "";
let musicLib=[] let musicLib = []
let isRandom=true; let isRandom = true;
let playIndex=0; let playIndex = 0;
let playNow="" let playNow = ""
$.get("/nas/music/getlocalhost.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk",function (obj){ $.get("/nas/music/getlocalhost.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk", function (obj) {
let json=JSON.parse(obj); try {
localhost=json.data; let json = JSON.parse(obj);
// localhost="http://116.22.200.20:8000" localhost = json.data+":8000";
if(localhost==='http://null:8000'){
localhost=""
}
} catch (e) {
localhost = ""
}
console.log(localhost)
// localhost="http://116.22.200.20:8000"
layui.use(['table', 'element'], function () { layui.use(['table', 'element'], function () {
let table = layui.table; let table = layui.table;
let element = layui.element; let element = layui.element;
let listTable=table.render({ let listTable = table.render({
elem: '#playlist' elem: '#playlist'
, url: localhost + '/nas/music/list.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk' //数据接口 , url: localhost + '/nas/music/list.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk' //数据接口
, page: true //开启分页 , page: true //开启分页
, cols: [[ , cols: [[
{field: 'title', title: '标题', width: 400,templet:"#listTemplet"} {field: 'title', title: '标题', width: 400, templet: "#listTemplet"}
, {field: 'artist', title: '艺术家', width: 200} , {field: 'artist', title: '艺术家', width: 200}
, {field: 'album', title: '专辑', width: 200} , {field: 'album', title: '专辑', width: 200}
, {field: 'composer', title: '作曲', width: 200} , {field: 'composer', title: '作曲', width: 200}
, {field: 'track', title: '音轨号', width: 100} , {field: 'track', title: '音轨号', width: 100}
, {field: "right", width: 100, toolbar: '#music'} , {field: "right", width: 150, toolbar: '#music'}
]] ]]
,done:function (res,curr,count){ , done: function (res, curr, count) {
musicLib=res.data musicLib = res.data
} }
}) })
table.on('rowDouble(music)', function (obj) { table.on('rowDouble(music)', function (obj) {
//obj 同上 //obj 同上
console.log(obj.data) //得到当前行元素对象 console.log(obj.data) //得到当前行元素对象
if(obj.data.dir){ if (obj.data.dir) {
listTable.reload({ listTable.reload({
where:{ where: {
path:obj.data.file path: obj.data.file
} }
,page:{ , page: {
curr:1 curr: 1
} }
}); });
}else{ } else {
isRandom=false; isRandom = false;
playIndex=obj.tr[0].dataset.index; playIndex = obj.tr[0].dataset.index;
play(obj.data.file) play(obj.data.file)
} }
@ -103,11 +113,23 @@
table.on('tool(music)', function (obj) { table.on('tool(music)', function (obj) {
let data = obj.data; let data = obj.data;
if(obj.event === 'download'){ if (obj.event === 'download') {
if(!data.dir){ if (!data.dir) {
window.open(localhost+"/nas/music/play.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&random=false&filePath="+new Base64().encode(data.file)) window.open(localhost + "/nas/music/play.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&random=false&filePath=" + new Base64().encode(data.file))
} }
} else if (obj.event === 'play') {
console.log(data.file)
$.get(localhost + "/nas/music/list.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&path=" + new Base64().encode(data.file), function (obj) {
let json = JSON.parse(obj);
console.log(json)
if(json.code===0){
playIndex=0;
musicLib=json.data;
isRandom=false;
playNext()
}
})
} }
}); });
element.on('nav(menus)', function (elem) { element.on('nav(menus)', function (elem) {
@ -126,9 +148,9 @@
, allowSeek: true , allowSeek: true
, endedCallback: function () { , endedCallback: function () {
console.log('播放完成') console.log('播放完成')
if(isRandom){ if (isRandom) {
random() random()
}else{ } else {
playNext() playNext()
} }
@ -141,22 +163,29 @@
$('#footer').load("/html/footer.html"); $('#footer').load("/html/footer.html");
$('#side').load("/html/body/nas/side.html"); $('#side').load("/html/body/nas/side.html");
$('#img').click(function () { $('#img').click(function () {
isRandom=true; isRandom = true;
random() random()
}); });
$('#download').click(function (){ $('#download').click(function () {
window.open(localhost+"/nas/music/play.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&random=false&filePath="+playNow) window.open(localhost + "/nas/music/play.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&random=false&filePath=" + playNow)
})
$('#next').click(function (){
playNext()
}) })
function playNext(){ function playNext() {
if(playIndex===musicLib.length){ if(isRandom){
playIndex=0; random()
return ;
} }
if(musicLib[playIndex].dir){ if (playIndex === musicLib.length) {
playIndex = 0;
}
if (musicLib[playIndex].dir) {
playIndex++ playIndex++
playNext() playNext()
}else{ } else {
play(musicLib[playIndex].file); play(musicLib[playIndex].file);
playIndex++ playIndex++
} }
@ -166,9 +195,9 @@
$.get(localhost + "/nas/music/random.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk", function (data) { $.get(localhost + "/nas/music/random.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk", function (data) {
let json = JSON.parse(data) let json = JSON.parse(data)
if (json.code === 0) { if (json.code === 0) {
playNow=json.data playNow = json.data
player.updateSource({ player.updateSource({
source: localhost + "/nas/music/play.do?random=false&filePath=" + json.data+"&token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk" source: localhost + "/nas/music/play.do?random=false&filePath=" + json.data + "&token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk"
}); });
update(json.data) update(json.data)
player.play() player.play()
@ -181,9 +210,9 @@
function play(file) { function play(file) {
let filePath = escape(new Base64().encode(file)) let filePath = escape(new Base64().encode(file))
playNow=filePath playNow = filePath
player.updateSource({ player.updateSource({
source: localhost + "/nas/music/play.do?random=false&filePath=" + filePath+"&token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk" source: localhost + "/nas/music/play.do?random=false&filePath=" + filePath + "&token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk"
}); });
update(filePath) update(filePath)
player.play() player.play()
@ -192,6 +221,11 @@
function update(fileName) { function update(fileName) {
$.get(localhost + '/nas/music/find/file.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&path=' + fileName, function (data) { $.get(localhost + '/nas/music/find/file.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&path=' + fileName, function (data) {
let json = JSON.parse(data); let json = JSON.parse(data);
if(isRandom){
$('#playerNowList').html("播放列表: (全列表随机播放)")
}else{
$('#playerNowList').html("播放列表: (" + playIndex+"/"+musicLib.length+")")
}
$('#title').html("标题:" + json.data.title) $('#title').html("标题:" + json.data.title)
$('#artist').html("艺术家:" + json.data.artist) $('#artist').html("艺术家:" + json.data.artist)
$('#album').html("专辑:" + json.data.album) $('#album').html("专辑:" + json.data.album)