新增播放整个文件夹功能

新增下一首功能
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")
@ResponseBody
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=path.replace(defaultMusicPath+File.separator,"");
JSONObject json=new JSONObject();
JSONObject data = new JSONObject();

View File

@ -29,6 +29,7 @@ public class MusicTools {
}
return tools;
}
private MusicTools() {
scanMusic();
}
@ -76,9 +77,29 @@ public class MusicTools {
}
}
}
public void getPathOrDir(String path, List<MusicData> list) {
File files = new File(path);
for (File file : files.listFiles()) {
if (file.isFile()) {
list.add(getMetadata(file));
} else {
getPathOrDir(file.getAbsolutePath(), list);
}
}
}
public List<MusicData> getPath(String path) {
if(musicMap.containsKey(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)) {
@ -94,11 +115,12 @@ public class MusicTools {
return list;
} else {
if (path.contains(MusicController.defaultMusicPath)) {
List<MusicData> list=new ArrayList<>();
MusicData t2 = new MusicData();
t2.setTitle("返回");
t2.setFile(new File(path).getParentFile());
if (!path.equals(MusicController.defaultMusicPath)) {
list.add(t2);
}
getDirList(path, list);
return list;
}

View File

@ -7,7 +7,6 @@
<title>来点Music~</title>
<link rel="stylesheet" href="/layui/css/layui.css">
</head>
<link href="/css/video-js.min.css" rel="stylesheet">
<body>
<div class="layui-layout layui-layout-admin">
<div id="header"></div>
@ -20,11 +19,13 @@
<img src="/assets/defaultPlayImg.jpg" id="img" style="height: 200px; width: 200px"/>
<div style="display:inline-block;">
<div id="playerNowList">播放列表:</div>
<div id="title">标题:</div>
<div id="artist">艺术家:</div>
<div id="album">专辑:</div>
<div id="composer">作曲家</div>
<a class="layui-btn layui-btn-normal" id="download">下载</a>
<a class="layui-btn layui-btn-normal" id="next">下一首</a>
</div>
<div id="audioWrap"></div>
@ -51,6 +52,7 @@
</script>
<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="play">播放</a>
</script>
<script>
let localhost = "";
@ -59,8 +61,16 @@
let playIndex = 0;
let playNow = ""
$.get("/nas/music/getlocalhost.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk", function (obj) {
try {
let json = JSON.parse(obj);
localhost=json.data;
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 () {
let table = layui.table;
@ -75,7 +85,7 @@
, {field: 'album', title: '专辑', width: 200}
, {field: 'composer', title: '作曲', width: 200}
, {field: 'track', title: '音轨号', width: 100}
, {field: "right", width: 100, toolbar: '#music'}
, {field: "right", width: 150, toolbar: '#music'}
]]
, done: function (res, curr, count) {
musicLib = res.data
@ -107,7 +117,19 @@
if (!data.dir) {
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) {
@ -147,9 +169,16 @@
$('#download').click(function () {
window.open(localhost + "/nas/music/play.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&random=false&filePath=" + playNow)
})
$('#next').click(function (){
playNext()
})
function playNext() {
if(isRandom){
random()
return ;
}
if (playIndex === musicLib.length) {
playIndex = 0;
}
@ -192,6 +221,11 @@
function update(fileName) {
$.get(localhost + '/nas/music/find/file.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&path=' + fileName, function (data) {
let json = JSON.parse(data);
if(isRandom){
$('#playerNowList').html("播放列表: (全列表随机播放)")
}else{
$('#playerNowList').html("播放列表: (" + playIndex+"/"+musicLib.length+")")
}
$('#title').html("标题:" + json.data.title)
$('#artist').html("艺术家:" + json.data.artist)
$('#album').html("专辑:" + json.data.album)