mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-25 16:11: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
+11
-7
@@ -1,10 +1,14 @@
|
||||
import type Artplayer from 'artplayer'
|
||||
import type { Option, RuntimeResult, UpdateOption } from '../types/artplayer-plugin-audio-track'
|
||||
import { createAudioTrack } from './track'
|
||||
|
||||
export default function artplayerPluginAudioTrack(option) {
|
||||
return (art) => {
|
||||
type AudioEvent = 'play' | 'pause' | 'seek' | 'destroy' | 'video:pause' | 'video:ended' | 'video:waiting' | 'video:emptied' | 'video:seeking' | 'video:seeked' | 'video:timeupdate' | 'video:ratechange' | 'video:volumechange' | 'video:playing'
|
||||
|
||||
export default function artplayerPluginAudioTrack(option: Option) {
|
||||
return (art: Artplayer): RuntimeResult => {
|
||||
const track = createAudioTrack(option)
|
||||
const { audio } = track
|
||||
const subscriptions = []
|
||||
const subscriptions: [AudioEvent, () => void][] = []
|
||||
let active = true
|
||||
|
||||
function syncAudio() {
|
||||
@@ -12,7 +16,7 @@ export default function artplayerPluginAudioTrack(option) {
|
||||
track.sync(art.currentTime)
|
||||
}
|
||||
|
||||
function listen(event, callback) {
|
||||
function listen(event: AudioEvent, callback: () => void) {
|
||||
const listener = () => {
|
||||
if (active)
|
||||
callback()
|
||||
@@ -25,7 +29,7 @@ export default function artplayerPluginAudioTrack(option) {
|
||||
if (!active)
|
||||
return
|
||||
active = false
|
||||
const failures = []
|
||||
const failures: unknown[] = []
|
||||
for (const [event, listener] of subscriptions.splice(0)) {
|
||||
try {
|
||||
art.off(event, listener)
|
||||
@@ -45,7 +49,7 @@ export default function artplayerPluginAudioTrack(option) {
|
||||
syncAudio()
|
||||
track.play()
|
||||
})
|
||||
for (const event of ['pause', 'video:pause', 'video:ended', 'video:waiting', 'video:emptied', 'video:seeking'])
|
||||
for (const event of ['pause', 'video:pause', 'video:ended', 'video:waiting', 'video:emptied', 'video:seeking'] as const)
|
||||
listen(event, track.pause)
|
||||
listen('seek', syncAudio)
|
||||
listen('video:seeked', () => {
|
||||
@@ -86,7 +90,7 @@ export default function artplayerPluginAudioTrack(option) {
|
||||
return {
|
||||
name: 'artplayerPluginAudioTrack',
|
||||
audio,
|
||||
update(newOption) {
|
||||
update(newOption: UpdateOption) {
|
||||
if (active)
|
||||
track.update(newOption, art.playing)
|
||||
},
|
||||
+7
-5
@@ -1,4 +1,6 @@
|
||||
export function createAudioTrack(option) {
|
||||
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
|
||||
@@ -6,7 +8,7 @@ export function createAudioTrack(option) {
|
||||
function play() {
|
||||
if (closed || !url)
|
||||
return
|
||||
audio.play().catch((error) => {
|
||||
audio.play().catch((error: unknown) => {
|
||||
if (!closed)
|
||||
console.warn(error)
|
||||
})
|
||||
@@ -21,7 +23,7 @@ export function createAudioTrack(option) {
|
||||
if (closed)
|
||||
return
|
||||
closed = true
|
||||
const failures = []
|
||||
const failures: unknown[] = []
|
||||
for (const release of [() => audio.pause(), () => audio.removeAttribute('src'), () => audio.load()]) {
|
||||
try {
|
||||
release()
|
||||
@@ -50,14 +52,14 @@ export function createAudioTrack(option) {
|
||||
play,
|
||||
pause,
|
||||
destroy,
|
||||
sync(time) {
|
||||
sync(time: number) {
|
||||
if (closed || !url)
|
||||
return
|
||||
const target = time + offset
|
||||
if (Math.abs(audio.currentTime - target) > sync)
|
||||
audio.currentTime = target
|
||||
},
|
||||
update(newOption, playing) {
|
||||
update(newOption: UpdateOption, playing: boolean) {
|
||||
if (closed)
|
||||
return
|
||||
if (newOption.url && newOption.url !== url) {
|
||||
Reference in new issue
Block a user