更新页面效果
This commit is contained in:
@@ -32,16 +32,18 @@ export function useAudio() {
|
||||
}
|
||||
|
||||
const playUrl = async (url) => {
|
||||
if (!url) return
|
||||
if (!url) return false
|
||||
const a = ensureAudio()
|
||||
if (a.src !== url) a.src = url
|
||||
activeUrl.value = url
|
||||
try {
|
||||
await a.play()
|
||||
isPlaying.value = true
|
||||
return true
|
||||
} catch (e) {
|
||||
// 被浏览器拦截,等待用户手势
|
||||
isPlaying.value = false
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,12 +68,7 @@ export function useAudio() {
|
||||
// 尝试自动播放,返回是否成功(被浏览器拦截则返回 false,由调用方决定是否弹授权框)
|
||||
const attemptAutoplay = async () => {
|
||||
if (!activeUrl.value) return false
|
||||
try {
|
||||
await playUrl(activeUrl.value)
|
||||
return isPlaying.value
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
return playUrl(activeUrl.value)
|
||||
}
|
||||
|
||||
const pause = () => {
|
||||
@@ -85,9 +82,9 @@ export function useAudio() {
|
||||
|
||||
// 首次交互解锁自动播放
|
||||
const armAutoplay = () => {
|
||||
const handler = () => {
|
||||
play()
|
||||
removeUnlock()
|
||||
const handler = async () => {
|
||||
const ok = await play()
|
||||
if (ok) removeUnlock()
|
||||
}
|
||||
unlockHandler = handler
|
||||
window.addEventListener('pointerdown', handler, { once: true })
|
||||
|
||||
Reference in New Issue
Block a user