From 91335e4e04c2bfee3f7958014916e2758813f611 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 15 Dec 2023 13:57:43 +0100 Subject: [PATCH] wait #6576 @3 --- js/libs/fluidbook/fluidbook.audioplayer.js | 5 +- js/libs/fluidbook/fluidbook.sound.js | 106 ++++++++++++++++++++- js/libs/fluidbook/fluidbook.stats.js | 1 - js/libs/fluidbook/fluidbook.video.js | 6 ++ 4 files changed, 113 insertions(+), 5 deletions(-) diff --git a/js/libs/fluidbook/fluidbook.audioplayer.js b/js/libs/fluidbook/fluidbook.audioplayer.js index be55b8c9..8ea5443e 100644 --- a/js/libs/fluidbook/fluidbook.audioplayer.js +++ b/js/libs/fluidbook/fluidbook.audioplayer.js @@ -12,10 +12,12 @@ FluidbookAudioPlayer.prototype = { audio.play(); $(audio).addClass('playing'); $(this).addClass('playing'); + $this.fluidbook.sound.pauseAmbient(); } else { audio.pause(); $(audio).removeClass('playing'); $(this).removeClass('playing'); + $this.fluidbook.sound.playAmbientIfNothingIsPlaying(); } return false; }); @@ -63,7 +65,7 @@ FluidbookAudioPlayer.prototype = { }, initTwoStatesIconPlayer: function (player) { - if ($(player).find('.icon').length>0) { + if ($(player).find('.icon').length > 0) { return; } var vp = $(player).next('.visualPlayer'); @@ -83,7 +85,6 @@ FluidbookAudioPlayer.prototype = { $(vp).addClass('playing'); } - vp.append('
'); vp.append('
' + getSpriteIcon('audioplayer-play') + '
'); vp.append('
' + getSpriteIcon('audioplayer-pause') + '
'); diff --git a/js/libs/fluidbook/fluidbook.sound.js b/js/libs/fluidbook/fluidbook.sound.js index 8bde95df..db850437 100644 --- a/js/libs/fluidbook/fluidbook.sound.js +++ b/js/libs/fluidbook/fluidbook.sound.js @@ -1,10 +1,11 @@ function FluidbookSound(fluidbook) { this.fluidbook = fluidbook; + this.ambientEnabled = false; + if (this.fluidbook.settings.soundTheme === 'none' || this.fluidbook.settings.soundTheme == '' || !Modernizr.audio || this.fluidbook.support.iOS || this.fluidbook.support.android) { this.enabled = false; this.on = false; - return; } this.enabled = true; @@ -17,7 +18,19 @@ function FluidbookSound(fluidbook) { this.volume = 100; } this.volume = Math.max(0, Math.min(100, this.volume)) / 100; - console.log(this.volume, this.fluidbook.settings.soundVolume); + + if (this.fluidbook.settings.ambientSound) { + this.ambientEnabled = true; + this.ambientVolume = parseFloat(this.fluidbook.settings.ambientSoundVolume); + if (isNaN(this.ambientVolume)) { + this.ambientVolume = 100; + } + this.ambientVolume = Math.max(0, Math.min(100, this.ambientVolume)) / 100; + this.ambient = new Audio(this.fluidbook.loader.getURL('data/sounds/' + this.fluidbook.settings.ambientSound)); + this.ambient.volume = 0; + this.ambient.loop = true; + this.ambient.preload = 'auto'; + } this.initEvents(); } @@ -37,10 +50,23 @@ FluidbookSound.prototype = { $this.playSoundForPage(data); }); + $(this.fluidbook).on('fluidbook.page.change.end', function (e, page, data) { + setTimeout(function () { + if ($this.isSomethingPlaying()) { + $this.pauseAmbient(); + } else { + $this.playAmbientIfNothingIsPlaying(); + } + }, 1000); + }); + $(document).one(this.fluidbook.input.clickEvent, '*', function () { try { $this.audios['empty'].play(); $this.playing = $this.audios['empty']; + setTimeout(function () { + $this.playAmbient(); + }, 1000); } catch (e) { } return true; @@ -77,6 +103,80 @@ FluidbookSound.prototype = { } }, + playAmbient: function () { + if (!this.ambientEnabled || !this.on) { + return; + } + this.ambient.play(); + gsap.to(this.ambient, 3, { + volume: this.ambientVolume + }); + + }, + + playAmbientIfNothingIsPlaying: function () { + if (!this.ambientEnabled || !this.on) { + return; + } + if (!this.isSomethingPlaying()) { + return this.playAmbient(); + } + }, + + isSomethingPlaying: function () { + let $this = this; + let res = false; + $.each(this.fluidbook.video.getActivePlayers(), function (k, player) { + console.log(player); + if (player.muted()) { + return; + } + if (player.paused()) { + return; + } + if (player.volume() <= 0) { + return; + } + res = true; + return true; + }); + $('audio').each(function () { + let a = $(this).get(0); + if ($this.isAudioPlaying(a)) { + res = true; + return true; + } + }); + return res; + }, + + isAudioPlaying: function (a) { + return a + && a.currentTime > 0 + && !a.paused + && !a.ended + && a.readyState > 2; + }, + + pauseAmbientIfSomethingIsPlaying: function () { + if (!this.on || this.isSomethingPlaying()) { + this.pauseAmbient(); + } + }, + + pauseAmbient: function () { + if (!this.ambientEnabled) { + return; + } + + let $this = this; + gsap.to(this.ambient, 1.5, { + volume: 0, onComplete: function () { + $this.ambient.pause(); + } + }); + }, + toggle: function () { if (this.on) { this.disable(); @@ -87,12 +187,14 @@ FluidbookSound.prototype = { enable: function () { this.on = true; + this.playAmbientIfNothingIsPlaying(); $(".icon-sound-off").hide(); $(".icon-sound-on").show(); }, disable: function () { this.on = false; + this.pauseAmbient(); $(".icon-sound-on").hide(); $(".icon-sound-off").show(); }, diff --git a/js/libs/fluidbook/fluidbook.stats.js b/js/libs/fluidbook/fluidbook.stats.js index d5b2d949..6119d40d 100644 --- a/js/libs/fluidbook/fluidbook.stats.js +++ b/js/libs/fluidbook/fluidbook.stats.js @@ -343,7 +343,6 @@ FluidbookStats.prototype = { } else if (data[0] === 'trackPageView') { window._paq.push(['setCustomUrl', server.lastURL]); } - console.log('push', data, server); window._paq.push(data); }, diff --git a/js/libs/fluidbook/fluidbook.video.js b/js/libs/fluidbook/fluidbook.video.js index 04224bae..75a1db4a 100644 --- a/js/libs/fluidbook/fluidbook.video.js +++ b/js/libs/fluidbook/fluidbook.video.js @@ -311,6 +311,8 @@ FluidbookVideo.prototype = { $.each(hidelinksonplay, function (k, id) { $this.fluidbook.links.hideLinkById(id); }); + + $this.fluidbook.sound.pauseAmbientIfSomethingIsPlaying(); }); player.on('pause', function () { @@ -329,6 +331,8 @@ FluidbookVideo.prototype = { $.each(hidelinksonplay, function (k, id) { $this.fluidbook.links.showLinkById(id); }); + + $this.fluidbook.sound.playAmbientIfNothingIsPlaying(); }); player.on('fullscreenchange', function () { @@ -450,6 +454,8 @@ FluidbookVideo.prototype = { playersToBeRemoved.forEach(function (player) { $this.disposeVideo(player); }); + + this.fluidbook.sound.playAmbientIfNothingIsPlaying(); }, disposeVideo: function (player) { -- 2.39.5