From ccb1d91a5bed870f04931e5779d5fc70facbff88 Mon Sep 17 00:00:00 2001 From: rainbow napkin Date: Sat, 1 Nov 2025 07:10:40 -0400 Subject: [PATCH] Seperate audio tracks are now *reasonably* synchronized to their videos. This may need more testing to perfect. --- www/js/channel/mediaHandler.js | 46 +++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/www/js/channel/mediaHandler.js b/www/js/channel/mediaHandler.js index 0eb0d4e..b4792af 100644 --- a/www/js/channel/mediaHandler.js +++ b/www/js/channel/mediaHandler.js @@ -401,6 +401,15 @@ class rawFileHandler extends rawFileBase{ //Call derived constructor super(client, player, media, 'raw'); + //Re-sync audio every .1 seconds + this.audioSyncDelta = 1000; + + //Set audio sync tolerance to .3 + this.audioSyncTolerance = .3; + + //Create value to hold last calculated difference between audio and video timestamp + this.lastAudioDelta = 0; + //Define listeners this.defineListeners(); } @@ -428,6 +437,7 @@ class rawFileHandler extends rawFileBase{ //Destroy the audio player this.audio.pause(); this.audio.remove(); + clearInterval(this.audioInterval); } start(){ @@ -461,14 +471,11 @@ class rawFileHandler extends rawFileBase{ //play video this.video.play(); - //if we have an audio src + /*/if we have an audio src if(this.audio.src != ""){ - //Set audio volume - this.audio.volume = this.player.volume; - //Play it too this.audio.play(); - } + }*/ } play(){ @@ -476,22 +483,22 @@ class rawFileHandler extends rawFileBase{ this.video.play(); - //if we have a seperate audio track + /*/if we have a seperate audio track if(this.audio.src != ""){ //Play it too this.audio.play(); - } + }*/ } pause(){ //pause video this.video.pause(); - //if we have a seperate audio track + /*/if we have a seperate audio track if(this.audio.src != ""){ //Pause it too this.audio.pause(); - } + }*/ } sync(timestamp = this.lastTimestamp){ @@ -537,6 +544,7 @@ class rawFileHandler extends rawFileBase{ this.audio.currentTime = this.video.currentTime; //pause it this.audio.pause(); + clearInterval(this.audioInterval); } } @@ -550,18 +558,38 @@ class rawFileHandler extends rawFileBase{ this.audio.currentTime = this.video.currentTime; //pause it this.audio.pause(); + clearInterval(this.audioInterval); } } onPlay(event){ //if we have a seperate audio track if(this.audio != "" && this.video != null){ + //Set audio volume + this.audio.volume = this.player.volume; //Set it's timestamp this.audio.currentTime = this.video.currentTime; //pause it this.audio.play(); + this.audioInterval = setInterval(this.syncAudio.bind(this), this.audioSyncDelta); } } + + syncAudio(){ + //get current audi odelta + const audioDelta = this.video.currentTime - this.audio.currentTime; + + //If the audio is out of sync enough that someone would notice + if(Math.abs(audioDelta) > this.audioSyncTolerance){ + //Set audio volume + this.audio.volume = this.player.volume; + //Re-sync the audio + this.audio.currentTime = this.video.currentTime + audioDelta; + } + + //Set last audio delta + this.lastAudioDelta = audioDelta; + } } /**