Sound
Sound 类允许您在应用程序中使用声音。
在sound中加载声音;获取加载大小;获取id3信息;声音长度;外部 MP3 文件的缓冲状态;
SoundLoaderContext
SoundLoaderContext 类为加载声音的 SWF 文件提供安全检查。
一般作为sound类的load方法参数,load(URL,context);
SoundChannel
SoundChannel 类控制应用程序中的声音。
sound的load方法返回一个soundchannel对象,声音在这个channel中播放;soundChannel对象可以控制左右声道的声量;播放头的位置;以及访问分配给的soundTransform;
SoundTransform
SoundTransform 类包含音量和平移的属性。
比soundchannel更加细节的控制左右声道,如左输入在左右扬声器中分别的音量大小;声音从左到右的平移; volume大小;
SoundMixer
SoundMixer 类包含 SWF 文件中全局声音控件的静态属性和方法。
注意soundMixer是控制全局声音,可以获取当前声音的波形;
例:
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.net.URLRequest;
import flash.utils.Timer;
public class SoundTransformExample extends Sprite {
private var url:String = "MySound.mp3";
private var soundFactory:Sound;
private var channel:SoundChannel;
private var positionTimer:Timer;
public function SoundTransformExample() {
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
var request:URLRequest = new URLRequest(url);
soundFactory = new Sound();
soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
soundFactory.load(request);
channel = soundFactory.play();
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
private function ioErrorHandler(event:Event):void {
trace("ioErrorHandler: " + event);
}
private function setPan(pan:Number):void {
trace("setPan: " + pan.toFixed(2));
var transform:SoundTransform = channel.soundTransform; transform.pan = pan;
channel.soundTransform = transform;
}
private function setVolume(volume:Number):void {
trace("setVolume: " + volume.toFixed(2));
var transform:SoundTransform = channel.soundTransform;
transform.volume = volume;
channel.soundTransform = transform;
}
private function mouseMoveHandler(event:MouseEvent):void {
var halfStage:uint = Math.floor(stage.stageWidth / 2);
var xPos:uint = event.stageX;
var yPos:uint = event.stageY;
var value:Number;
var pan:Number;
if (xPos > halfStage) {
value = xPos / halfStage;
pan = value - 1;
} else if (xPos < halfStage) {
value = (xPos - halfStage) / halfStage;
pan = value;
} else { pan = 0; }
var volume:Number = 1 - (yPos / stage.stageHeight);
setVolume(volume); setPan(pan);
}
}
}