as3 mp3播放器中波谱显示的实现

package{
import flash.display.Sprite;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray;

/**
* @author zhanghongbo
*/
public class ProgressBar2 extends Sprite {
private var _sound : Sound;
private var _channel : SoundChannel;
private var _sprite : Sprite;

public function ProgressBar2() {

_sprite = new Sprite();
_sprite.y = 20;
_sprite.x = 10;

addChild(_sprite);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
_sound = new Sound(new URLRequest("mp3.mp3"));
_channel = _sound.play();
}

public function onEnterFrame(event : Event) : void {
var barWidth : int = 200;
var barHeight : int = 5;
var loaded : int = _sound.bytesLoaded;
var total : int = _sound.bytesTotal;
var length : int = _sound.length;
var position : int = _channel.position;
// Draw a background bar

graphics.clear();
graphics.beginFill(0xFFFFFF);
graphics.drawRect(10, 10, barWidth, barHeight);
graphics.endFill();
if(total > 0) {
// The percent of the sound that has loaded

var percentBuffered : Number = loaded / total;
// Draw a bar that represents the percent of
// the sound that has loaded

graphics.beginFill(0xCCCCCC);
graphics.drawRect(10, 10, barWidth * percentBuffered, barHeight);
graphics.endFill();
// Correct the sound length calculation

length /= percentBuffered;
// The percent of the sound that has played

var percentPlayed : Number = position / length;
// Draw a bar that represents the percent of
// the sound that has played

graphics.beginFill(0x666666);
graphics.drawRect(10, 10, barWidth * percentPlayed, barHeight);
graphics.endFill();
}


//控制波形
var spectrum : ByteArray = new ByteArray();
SoundMixer.computeSpectrum(spectrum, true);
// Clear the bitmap

_sprite.graphics.clear();
_sprite.graphics.lineStyle(1, 0x336699);
var i : int;
for(i = 0;i < 256;i++) {
var __y : int = 30 + (spectrum.readFloat() * 30 * -1);
if(__y == 30)continue;
_sprite.graphics.moveTo(i, 30);
//_sprite.graphics.drawRect(i, __y, 1, 1);
_sprite.graphics.lineTo(i, __y);
}
}
}
}


唉,先记下吧,有机会再作一个漂亮的播放器.

你可能感兴趣的:(.net,Flash)