提交 bfa67ab9 编写于 作者: Z Zachary

feat: finish most function.

上级 b25ce76d
export const playModeMap = { export const playModeMap = {
sequence: { sequence: {
code: "sequence", code: "sequence",
icon: "sequence", icon: "icn-loop",
name: "顺序播放" name: "循环播放"
}, },
loop: { loop: {
code: "loop", code: "loop",
icon: "loop", icon: "icn-one",
name: "循环播放" name: "单曲循环"
}, },
random: { random: {
code: "random", code: "random",
icon: "random", icon: "icn-shuffle",
name: "随机播放" name: "随机播放"
} }
}; };
...@@ -61,8 +61,8 @@ ...@@ -61,8 +61,8 @@
</span> </span>
</span> </span>
</div> </div>
<div class="m-pbar" data-action="noop" @click="clickBar"> <div class="m-pbar" data-action="noop">
<div class="barbg j-flag"> <div class="barbg j-flag" @click="clickBar">
<div class="rdy" style="width: 0px"></div> <div class="rdy" style="width: 0px"></div>
<div class="cur" :style="'width: ' + barPrecent + '%'"> <div class="cur" :style="'width: ' + barPrecent + '%'">
<span class="btn f-tdn f-alpha" @mousedown="onChangeBar"> <span class="btn f-tdn f-alpha" @mousedown="onChangeBar">
...@@ -93,11 +93,21 @@ ...@@ -93,11 +93,21 @@
> >
</div> </div>
<div class="ctrl f-fl f-pr j-flag"> <div class="ctrl f-fl f-pr j-flag">
<div class="m-vol" style="visibility: hidden"> <div
class="m-vol"
:style="{ visibility: isShowVol ? 'visible' : 'hidden' }"
>
<div class="barbg"></div> <div class="barbg"></div>
<div class="vbg j-t" id="auto-id-L68XOkib47dr55Wt"> <div class="vbg j-t" @click="clickVolBar">
<div class="curr j-t" style="height: 74.4px"></div> <div
<span class="btn f-alpha j-t" style="top: 16.2px"></span> class="curr j-t"
:style="{ height: volumePrecent * 93 + 'px' }"
></div>
<span
class="btn f-alpha j-t"
:style="{ top: (1 - volumePrecent) * 81 + 'px' }"
@mousedown="onChangeVolBar"
></span>
</div> </div>
</div> </div>
<a <a
...@@ -105,13 +115,16 @@ ...@@ -105,13 +115,16 @@
hidefocus="true" hidefocus="true"
data-action="volume" data-action="volume"
class="icn icn-vol" class="icn icn-vol"
@click="clickVolBtn"
></a> ></a>
<a <a
href="javascript:;" href="javascript:;"
hidefocus="true" hidefocus="true"
data-action="mode" data-action="mode"
class="icn icn-loop" class="icn"
title="循环" :class="currentMode.icon"
:title="currentMode.name"
@click="onChangeMode"
></a> ></a>
<span class="add f-pr"> <span class="add f-pr">
<span class="tip" style="display: none">已添加到播放列表</span> <span class="tip" style="display: none">已添加到播放列表</span>
...@@ -121,10 +134,15 @@ ...@@ -121,10 +134,15 @@
hidefocus="true" hidefocus="true"
data-action="panel" data-action="panel"
class="icn icn-list s-fc3" class="icn icn-list s-fc3"
>1 >{{ playlist.length }}
</a> </a>
</span> </span>
<div class="tip tip-1" style="display: none">循环</div> <div
class="tip tip-1"
:style="{ display: isShowModeTip ? '' : 'none' }"
>
{{ currentMode.name }}
</div>
</div> </div>
</div> </div>
</div> </div>
...@@ -140,19 +158,24 @@ ...@@ -140,19 +158,24 @@
<script> <script>
import { isDef, genSongPlayUrl, formatTime } from "common/utils"; import { isDef, genSongPlayUrl, formatTime } from "common/utils";
import { playModeMap } from "common/config";
// import { getSong } from "api"; // import { getSong } from "api";
export default { export default {
data() { data() {
return { return {
isLock: false, isLock: false,
isShowVol: false,
isHoldBtn: false, isHoldBtn: false,
isShowModeTip: false,
songReady: false, songReady: false,
volumePrecent: 1,
volume: 100, volume: 100,
}; };
}, },
mounted() { mounted() {
console.log(this.audio); console.log(this.audio);
this.audio.volume = this.volumePrecent;
}, },
methods: { methods: {
formatTime(duration) { formatTime(duration) {
...@@ -192,15 +215,53 @@ export default { ...@@ -192,15 +215,53 @@ export default {
}, },
timeupdate(e) { timeupdate(e) {
const time = e.target.currentTime; const time = e.target.currentTime;
console.log(time); //console.log(time);
this.$store.commit("music/setCurrentTime", time); this.$store.commit("music/setCurrentTime", time);
}, },
onChangeMode() {
const modeKeys = Object.keys(playModeMap);
const currentModeIndex = modeKeys.findIndex(
(key) => playModeMap[key].code === this.playMode
);
const nextIndex = (currentModeIndex + 1) % modeKeys.length;
const nextModeKey = modeKeys[nextIndex];
const nextMode = playModeMap[nextModeKey];
this.$store.commit("music/setPlayMode", nextMode.code);
this.showModeChangeTip();
},
showModeChangeTip() {
this.isShowModeTip = true;
if (this.ModeTipTimer) clearTimeout(this.ModeTipTimer);
this.ModeTipTimer = setTimeout(() => {
this.isShowModeTip = false;
}, 1000);
},
clickVolBtn() {
this.isShowVol = !this.isShowVol;
},
onChangeVolBar(e) {
let odiv = e.target;
let screenH = document.documentElement.clientHeight;
let percent;
document.onmousemove = (e) => {
percent = (screenH - e.clientY - 44) / 101;
if (percent >= 0 && percent <= 1) this.volumePrecent = percent;
};
document.onmouseup = (e) => {
document.onmousemove = null;
document.onmouseup = null;
};
},
clickVolBar(e) {
let screenH = document.documentElement.clientHeight;
let precent = (screenH - e.clientY - 44) / 101;
this.volumePrecent = precent;
},
onChangeBar(e) { onChangeBar(e) {
this.isHoldBtn = true; this.isHoldBtn = true;
console.log(e); console.log(e);
let odiv = e.target; //获取目标元素 let odiv = e.target; //获取目标元素
console.log(odiv.offsetLeft); console.log(odiv.offsetLeft);
console.log(odiv.style.left);
//算出鼠标相对元素的位置 //算出鼠标相对元素的位置
let disX = e.clientX - odiv.offsetLeft; let disX = e.clientX - odiv.offsetLeft;
console.log(disX); console.log(disX);
...@@ -226,8 +287,7 @@ export default { ...@@ -226,8 +287,7 @@ export default {
}, },
clickBar(e) { clickBar(e) {
if (!this.isHoldBtn) { if (!this.isHoldBtn) {
let percent = (e.clientX - 416) / 493; let percent = e.offsetX / 493;
console.log(percent);
let targetTime = this.currentSong.durationSecond * percent; let targetTime = this.currentSong.durationSecond * percent;
// this.$store.commit('music/setCurrenTime', targetTime) // this.$store.commit('music/setCurrenTime', targetTime)
this.audio.currentTime = targetTime; this.audio.currentTime = targetTime;
...@@ -242,6 +302,9 @@ export default { ...@@ -242,6 +302,9 @@ export default {
playing() { playing() {
return this.$store.state.music.playing; return this.$store.state.music.playing;
}, },
playlist() {
return this.$store.state.music.playlist;
},
hasCurrentSong() { hasCurrentSong() {
return isDef(this.currentSong.id); return isDef(this.currentSong.id);
}, },
...@@ -255,6 +318,12 @@ export default { ...@@ -255,6 +318,12 @@ export default {
console.log(this.$store.getters); console.log(this.$store.getters);
return this.$store.getters["music/nextSong"]; return this.$store.getters["music/nextSong"];
}, },
playMode() {
return this.$store.state.music.playMode;
},
currentMode() {
return playModeMap[this.playMode];
},
currentTime() { currentTime() {
return this.$store.state.music.currentTime; return this.$store.state.music.currentTime;
}, },
...@@ -266,23 +335,36 @@ export default { ...@@ -266,23 +335,36 @@ export default {
watch: { watch: {
currentSong(newSong, oldSong) { currentSong(newSong, oldSong) {
console.log("new", newSong, "old", oldSong); console.log("new", newSong, "old", oldSong);
// 清空歌曲
if (!newSong.id) {
this.audio.pause();
this.audio.currentTime = 0;
return;
}
// 单曲循环 // 单曲循环
if (oldSong && newSong.id === oldSong.id) { if (oldSong && newSong.id === oldSong.id) {
this.setCurrentTime(0); this.$store.commit("music/setCurrentTime", 0);
this.audio.currentTime = 0; this.audio.currentTime = 0;
this.play(); this.play();
return; return;
} }
if (this.playing) { this.songReady = false;
this.play(); if (this.timer) {
clearTimeout(this.timer);
} }
this.timer = setTimeout(() => {
if (this.playing) this.play();
}, 1000);
}, },
playing(newPlaying) { playing(newPlaying) {
console.log("new playing", newPlaying); console.log("new playing", newPlaying);
this.$nextTick(() => (newPlaying ? this.play() : this.pause())); this.$nextTick(() => (newPlaying ? this.play() : this.pause()));
}, },
volumePrecent(newVol) {
this.audio.volume = newVol;
},
}, },
}; };
</script> </script>
...@@ -600,9 +682,15 @@ i { ...@@ -600,9 +682,15 @@ i {
margin: 11px 2px 0 0; margin: 11px 2px 0 0;
text-indent: -9999px; text-indent: -9999px;
} }
.m-playbar .icn-add:hover {
background-position: -88px -189px;
}
.m-playbar .icn-add { .m-playbar .icn-add {
background-position: -88px -163px; background-position: -88px -163px;
} }
.m-playbar .icn-share:hover {
background-position: -114px -189px;
}
.m-playbar .icn-share { .m-playbar .icn-share {
background-position: -114px -163px; background-position: -114px -163px;
} }
...@@ -662,6 +750,9 @@ i { ...@@ -662,6 +750,9 @@ i {
.m-playbar .icn-vol { .m-playbar .icn-vol {
background-position: -2px -248px; background-position: -2px -248px;
} }
.m-playbar .icn-vol:hover {
background-position: -31px -248px;
}
.m-playbar .icn { .m-playbar .icn {
float: left; float: left;
width: 25px; width: 25px;
...@@ -669,9 +760,24 @@ i { ...@@ -669,9 +760,24 @@ i {
margin: 11px 2px 0 0; margin: 11px 2px 0 0;
text-indent: -9999px; text-indent: -9999px;
} }
.m-playbar .icn-loop:hover {
background-position: -33px -344px;
}
.m-playbar .icn-loop { .m-playbar .icn-loop {
background-position: -3px -344px; background-position: -3px -344px;
} }
.m-playbar .icn-one:hover {
background-position: -93px -344px;
}
.m-playbar .icn-one {
background-position: -66px -344px;
}
.m-playbar .icn-shuffle:hover {
background-position: -93px -248px;
}
.m-playbar .icn-shuffle {
background-position: -66px -248px;
}
.m-playbar .add { .m-playbar .add {
float: left; float: left;
width: 59px; width: 59px;
......
import { isDef } from "common/utils"; import { isDef } from "common/utils";
import { playModeMap } from "common/config"; import { playModeMap } from "common/config";
function getRandomIndex(playlist, currentIndex) {
// 防止无限循环
if (playlist.length === 1) {
return currentIndex;
}
let index = Math.round(Math.random() * (playlist.length - 1));
if (index === currentIndex) {
index = getRandomIndex(playlist, currentIndex);
}
return index;
}
export default { export default {
currentIndex(state) { currentIndex(state) {
const { currentSong, playlist } = state; const { currentSong, playlist } = state;
return playlist.findIndex(({ id }) => id === currentSong.id); return playlist.findIndex(({ id }) => id === currentSong.id);
}, },
getRandomIndex(playlist, currentIndex) {
// 防止无限循环
if (playlist.length === 1) {
return currentIndex;
}
let index = Math.round(Math.random() * (playlist.length - 1));
if (index === currentIndex) {
index = getRandomIndex(playlist, currentIndex);
}
return index;
},
nextSong(state, getters) { nextSong(state, getters) {
const { playlist, playMode } = state; const { playlist, playMode } = state;
const nextStratMap = { const nextStratMap = {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册