Created
September 16, 2011 15:54
-
-
Save joona/1222422 to your computer and use it in GitHub Desktop.
Simple ExternalInterface Sound Player interface to be used with JavaScript to play Music and UI sounds on a web site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package | |
| { | |
| import flash.events.*; | |
| import flash.media.Sound; | |
| import flash.media.SoundChannel; | |
| import flash.media.SoundTransform; | |
| import flash.utils.Timer; | |
| import flash.external.ExternalInterface; | |
| import flash.net.URLRequest; | |
| import flash.display.Sprite; | |
| public class EASounds extends Sprite { | |
| private var sc:SoundChannel; | |
| private var st:SoundTransform; | |
| private var sr:Boolean; | |
| private var sound:Sound; | |
| private var timer:Timer; | |
| private var endPoint:uint; | |
| private var tc:SoundChannel; | |
| private var tt:SoundTransform; | |
| private var track:Sound; | |
| private var trackPlaying:Boolean = false; | |
| private var trackPausePoint:Number = 0.00; | |
| private var url:String; | |
| public function EASounds():void { | |
| st = new SoundTransform(0, 0); | |
| st.volume = 0.7; | |
| tt = new SoundTransfrom(0, 0); | |
| tt.volume = 0.5; | |
| ExternalInterface.addCallback("playSound",playSound); | |
| ExternalInterface.addCallback("loadSound",loadSound); | |
| ExternalInterface.addCallback("stopSound",stopSound); | |
| ExternalInterface.addCallback("playTrack",playTrack); | |
| ExternalInterface.addCallback("pauseTrack",pauseTrack); | |
| ExternalInterface.addCallback("stopTrack",stopTrack); | |
| ExternalInterface.addCallback("adjustVolume",adjustVolume); | |
| ExternalInterface.call("EASoundsInitialized"); | |
| } | |
| private function loadSound(url:String):void { | |
| // Create a new URLRequest for the mp3 file | |
| var urlRequest:URLRequest = new URLRequest(url); | |
| sr = false; | |
| // Try to stop the sound channel if it already | |
| // playing any audio. | |
| try { sc.stop(); } | |
| catch(e:Error) { } | |
| sound = new Sound(); | |
| sound.addEventListener(Event.COMPLETE, onSoundLoaded); | |
| sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); | |
| sound.addEventListener(ProgressEvent.PROGRESS, soundProgressHandler); | |
| sound.load(urlRequest); | |
| } | |
| private function onSoundLoaded(event:Event):void { | |
| var sound:Sound = event.target as Sound; | |
| ExternalInterface.call("EASoundsSoundLoaded", url); | |
| sr = true; | |
| //playSound(); | |
| } | |
| private function playSoundSnippet(start:uint=0, end:uint=0):void { | |
| if (!sr) { | |
| soundNotReady(); | |
| } | |
| sc = sound.play(start); | |
| sc.soundTransform = st; | |
| if (end == 0) { | |
| endPoint = sound.length; | |
| } else { | |
| endPoint = end | |
| } | |
| enableTimer(); | |
| } | |
| private function playSound():void { | |
| sc = sound.play(start); | |
| sc.soundTransform = st; | |
| sc.addEventListener(Event.SOUND_COMPLETE, onSoundComplete); | |
| } | |
| private function enableTimer():void { | |
| timer = new Timer(20); | |
| timer.addEventListener(TimerEvent.TIMER, timerHandler); | |
| timer.start(); | |
| } | |
| private function timerHandler(event:TimerEvent):void { | |
| if(uint(sc.position.toFixed(2)) >= endPoint) { | |
| timer.stop(); | |
| sc.stop(); | |
| soundComplete(); | |
| return; | |
| } | |
| } | |
| private function soundComplete():void { | |
| ExternalInterface.call("EASoundsSoundComplete"); | |
| } | |
| private function soundNotReady():void { | |
| ExternalInterface.call("EASoundsSoundNotReady"); | |
| } | |
| private function ioErrorHandler(event:Event):void { | |
| ExternalInterface.call("EASoundsError", event); | |
| } | |
| private function soundProggressHandler(event:ProgressEvent):void { | |
| ExternalInterface.call("EASoundsSoundProggress", event); | |
| } | |
| private function onTrackComplete(event:Event):void { | |
| event.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete); | |
| soundComplete(); | |
| } | |
| private function playTrack(url:String=false):void { | |
| // Try to stop the sound channel if it already | |
| // playing any audio. | |
| try { tc.stop(); } | |
| catch(e:Error) { } | |
| // if url specified | |
| if (url) { | |
| var urlRequest:URLRequest = new URLRequest(url); | |
| track = new Sound(); | |
| track.addEventListener(Event.COMPLETE, onTrackLoaded); | |
| track.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); | |
| track.addEventListener(ProgressEvent.PROGRESS, trackProgressHandler); | |
| track.load(urlRequest); | |
| } | |
| // play track | |
| tc = track.play(trackPause); | |
| tc.soundTransform = tt; | |
| tc.addEventListener(Event.SOUND_COMPLETE, onTrackComplete); | |
| trackPlaying = true; | |
| } | |
| private function onTrackComplete(event:Event):void { | |
| event.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onTrackComplete); | |
| ExternalInterface.call("EASoundsTrackComplete"); | |
| trackPlaying = false; | |
| trackPausePoint = 0.00; | |
| } | |
| private function pauseTrack():void { | |
| trackPausePoint = tc.position; | |
| tc.stop(); | |
| trackPlaying = false; | |
| } | |
| private function stopTrack():void { | |
| pauseTrack(); | |
| trackPausePoint = 0.00; | |
| } | |
| private function stopSound():void { | |
| // Try to stop the sound if it is already playing. | |
| try { sc.stop(); } catch(e:Error) { } | |
| } | |
| private function adjustVolume(volume:Number):void { | |
| // Try to stop the sound if it is already playing. | |
| try { st.volume = volume; } catch(e:Error) { } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment