mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-28 02:01:20 -08:00
refactor(audio): [PKG-AUDIO-04] migrate strict source and compatible type entries
This commit is contained in:
1 parent
9c07ea830c
commit
13c898c149
31 files changed
+18923
-90
No files matched your search
@@ -0,0 +1,77 @@
|
||||
import type { Option, UpdateOption } from '../types/artplayer-plugin-audio-track'
|
||||
|
||||
export function createAudioTrack(option: Option) {
|
||||
let { url, offset = 0, sync = 0.3 } = option
|
||||
const audio = new Audio()
|
||||
let closed = false
|
||||
|
||||
function play() {
|
||||
if (closed || !url)
|
||||
return
|
||||
audio.play().catch((error: unknown) => {
|
||||
if (!closed)
|
||||
console.warn(error)
|
||||
})
|
||||
}
|
||||
|
||||
function pause() {
|
||||
if (!closed)
|
||||
audio.pause()
|
||||
}
|
||||
|
||||
function destroy() {
|
||||
if (closed)
|
||||
return
|
||||
closed = true
|
||||
const failures: unknown[] = []
|
||||
for (const release of [() => audio.pause(), () => audio.removeAttribute('src'), () => audio.load()]) {
|
||||
try {
|
||||
release()
|
||||
}
|
||||
catch (error) { failures.push(error) }
|
||||
}
|
||||
if (failures.length)
|
||||
throw failures[0]
|
||||
}
|
||||
|
||||
try {
|
||||
audio.preload = 'auto'
|
||||
if (url)
|
||||
audio.src = url
|
||||
}
|
||||
catch (error) {
|
||||
try {
|
||||
destroy()
|
||||
}
|
||||
catch {}
|
||||
throw error
|
||||
}
|
||||
|
||||
return {
|
||||
audio,
|
||||
play,
|
||||
pause,
|
||||
destroy,
|
||||
sync(time: number) {
|
||||
if (closed || !url)
|
||||
return
|
||||
const target = time + offset
|
||||
if (Math.abs(audio.currentTime - target) > sync)
|
||||
audio.currentTime = target
|
||||
},
|
||||
update(newOption: UpdateOption, playing: boolean) {
|
||||
if (closed)
|
||||
return
|
||||
if (newOption.url && newOption.url !== url) {
|
||||
url = newOption.url
|
||||
audio.src = url
|
||||
if (playing)
|
||||
play()
|
||||
}
|
||||
if (newOption.offset !== undefined)
|
||||
offset = newOption.offset
|
||||
if (newOption.sync !== undefined)
|
||||
sync = newOption.sync
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user