// Richard Caceres, rcaceres {at} ucla.edu // DMA 199 // $Id: HelloSound.as,v 381.1 2008-02-07 01:06:50-08 - - $ // $Date: 2008-02-07 01:06:50-08 $ package { import flash.display.Sprite; import flash.events.*; import flash.media.Sound; import flash.media.SoundChannel; import flash.net.URLRequest; public class HelloSound extends Sprite { private var urls:Array; private var boxsprites:Array; private var song:SoundChannel; private var curr_sound_index = 0; private var boxspr:Sprite; public function HelloSound() { urls = ["mbira01.mp3", "mbira02.mp3", "mbira03.mp3", "mbira04.mp3", "mbira05.mp3", "mbira06.mp3", "mbira07.mp3", "mbira08.mp3", "mbira09.mp3", "mbira10.mp3", "mbira11.mp3"]; //loadAllSou // create a sprite for each url boxsprites = new Array(); for (var i = 0; i < urls.length; i++){ var tmpspri = new Sprite(); tmpspri.x = i * 30; tmpspri.y = stage.stageHeight / 2 - 10; boxsprites.push (tmpspri); addChild (tmpspri); } loadAndPlayNewSound (urls[curr_sound_index]); } public function handleRollOver (e:MouseEvent) { trace ("handleRollOver"); trace ("e:"+e); } public function onEnterFrameHandler (e:Event) { //trace ("onEnterFrameHandler"); var tmpspr = boxsprites[curr_sound_index]; tmpspr.graphics.clear(); tmpspr.graphics.beginFill(0x000000); tmpspr.graphics.drawRect(0,0, 20 * (1 + song.leftPeak) , 20 * (1 + song.leftPeak)); tmpspr.graphics.endFill(); } private function completeHandler(event:Event):void { trace("completeHandler: " + event); } private function id3Handler(event:Event):void { trace("id3Handler: " + event); } private function ioErrorHandler(event:Event):void { trace("ioErrorHandler: " + event); } private function progressHandler(event:ProgressEvent):void { trace("progressHandler: " + event); } public function loadAndPlayNewSound (_url):void { var request:URLRequest = new URLRequest(_url); var soundFactory:Sound = new Sound(); soundFactory.addEventListener(Event.COMPLETE, completeHandler); soundFactory.addEventListener(Event.ID3, id3Handler); soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler); soundFactory.load(request); song = soundFactory.play(); // Poke around with SoundChannel class addEventListener (Event.ENTER_FRAME, onEnterFrameHandler); song.addEventListener (Event.SOUND_COMPLETE, soundCompleteHandler); } public function soundCompleteHandler (e:Event) { curr_sound_index++; curr_sound_index %= urls.length; removeEventListener (Event.ENTER_FRAME, onEnterFrameHandler); loadAndPlayNewSound (urls[curr_sound_index]); } } // Class public class Sound // Inheritance Sound Inheritance EventDispatcher Inheritance Object // Subclasses SoundAsset // // The Sound class lets you work with sound in an application. The Sound class lets you create a new Sound object, load and play an external MP3 file into that object, close the sound stream, and access data about the sound, such as information about the number of bytes in the stream and ID3 metadata. More detailed control of the sound is performed through the sound source — the SoundChannel or Microphone object for the sound — and through the properties in the SoundTransform class that control the output of the sound to the computer's speakers. // // To control sounds that are embedded in a SWF file, use the properties in the SoundMixer class. // // ------------- // Class public final class SoundChannel // Inheritance SoundChannel Inheritance EventDispatcher Inheritance Object // // The SoundChannel class controls a sound in an application. Every sound is assigned // to a sound channel, and the application can have multiple sound channels that are mixed // together. The SoundChannel class contains a stop() method, properties for monitoring the // amplitude (volume) of the channel, and a property for assigning a SoundTransform object // to the channel. // // ----------- // // Class public final class SoundTransform // Inheritance SoundTransform Inheritance Object // // The SoundTransform class contains properties for volume and panning. }