Flash ActionScript3.0後
已經可以抓取到mp3里的音頻資訊
這樣就可以製作出音頻跳動效果
使用的是SoundMixer類別。

程式碼:
已經可以抓取到mp3里的音頻資訊
這樣就可以製作出音頻跳動效果
使用的是SoundMixer類別。

程式碼:
package
{
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.media.SoundTransform;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.utils.Timer;
import flash.text.TextField;
import com.bit101.components.PushButton;
[SWF(width = "400", height = "300", frameRate = "31", backgroundColor = "#000000", pageTitle = "milkmidi")]
public class MySoundDemo extends Sprite
{
private var _bArray :ByteArray = new ByteArray();
private var _txt :TextField = new TextField();
private var _position :Number = 0;
private var _sound :Sound = new Sound();
private var _channel :SoundChannel;
public function MySoundDemo() {
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
//設定場景
_sound.addEventListener(Event.COMPLETE, completeHandler);
//載入完成事件
_sound.addEventListener(ProgressEvent.PROGRESS, progressHandler);
//載入進度
_sound.load(new URLRequest("MySound.mp3"));
//載入mp3
_channel = _sound.play();
//播放sound
_channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
//偵聽歌曲是否播放完畢。
_txt.width = 200;
_txt.textColor = 0xFFFFFF;
//文字物件
this.addEventListener(Event.ENTER_FRAME, soundEnterFrame);
//EnterFrame事件
addChild(_txt);
var playBtn:PushButton = new PushButton(this, 0, 20, "PLAY", onPlayClick);
var stopBtn:PushButton = new PushButton(this, 0, 40, "STOP", onStopClick);
var pauseBtn:PushButton = new PushButton(this, 0, 60, "PAUSE", onPauseClick);
//建立Button,使用的是bit101作者的類別。
}
private function onPauseClick(e:MouseEvent):void {
_position = _channel.position;
//將現在播放的位置記錄在_position變數里。
_channel.stop();
//停止。
}
private function onStopClick(e:MouseEvent):void {
_position = 0;
_channel.stop();
}
private function onPlayClick(e:MouseEvent):void {
_channel = _sound.play(_position);
//從記錄的位置開始播放。
}
private function soundCompleteHandler(e:Event):void {
trace("soundCompleteHandler " + e);
//播放完畢,可以選擇再重新播放一次
}
private function soundEnterFrame(e:Event):void {
_txt.text = "positionTimer: " + int(_channel.position/1000) +" / " + int(_sound.length/1000);
//將播放的進度顯示在動態文字裡。
var n:Number = 0;
this.graphics.clear();
SoundMixer.computeSpectrum(_bArray, true, 0);
//SoundMixer可以得到目前.swf裡音頻的資訊。
for (var i:int = 0; i < 256; i = i + 16) {
n = _bArray.readFloat();
//讀取ByteArray。
var num:Number = n * 360;
//將數值放大
this.graphics.lineStyle(10, 0x99FF00, 100, false, "noSacle", "none");
this.graphics.moveTo(50 + i, 200);
this.graphics.lineTo(50 + i, 200 - num / 5);
//繪圖。
}
}
private function completeHandler(e:Event):void {
trace("completeHandler: " + e);
//載入完成
}
private function progressHandler(e:ProgressEvent):void {
trace("progressHandler: " + e);
//載入中
}
}
}
留言
程式碼要寫在哪邊?
把該as和.fla放在一起
開啟flash, 在下方的Document Class填寫類別名稱,發怖