]> _ Git - fluidbook-html5.git/commitdiff
wait #6576 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 15 Dec 2023 12:57:43 +0000 (13:57 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 15 Dec 2023 12:57:43 +0000 (13:57 +0100)
js/libs/fluidbook/fluidbook.audioplayer.js
js/libs/fluidbook/fluidbook.sound.js
js/libs/fluidbook/fluidbook.stats.js
js/libs/fluidbook/fluidbook.video.js

index be55b8c942582449b63cab02fc6f8d451e54a649..8ea5443ecccacdc3bab1c4fe5b754ae66e1009a0 100644 (file)
@@ -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('<div class="back"></div>');
         vp.append('<div class="icon play">' + getSpriteIcon('audioplayer-play') + '</div>');
         vp.append('<div class="icon pause">' + getSpriteIcon('audioplayer-pause') + '</div>');
index 8bde95df13d13c88527d409d0ad24886e606d000..db850437adb6f6bcb10d44ad16a1afe7a6a6e731 100644 (file)
@@ -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();
     },
index d5b2d94943f0b41813871c1d35a55f7a4ce3230a..6119d40d5092b80d8f213f9caf59c62170588994 100644 (file)
@@ -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);
     },
 
index 04224bae4a100736151748471b2c4d42d9bf230d..75a1db4a007b31b1ef0f9705fe45ceea422bde5d 100644 (file)
@@ -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) {