Flash / Flex / ActionScript/Development/Sound

Материал из Web эксперт
Перейти к: навигация, поиск

Applying Sound Transformations

 
package {
    import flash.display.*;
    import flash.media.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    public class Main extends Sprite {
        private var _id3Display:TextField = new TextField();
        private static const SONG_URL:String ="http://www.wbex.ru/y.mp3";
        public function Main () {
            _id3Display.width = 300;
            _id3Display.height = 400;
            addChild(_id3Display);
            var sound:Sound = new Sound ();
            sound.addEventListener(Event.ID3, onID3);
            sound.load(new URLRequest(SONG_URL));
            sound.play();
        }
        private function onID3(event:Event):void {
            var sound:Sound = event.target as Sound;
            _id3Display.text = sound.id3.songName + "\n";
            _id3Display.appendText("by "+ sound.id3.artist + "\n");
            _id3Display.appendText("from the album "+ sound.id3.album);
        }
    }
}



Buffering a Streaming Sound

 
package{
    import flash.display.Sprite;
    import flash.media.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
  public class Main extends Sprite{
    public function Main(){
        var context:SoundLoaderContext = new SoundLoaderContext();
        context.bufferTime = 10000; // set buffer time to 10 seconds.
        var song:Sound = new Sound(new URLRequest("http://losdesigns.ru/music/robotPicksFlowers.mp3"), context);
        song.play();
    }
  }
}



Controlling Playback of a Sound

 
package{
  import flash.display.Sprite;
      import flash.media.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import flash.utils.*;
  public class Main extends Sprite{
    public function Main(){
        var jazz:Sound = new Sound(new URLRequest("My.mp3"));
        var jazzChannel:SoundChannel = jazz.play(); // sound is playing
        var timer:Timer = new Timer(5000, 1);
        timer.addEventListener(TimerEvent.TIMER, onTimeout);
        timer.start();
        function onTimeout() {
            jazzChannel.stop(); // sound stops playing
        }
    }
  }
}



Getting the Size of a Sound File

 
//ProgressBar
package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.events.Event;
    
    public class Main extends Sprite {
        private var _sound:Sound;
        var i:int = 1;
        public function Main(  ) {
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        public function onEnterFrame(event:Event):void
        {
            var barWidth:int = 200;
            var barHeight:int = 5;
            var loaded:int = i++;
            var total:int = 100;
            if(total > 0) {
                graphics.clear(  );
                graphics.beginFill(0xFFFFFF);
                graphics.drawRect(10, 10, barWidth, barHeight);
                graphics.endFill(  );
                var percent:Number = loaded / total;
                graphics.beginFill(0xCCCCCC);
                graphics.drawRect(10, 10, barWidth * percent, barHeight);
                graphics.endFill(  );
            }
        }
    }    
}



How Sound Works in AS3: assume that there is an MP3 file with the name sound.mp3 stored in the same folder as the SWF file.

 
package{
  import flash.display.Sprite;
      import flash.media.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
  public class Main extends Sprite{
    public function Main(){
        var sound:Sound = new Sound();
        var url:String = "sound.mp3";
        var urlRequest:URLRequest = new URLRequest(url);
        sound.load(urlRequest);
    }
  }
}



Offsetting the Start of a Sound

 
package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.net.URLRequest;
    
    public class Main extends Sprite {
        private var _sound:Sound;
        private var _cuePoints:Array;
        
        public function Main(  ) {
            _cuePoints = [0, 10000, 30000, 68000, 120000];
            _sound = new Sound(new URLRequest("song.mp3"));
            playCuePoint(2);
        }
        
        public function playCuePoint(index:int):void {
            _sound.play(_cuePoints[index]);
        }
        
    }    
}



Pausing and Restarting a Sound

 
package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    
    public class PlayPause extends Sprite {
        private var _sound:Sound;
        private var _channel:SoundChannel;
        private var _playPauseButton:Sprite;
        private var _playing:Boolean = false;
        private var _position:int;
        
        public function PlayPause(  ) {
            _sound = new Sound(new URLRequest("song.mp3"));
            _channel = _sound.play(  );
            _playing = true;
            _playPauseButton = new Sprite(  );
            addChild(_playPauseButton);
            _playPauseButton.x = 10;
            _playPauseButton.y = 20;
            _playPauseButton.graphics.beginFill(0xcccccc);
            _playPauseButton.graphics.drawRect(0, 0, 20, 20);
            _playPauseButton.addEventListener(MouseEvent.MOUSE_UP, 
                                             onPlayPause);
        }
        
        public function onPlayPause(event:MouseEvent):void {
            if(_playing) {
                   _position = _channel.position;
                   _channel.stop(  );
            }
            else {
                // If not playing, re-start it at
                // last known position
                _channel = _sound.play(_position);
            }
               _playing = !_playing;
        }
    }
}



Reading the Sound Spectrum

 
package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
    
        
    public class Main extends Sprite {
        private var _sound:Sound;
        private var _channel:SoundChannel;
        public function Main(  ) {
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            _sound = new Sound(new URLRequest("song.mp3"));
            _channel = _sound.play(  );
        }
        
        public function onEnterFrame(event:Event):void
        {
            var spectrum:ByteArray = new ByteArray(  );
            flash.media.SoundMixer.ruputeSpectrum(spectrum);
            
           
            for(var i:int=0;i<256;i++) {
                trace(spectrum.readFloat());
            }
            
        }       
    }
}