1
Fork 0

fix audio

This commit is contained in:
ashley 2024-08-03 19:03:20 +00:00
parent 2950cfb808
commit fa6a64c5f3

View file

@ -378,8 +378,8 @@ a[data-onclick="jump_to_time"] {
<link href="/css/watch-util.css?v=9893448" rel=stylesheet> <link href="/css/watch-util.css?v=9893448" rel=stylesheet>
<link href="/css/watch-navbar.css?v=9893448" rel=stylesheet> <link href="/css/watch-navbar.css?v=9893448" rel=stylesheet>
<link href="/css/poketube.css?v=948934774844" rel=stylesheet> <link href="/css/poketube.css?v=948934774844" rel=stylesheet>
<link href="https://vjs.zencdn.net/7.15.4/video-js.css" rel="stylesheet" /> <link href="https://vjs.zencdn.net/8.16.0/video-js.css" rel="stylesheet" />
<script src="https://vjs.zencdn.net/7.15.4/video.min.js"></script> <script src="https://vjs.zencdn.net/8.16.0/video.min.js"></script>
<!-- css files end --> <!-- css files end -->
@ -612,7 +612,7 @@ background-color: #0000;
</style> </style>
<script> <script>
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
const video = videojs('video', { const video = videojs('video', {
controls: true, controls: true,
autoplay: false, autoplay: false,
@ -667,32 +667,34 @@ background-color: #0000;
} }
}; };
video.on('play', () => { const syncPlayPause = () => {
if (isVideoBuffered()) { if (video.paused) {
audio.play(); audio.pause();
} else { } else {
video.pause(); audio.play().catch(() => {
video.pause();
});
} }
}); };
video.on('pause', () => { video.on('play', syncPlayPause);
audio.pause(); video.on('pause', syncPlayPause);
});
video.on('timeupdate', () => { video.on('timeupdate', () => {
if (Math.abs(video.currentTime() - audio.currentTime) > 0.3) { if (Math.abs(video.currentTime() - audio.currentTime) > 0.3) {
audio.pause(); // Pause audio if it's not in sync audio.pause(); // Pause audio if it's not in sync
audio.currentTime = video.currentTime(); audio.currentTime = video.currentTime();
audio.play();
} }
}); });
video.on('seeking', handleSeek); video.on('seeking', handleSeek);
video.on('seeked', () => { video.on('seeked', () => {
if (isVideoBuffered()) { if (isVideoBuffered() && checkAudioBuffer()) {
video.play(); video.play();
audio.play();
} }
audio.play(); // Ensure audio is playing after seek
}); });
video.on('volumechange', syncVolume); video.on('volumechange', syncVolume);
@ -704,9 +706,28 @@ background-color: #0000;
audio.pause(); audio.pause();
} }
}); });
// Ensure audio and video are both ready before playing
let videoReady = false;
let audioReady = false;
video.on('canplay', () => {
videoReady = true;
if (videoReady && audioReady) {
video.play();
audio.play();
}
});
audio.addEventListener('canplay', () => {
audioReady = true;
if (videoReady && audioReady) {
video.play();
audio.play();
}
});
} }
}); });
</script> </script>
<% if(dm) { %> <% if(dm) { %>