[AS3.0编程教学]最全的声音控制方法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
[AS3.0编程教学]最全的声音控制方法
网上做flash音乐播放器的人不少,这个作品主要是对声音的外部读取,然后保存,然后控制播放,暂停,停止等操作,今天这个作品就是向大家展示这些操作的方法。
1.首先我们新建一个文件,在舞台上摆出下面这些按钮,我们今天对这个声音文件的操纵就如按钮所
示:
2. 2
动手之前我们按下Ctrl+Shift+F12,打开ActionScript设置,将“自动申明舞台对象”打钩取消,我们将每个对象自己用Public声明,这样做的好处是开发时每个元件的属性方便引用和提醒。
3. 3
我们新建一个文档类,首先声明舞台上这些按钮,并定义声音变量:testSound,控制变量testChannel,testTrans,testPosition。
publicvarbtnPlay:SimpleButton;
publicvarbtnPause:SimpleButton;
publicvarbtnStop:SimpleButton;
publicvarbtnQuick:SimpleButton;
publicvarbtnVocUp:SimpleButton;
publicvarbtnVocDown:SimpleButton;
publicvarbtnPanUp:SimpleButton;
publicvarbtnPanDown:SimpleButton;
privatevartestSound:Sound;
privatevartestChannel:SoundChannel;
privatevartestPosition:Number=0;
4. 4
首先用下面代码将一首叫做“test.mp3"的音乐加载到舞台。public function TestSoundMain()
{
testSound = new Sound();
testChannel=new SoundChannel();
testTrans = new SoundTransform();
testSound.load(new URLRequest("test.mp3"));
testSound.addEventListener(PLETE,soundLoadOver); }
private function soundLoadOver(e:Event):void
{
testSound.removeEventListener(PLETE, soundLoadOver);
soudLoad = true;
}
5.播放按钮功能。控制音乐播放的按钮,单击后音乐开始播放,并记录音乐的SoundChannel属性。
为了防止连击,我们定义一个isSoundPlay布尔变量判断音乐是否在播放中。
//播放按钮功能
private function playBtnEvent():void
{
btnPlay.addEventListener(MouseEvent.CLICK, soundPlay);
}
private function soundPlay(e:MouseEvent):void
{
if (isSoundPlay) return;
isSoundPlay = true;
}
6.暂停按钮功能,该按钮让音乐暂停掉,为了能继续播放,我们需要记录下此时testChannel的位
置,然后播放按钮单击时可以继续播放
//暂停按钮功能
private function pauseBtnEvent():void
{
btnPause.addEventListener(MouseEvent.CLICK, soudPause);
}
private function soudPause(e:MouseEvent):void
{
if (!isSoundPlay) return;
isSoundPlay = false;
testPosition = testChannel.position;
testChannel.stop();
}
7.停止按钮功能,单击后音乐停止播放,记录位置归0.
//停止按钮功能
private function stopBtnEvent():void
{
btnStop.addEventListener(MouseEvent.CLICK, soundStop);
}
private function soundStop(e:MouseEvent):void
{
isSoundPlay = false;
testPosition = 0
testChannel.stop();
}
8.快进声音。单击该按钮时,我们让声音从当前位置向前播放500毫秒,也就是快进半秒。
//快进按钮功能
private function qucikBtnEvent():void
{
btnQuick.addEventListener(MouseEvent.CLICK, soudQuickPlay);
}
private function soudQuickPlay(e:MouseEvent):void
{
if (!isSoundPlay) return;
testPosition = testChannel.position;
testChannel.stop();
testChannel = testSound.play(testPosition + 500);
}
9.设定声音的音量增加。控制音量就需要soundTransform对象了,它其实是testChanel的
soundTransform属性而已,通过它来控制音量。
//音量增加