Flash / Flex / ActionScript/Development/MP3

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

Calculating Spectrum Data

 
package {
    import flash.utils.ByteArray;
    import flash.events.*;
    import flash.net.*;
    import flash.display.*;
    import flash.media.*;
    import flash.geom.*;
    public class Main extends Sprite {
        private const SPECTRUM_WIDTH:int = 256;
        private const BMP_HEIGHT:int = 200;
        private const BMP_WIDTH:int = 256;
        private var sound:Sound = new Sound(new URLRequest("http://www.wbex.ru/y.mp3"));
        private var soundData:ByteArray;
        private var bitmapData:BitmapData;
        private var bitmapDisplay:Bitmap;
        public function Main () {
            sound.play();
            bitmapData = new BitmapData(BMP_WIDTH, BMP_HEIGHT, true, 0x00000000);
            bitmapDisplay = new Bitmap(bitmapData);
            addChild(bitmapDisplay);
            this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        public function onEnterFrame (event:Event):void {
            soundData = new ByteArray();
            SoundMixer.ruputeSpectrum(soundData);
            bitmapData.fillRect(bitmapData.rect, 0xFF000000);
            for (var i:int=0; i < SPECTRUM_WIDTH; i++) {
                var amplitude:Number = soundData.readFloat();
                var ampHeight:Number = BMP_HEIGHT/2 * (amplitude + 1);
                var rect:Rectangle = new Rectangle(i, BMP_HEIGHT -ampHeight, 1, ampHeight);
                bitmapData.fillRect(rect, 0xffffffff);
            }
        }
    }
}



Find Out When a Sound Finishes Playing

 
package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.media.SoundChannel;
    
    public class Main extends Sprite {
        private var _sound:Sound;
        private var _channel:SoundChannel;
        private var _playList:Array;      // the list of songs
        private var _index:int = 0;       // the current song
        public function Main() {
            _playList = ["song1.mp3", 
                        "song2.mp3",
                        "song3.mp3"];
            playNextSong(  );
        }
        
        private function playNextSong(  ):void
        {
            if(_index < _playList.length) {
                _sound = new Sound(  );
                _sound.load(new URLRequest(_playList[_index]));
                _channel = _sound.play(  );
                _channel.addEventListener(Event.SOUND_COMPLETE,onComplete);
                _index++;
            }
        }
        
        public function onComplete(event:Event):void
        {
            playNextSong(  );
        }
    }    
}



ID3 Reader

 
package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.text.TextField;
    
    public class Main extends Sprite {
        private var _sound:Sound;
        
        public function Main(  ) {
            _sound = new Sound(new URLRequest("song.mp3"));
            _sound.addEventListener(Event.ID3, onID3);
            _sound.play(  );
        }
        
        public function onID3(event:Event):void {
            var id3Display:TextField = new TextField(  );
            addChild(id3Display);
            id3Display.x = 10;
            id3Display.y = 20;
            id3Display.width = 200;
            id3Display.height = 200;
            id3Display.background = true;
            id3Display.multiline = true;
            id3Display.wordWrap = true;
            id3Display.text += _sound.id3.songName + "\n";
            id3Display.text += _sound.id3.artist + "\n";
            id3Display.text += _sound.id3.album + "\n";
            id3Display.text += _sound.id3.year + "\n";           
        }
    }
}



Load mp3 file from a URL

 
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 song:Sound = new Sound(new URLRequest("http://server.ru/robotPicksFlowers.mp3"));
        song.play();
    }
  }
}



Load mp3 sound 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(new URLRequest("sound.mp3"));
        sound.play();
    }
  }
}



Reading the Level of a Sound

 
package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.events.Event;
    
    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 leftLevel:Number = _channel.leftPeak * 100;
            var rightLevel:Number = _channel.rightPeak * 100;
            trace(leftLevel);
            trace(rightLevel);
            
        }       
    }
}



Tracking the Progress of a Playing Sound

 
package {
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.events.Event;
    public class ProgressBar2 extends Sprite {
        private var _sound:Sound;
        private var _channel:SoundChannel;
        
        public function ProgressBar2(  ) {
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            _sound = new Sound(new URLRequest("song.mp3"));
            _channel = _sound.play(  );
        }
        
        public function onEnterFrame(event:Event):void
        {
           
            var loaded:int = _sound.bytesLoaded;
            var total:int = _sound.bytesTotal;
            
            var length:int = _sound.length;
            var position:int = _channel.position;
            
            if(total > 0) {
               trace(loaded);
               trace(length);
               trace(position);
            }
        }
    }    
}