]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#678] Adds .mute()/.unmute() proto methods
authorRick Waldron <waldron.rick@gmail.com>
Mon, 8 Aug 2011 15:18:26 +0000 (11:18 -0400)
committerRick Waldron <waldron.rick@gmail.com>
Mon, 8 Aug 2011 15:18:26 +0000 (11:18 -0400)
popcorn.js

index fb6cb938c9483673821095b4d295ba882803b1c7..5e8499959cb0f59bde6dd448a208aad8161931ed 100644 (file)
         // Store track event history data
         history: [],
 
+        // Stores ad-hoc state related data]
+        state: {
+          volume: this.media.volume
+        },
+
         // Store track event object references by trackId
         trackRefs: {},
 
   //  an object with defined methods
   Popcorn.extend(Popcorn.p, (function() {
 
-      var methods = "load play pause currentTime playbackRate mute volume duration preload playbackRate " +
+      var methods = "load play pause currentTime playbackRate volume duration preload playbackRate " +
                     "autoplay loop controls muted buffered readyState seeking paused played seekable ended",
           ret = {};
 
       return this;
     },
 
+    // Mute the calling media, optionally toggle
+    mute: function( toggle ) {
+
+      var event = toggle == null ? "muted" : "unmuted";
+
+      // If `toggle` is explicitly `false`,
+      // unmute the media and restore the volume level
+      if ( toggle === false ) {
+        this.media.muted = false;
+        this.media.volume = this.data.state.volume;
+      }
+
+      // If `toggle` is either null or undefined,
+      // save the current volume and mute the media element
+      if ( toggle == null ) {
+        this.data.state.volume = this.media.volume;
+        this.media.muted = true;
+      }
+
+      // Trigger either muted|unmuted event
+      this.trigger( event );
+
+      return this;
+    },
+
+    // Convenience method, unmute the calling media
+    unmute: function() {
+      return this.mute( false );
+    },
+
     // Get the client bounding box of an instance element
     position: function() {
       return Popcorn.position( this.media );