Permalink
Browse files

Added volume support (thanks lorenzos)

  • Loading branch information...
1 parent e286433 commit ecea2bdc19e3a3682290411fa86ad3a33803b908 @kolber committed Apr 11, 2011
Showing with 73 additions and 13 deletions.
  1. +6 −0 audiojs/audio.js
  2. +12 −11 audiojs/audio.min.js
  3. +16 −2 audiojs/audiojs.as
  4. BIN audiojs/audiojs.swf
  5. +39 −0 demos/test6.html
View
@@ -365,6 +365,9 @@
audio.element.ppause();
audio.settings.pause.apply(audio);
}
+ audio['setVolume'] = function(v) {
+ audio.element.setVolume(v);
+ }
audio['loadStarted'] = function() {
// Load the mp3 specified by the audio element into the swf.
audio.swfReady = true;
@@ -650,6 +653,9 @@
this.element.pause();
this.settings.pause.apply(this);
},
+ setVolume: function(v) {
+ this.element.volume = v;
+ },
trackEnded: function(e) {
this.skipTo.apply(this, [0]);
if (!this.settings.loop) this.pause.apply(this);
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -5,6 +5,7 @@ import flash.external.ExternalInterface;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;
+import flash.media.SoundTransform;
import flash.events.Event;
import flash.errors.IOError;
import flash.events.IOErrorEvent;
@@ -22,6 +23,7 @@ public class audiojs extends Sprite {
private var pausePoint:Number = 0;
private var playing:Boolean = false;
+ private var volume:Number = 1;
private var timer:Timer = new Timer(250, 0);
@@ -45,6 +47,7 @@ public class audiojs extends Sprite {
ExternalInterface.addCallback('pplay', play);
ExternalInterface.addCallback('ppause', pause);
ExternalInterface.addCallback('skipTo', skipTo);
+ ExternalInterface.addCallback('setVolume', setVolume);
ExternalInterface.call(this.playerInstance+'loadStarted');
}
@@ -94,8 +97,9 @@ public class audiojs extends Sprite {
private function play():void {
this.channel = this.sound.play(this.pausePoint);
- this.playing = true;
- this.timer.start();
+ this.setVolume(this.volume);
+ this.playing = true;
+ this.timer.start();
}
private function pause():void {
@@ -118,11 +122,21 @@ public class audiojs extends Sprite {
this.pausePoint = this.duration * percent;
if (this.playing) {
this.channel = this.sound.play(this.pausePoint);
+ this.setVolume(this.volume);
} else {
this.updatePlayhead();
}
}
+ private function setVolume(vol:Number):void {
+ this.volume = vol;
+ var transform:SoundTransform = this.channel.soundTransform;
+ if (vol < 0) vol = 0;
+ if (vol > 1) vol = 1;
+ transform.volume = vol;
+ channel.soundTransform = transform;
+ }
+
private function soundEnded(e:Event):void {
ExternalInterface.call(this.playerInstance+'trackEnded');
}
View
Binary file not shown.
View
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>Test 6 - audio.js</title>
+ <style>
+ p { clear: both; }
+ </style>
+ <script src="../audiojs/audio.js"></script>
+ <script>
+ audiojs.events.ready(function() {
+ var as = audiojs.createAll(),
+ audio = as[0],
+ ids = ['vol-0', 'vol-10', 'vol-40', 'vol-70', 'vol-100'];
+ for (var i = 0, ii = ids.length; i < ii; i++) {
+ var elem = document.getElementById(ids[i]),
+ volume = ids[i].split('-')[1];
+ elem.setAttribute('data-volume', volume / 100)
+ elem.onclick = function(e) {
+ audio.setVolume(this.getAttribute('data-volume'));
+ e.preventDefault();
+ return false;
+ }
+ }
+
+ });
+ </script>
+ </head>
+ <body>
+ <h1>audiojs.create(element, settings)</h1>
+ <p>Volume</p>
+ <audio src="http://s3.amazonaws.com/audiojs/02-juicy-r.mp3"></audio>
+ <p><a href="#" id="vol-0">Volume: 0.0</a></p>
+ <p><a href="#" id="vol-10">Volume: 0.1</a></p>
+ <p><a href="#" id="vol-40">Volume: 0.4</a></p>
+ <p><a href="#" id="vol-70">Volume: 0.7</a></p>
+ <p><a href="#" id="vol-100">Volume: 1.0</a></p>
+ </body>
+</html>

0 comments on commit ecea2bd

Please sign in to comment.