mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-08 10:56:15 -08:00
- Created a new documentation file for the danmuku plugin with code examples. - Added a new documentation file for language settings (i18n) with detailed instructions on language imports, default languages, and how to add or modify languages. - Included code snippets for initializing the Artplayer with different language settings.
93 lines
2.2 KiB
JavaScript
93 lines
2.2 KiB
JavaScript
/*!
|
|
* artplayer-plugin-audio-track.js v1.1.0
|
|
* Github: https://github.com/zhw2590582/ArtPlayer
|
|
* (c) 2017-2026 Harvey Zhao
|
|
* Released under the MIT License.
|
|
*/
|
|
function artplayerPluginAudioTrack(option) {
|
|
return (art) => {
|
|
let { url, offset = 0, sync = 0.3 } = option;
|
|
const audio = new Audio();
|
|
audio.preload = "auto";
|
|
if (url) {
|
|
audio.src = url;
|
|
}
|
|
function syncAudio() {
|
|
if (!art.video || !url)
|
|
return;
|
|
const videoTime = art.currentTime;
|
|
const targetTime = videoTime + offset;
|
|
if (Math.abs(audio.currentTime - targetTime) > sync) {
|
|
audio.currentTime = targetTime;
|
|
}
|
|
}
|
|
art.on("play", () => {
|
|
if (!url)
|
|
return;
|
|
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("video:waiting", () => {
|
|
audio.pause();
|
|
});
|
|
art.on("video:playing", () => {
|
|
if (url && art.playing) {
|
|
audio.play().catch((err) => {
|
|
console.warn(err);
|
|
});
|
|
}
|
|
});
|
|
art.on("destroy", () => {
|
|
audio.pause();
|
|
audio.src = "";
|
|
audio.load();
|
|
});
|
|
function update(newOption) {
|
|
if (newOption.url && newOption.url !== url) {
|
|
url = newOption.url;
|
|
audio.src = url;
|
|
if (art.playing) {
|
|
audio.play().catch((err) => console.warn(err));
|
|
}
|
|
}
|
|
if (newOption.offset !== void 0) {
|
|
offset = newOption.offset;
|
|
}
|
|
if (newOption.sync !== void 0) {
|
|
sync = newOption.sync;
|
|
}
|
|
}
|
|
audio.volume = art.volume;
|
|
audio.muted = art.muted;
|
|
audio.playbackRate = art.video?.playbackRate || 1;
|
|
return {
|
|
name: "artplayerPluginAudioTrack",
|
|
audio,
|
|
update
|
|
};
|
|
};
|
|
}
|
|
export {
|
|
artplayerPluginAudioTrack as default
|
|
};
|