changeset 736:e7cf6b9c7944

* getSound now asychronous (and testable!)
author mas01mj
date Wed, 15 Sep 2010 16:23:45 +0000
parents c625f52cf9b4
children 18974af682cf
files bindings/as3/build/as3bridge.swf bindings/as3/src/org/omras2/audiodb/Bridge.as bindings/as3/src/org/omras2/audiodb/SoundEvent.as bindings/as3/src/tests/TestBridge.as
diffstat 4 files changed, 53 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
Binary file bindings/as3/build/as3bridge.swf has changed
--- a/bindings/as3/src/org/omras2/audiodb/Bridge.as	Wed Sep 15 15:09:13 2010 +0000
+++ b/bindings/as3/src/org/omras2/audiodb/Bridge.as	Wed Sep 15 16:23:45 2010 +0000
@@ -116,7 +116,7 @@
 		 * Playback functions
 		 */
 		 
-		public function getSound(uid : String, start : uint, length : uint) : Sound
+		public function getSound(uid : String, start : uint, length : uint) : void
 		{
 			var url : String = _endpoint + "/audio/" + uid;
 			var req : URLRequest = new URLRequest(url);
@@ -125,8 +125,14 @@
 			vars['length'] = length;
 			req.data = vars;
 			var sound : Sound = new Sound();
+			sound.addEventListener(Event.COMPLETE, this.handleSoundLoaded);
 			sound.load(req);
-			return sound;
+		}
+
+		private function handleSoundLoaded(event : Event) : void 
+		{
+			trace("Sound loaded");
+			this.dispatchEvent(new SoundEvent(SoundEvent.COMPLETE, (event.target as Sound)));
 		}
 
 		/**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bindings/as3/src/org/omras2/audiodb/SoundEvent.as	Wed Sep 15 16:23:45 2010 +0000
@@ -0,0 +1,31 @@
+package org.omras2.audiodb 
+{
+	import flash.events.Event;
+	import flash.media.Sound;
+
+	/**
+	 * @author mikej
+	 */
+	public class SoundEvent extends Event 
+	{
+		public static const COMPLETE : String = "complete";
+		private var _sound : Sound;
+		
+		public function SoundEvent(type : String, sound : Sound, bubbles : Boolean = false, cancelable : Boolean = false)
+		{
+			this._sound = sound;
+			super(type, bubbles, cancelable);
+		}
+		
+				
+		public override function clone() : Event
+		{
+			return new SoundEvent(type, _sound, bubbles, cancelable);
+		}
+		
+		public function get sound() : Sound
+		{
+			return _sound;
+		}
+	}
+}
--- a/bindings/as3/src/tests/TestBridge.as	Wed Sep 15 15:09:13 2010 +0000
+++ b/bindings/as3/src/tests/TestBridge.as	Wed Sep 15 16:23:45 2010 +0000
@@ -5,6 +5,7 @@
 	import org.omras2.audiodb.Bridge;
 	import org.omras2.audiodb.LookupEvent;
 	import org.omras2.audiodb.SearchEvent;
+	import org.omras2.audiodb.SoundEvent;
 	import org.omras2.audiodb.model.SearchResult;
 
 	import flash.events.ErrorEvent;
@@ -25,7 +26,7 @@
 		override protected function setUp() : void 
 		{
 			super.setUp();
-			this._bridge = new Bridge("http://harrison.doc.gold.ac.uk:8080");
+			this._bridge = new Bridge("http://127.0.0.1:8080");
 		}
 
 		public function testLookupSuccess() : void
@@ -70,10 +71,18 @@
 		
 		public function testPlay() : void
 		{
-			var sound : Sound = _bridge.getSound("AWAL1000", 10, 5);
-			trace("Length: "+sound.length);
-			trace(sound.bytesLoaded);
-			trace(sound.bytesTotal);
+			
+			var handler : Function = addAsync(handleSoundLoaded, 10000);
+			_bridge.addEventListener(SoundEvent.COMPLETE, handler);
+			_bridge.getSound("AWAL1000", 5, 5);
+			
+		}
+		
+		
+		protected function handleSoundLoaded(event : SoundEvent) : void 
+		{
+			var sound : Sound = event.sound;
+			assertEquals(81083, sound.bytesLoaded);
 		}
 	}
 }