mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-28 02:01:20 -08:00
feat: add artplayer-plugin-audio-track for audio synchronization with video
- Implemented the audio track plugin for ArtPlayer, allowing audio to be synchronized with video playback. - Added main functionality in `src/index.js` to handle audio playback, synchronization, and event listeners for play, pause, seek, and volume changes. - Created TypeScript definitions for the plugin in `types/artplayer-plugin-audio-track.d.ts`. - Included necessary build files in `dist/` for various module formats (CommonJS, ES Module, and legacy). - Updated package metadata in `package.json` and added a README for documentation. - Added `.npmignore` to exclude unnecessary files from the package.
This commit is contained in:
1 parent
ae03670f9a
commit
a8d36a38ad
85 files changed
+702
-124
No files matched your search
@@ -0,0 +1,76 @@
|
||||
export default function artplayerPluginAudioTrack(option) {
|
||||
return (art) => {
|
||||
const { url, offset = 0, sync = 0.3 } = option
|
||||
|
||||
if (!url) {
|
||||
return {
|
||||
name: 'artplayerPluginAudioTrack',
|
||||
}
|
||||
}
|
||||
|
||||
const audio = new Audio()
|
||||
audio.src = url
|
||||
audio.preload = 'auto'
|
||||
|
||||
function syncAudio() {
|
||||
if (!art.video)
|
||||
return
|
||||
|
||||
const videoTime = art.currentTime
|
||||
const targetTime = videoTime + offset
|
||||
|
||||
if (Math.abs(audio.currentTime - targetTime) > sync) {
|
||||
audio.currentTime = targetTime
|
||||
}
|
||||
}
|
||||
|
||||
art.on('play', () => {
|
||||
syncAudio()
|
||||
audio.play().catch((err) => {
|
||||
console.warn(err)
|
||||
})
|
||||
})
|
||||
|
||||
art.on('pause', () => {
|
||||
audio.pause()
|
||||
})
|
||||
|
||||
art.on('seek', () => {
|
||||
syncAudio()
|
||||
})
|
||||
|
||||
art.on('video:timeupdate', () => {
|
||||
if (art.playing) {
|
||||
syncAudio()
|
||||
}
|
||||
})
|
||||
|
||||
art.on('video:ratechange', () => {
|
||||
audio.playbackRate = art.video.playbackRate
|
||||
})
|
||||
|
||||
art.on('video:volumechange', () => {
|
||||
audio.volume = art.volume
|
||||
audio.muted = art.muted
|
||||
})
|
||||
|
||||
art.on('destroy', () => {
|
||||
audio.pause()
|
||||
audio.src = ''
|
||||
audio.load()
|
||||
})
|
||||
|
||||
audio.volume = art.volume
|
||||
audio.muted = art.muted
|
||||
audio.playbackRate = art.video?.playbackRate || 1
|
||||
|
||||
return {
|
||||
name: 'artplayerPluginAudioTrack',
|
||||
audio,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.artplayerPluginAudioTrack = artplayerPluginAudioTrack
|
||||
}
|
||||
Reference in new issue
Block a user