mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-04-28 10:57:23 +08:00
✨ feat: 优化播放条和mv播放时没有暂停音乐的问题
This commit is contained in:
@@ -42,7 +42,59 @@ const store = useStore();
|
|||||||
|
|
||||||
const isPlay = computed(() => store.state.isPlay as boolean)
|
const isPlay = computed(() => store.state.isPlay as boolean)
|
||||||
const menus = store.state.menus;
|
const menus = store.state.menus;
|
||||||
|
const play = computed(() => store.state.play as boolean)
|
||||||
|
|
||||||
|
const audio = {
|
||||||
|
value: document.querySelector('#MusicAudio') as HTMLAudioElement
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
// 监听音乐是否播放
|
||||||
|
watch(
|
||||||
|
() => play.value,
|
||||||
|
value => {
|
||||||
|
if (value && audio.value) {
|
||||||
|
audioPlay()
|
||||||
|
} else {
|
||||||
|
audioPause()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
document.onkeyup = (e) => {
|
||||||
|
switch (e.code) {
|
||||||
|
case 'Space':
|
||||||
|
playMusicEvent()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 按下键盘按钮监听
|
||||||
|
document.onkeydown = (e) => {
|
||||||
|
switch (e.code) {
|
||||||
|
case 'Space':
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const audioPlay = () => {
|
||||||
|
if (audio.value) {
|
||||||
|
audio.value.play()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const audioPause = () => {
|
||||||
|
if (audio.value) {
|
||||||
|
audio.value.pause()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const playMusicEvent = async () => {
|
||||||
|
if (play.value) {
|
||||||
|
store.commit('setPlayMusic', false)
|
||||||
|
} else {
|
||||||
|
store.commit('setPlayMusic', true)
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
|||||||
@@ -131,41 +131,6 @@ watch(
|
|||||||
},
|
},
|
||||||
{ immediate: true }
|
{ immediate: true }
|
||||||
)
|
)
|
||||||
// 获取音乐播放Dom
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
onMounted(() => {
|
|
||||||
// 监听音乐是否播放
|
|
||||||
watch(
|
|
||||||
() => play.value,
|
|
||||||
(value, oldValue) => {
|
|
||||||
if (value && audio.value) {
|
|
||||||
audioPlay()
|
|
||||||
onAudio()
|
|
||||||
} else {
|
|
||||||
audioPause()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
// 抬起键盘按钮监听
|
|
||||||
document.onkeyup = (e) => {
|
|
||||||
switch (e.code) {
|
|
||||||
case 'Space':
|
|
||||||
playMusicEvent()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 按下键盘按钮监听
|
|
||||||
document.onkeydown = (e) => {
|
|
||||||
switch (e.code) {
|
|
||||||
case 'Space':
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const audioPlay = () => {
|
const audioPlay = () => {
|
||||||
if (audio.value) {
|
if (audio.value) {
|
||||||
@@ -179,7 +144,6 @@ const audioPause = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 计算属性 获取当前播放时间的进度
|
// 计算属性 获取当前播放时间的进度
|
||||||
const timeSlider = computed({
|
const timeSlider = computed({
|
||||||
get: () => (nowTime.value / allTime.value) * 100,
|
get: () => (nowTime.value / allTime.value) * 100,
|
||||||
@@ -227,6 +191,8 @@ const onAudio = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onAudio()
|
||||||
|
|
||||||
function handleEnded() {
|
function handleEnded() {
|
||||||
store.commit('nextPlay')
|
store.commit('nextPlay')
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -38,7 +38,7 @@ export const formatNumber = (num: any) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const getIsMc = () => {
|
export const getIsMc = () => {
|
||||||
return true
|
return false
|
||||||
}
|
}
|
||||||
const ProxyUrl =
|
const ProxyUrl =
|
||||||
import.meta.env.VITE_API_PROXY + '' || 'http://110.42.251.190:9856'
|
import.meta.env.VITE_API_PROXY + '' || 'http://110.42.251.190:9856'
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ watch(
|
|||||||
|
|
||||||
&-list {
|
&-list {
|
||||||
@apply grid gap-6 pb-28 pr-3;
|
@apply grid gap-6 pb-28 pr-3;
|
||||||
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
|
grid-template-columns: repeat(auto-fill, minmax(13%, 1fr));
|
||||||
}
|
}
|
||||||
&-item {
|
&-item {
|
||||||
&-img {
|
&-img {
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ onMounted(async () => {
|
|||||||
|
|
||||||
const handleShowMv = async (item: IMvItem) => {
|
const handleShowMv = async (item: IMvItem) => {
|
||||||
store.commit('setIsPlay', false)
|
store.commit('setIsPlay', false)
|
||||||
|
store.commit('setPlayMusic', false)
|
||||||
showMv.value = true
|
showMv.value = true
|
||||||
const res = await getMvUrl(item.id)
|
const res = await getMvUrl(item.id)
|
||||||
playMvItem.value = item;
|
playMvItem.value = item;
|
||||||
@@ -75,7 +76,7 @@ const close = () => {
|
|||||||
|
|
||||||
&-content {
|
&-content {
|
||||||
@apply grid gap-6 pb-28 pr-3;
|
@apply grid gap-6 pb-28 pr-3;
|
||||||
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
|
grid-template-columns: repeat(auto-fill, minmax(13%, 1fr));
|
||||||
}
|
}
|
||||||
|
|
||||||
.mv-item {
|
.mv-item {
|
||||||
|
|||||||
Reference in New Issue
Block a user