[AS3.0编程教学]最全的声音控制方法
[AS3.0编程教学]最全的声音限制方法 网上做flash音乐播放器的人不少,这个作品主要是对声音的外部读取,然后保存,然后限制播放,暂停,停止等操作,今日这个作品就是向大家展示这些操作的方法。 1. 首先我们新建一个文件,在舞台上摆出下面这些按钮,我们今日对这个声音文件的操纵就如按钮所示 动手之前我们按下CtrlShiftF12,打开ActionScript设置,将“自动申明舞台对象”打钩取消,我们将每个对象自己用Public声明,这样做的好处是开发时每个元件的属性便利引用和提示。 2. 3 我们新建一个文档类,首先声明舞台上这些按钮,并定义声音变量testSound,限制变量testChannel,testTrans,testPosition。 public var btnPlaySimpleButton; public var btnPauseSimpleButton; public var btnStopSimpleButton; public var btnQuickSimpleButton; public var btnVocUpSimpleButton; public var btnVocDownSimpleButton; public var btnPanUpSimpleButton; public var btnPanDownSimpleButton; private var testSoundSound; private var testChannelSoundChannel; private var testTransSoundTrans; private var testPositionNumber0; 3. 4 首先用下面代码将一首叫做“test.mp3的音乐加载到舞台。 public function TestSoundMain{ testSound new Sound; testChannelnew SoundChannel; testTrans new SoundTrans; testSound.loadnew URLRequesttest.mp3; testSound.addEventListenerEvent.COMPLETE,soundLoadOver;} private function soundLoadOvereEventvoid{ testSound.removeEventListenerEvent.COMPLETE, soundLoadOver; soudLoad true;} 4. 播放按钮功能。限制音乐播放的按钮,单击后音乐起先播放,并记录音乐的SoundChannel属性。为了防止连击,我们定义一个isSoundPlay布尔变量推断音乐是否在播放中。 //播放按钮功能 private function playBtnEventvoid{ btnPlay.addEventListenerMouseEvent.CLICK, soundPlay;} private function soundPlayeMouseEventvoid{ if isSoundPlay return; isSoundPlay true; testChannel testSound.playtestPosition;} 5. 暂停 按钮功能,该按钮让音乐暂停掉,为了能接着播放,我们须要记录下此时testChannel的位置,然后播放按钮单击时可以接着播放 //暂停按钮功能 private function pauseBtnEventvoid{ btnPause.addEventListenerMouseEvent.CLICK, soudPause;} private function soudPauseeMouseEventvoid{ if isSoundPlay return; isSoundPlay false; testPosition testChannel.position; testChannel.stop;} 6. 停止按钮功能,单击后音乐停止播放,记录位置归0. //停止按钮功能 private function stopBtnEventvoid{ btnStop.addEventListenerMouseEvent.CLICK, soundStop;} private function soundStopeMouseEventvoid{ isSoundPlay false; testPosition 0 testChannel.stop;} 7. 快进声音。单击该按钮时,我们让声音从当前位置向前播放500毫秒,也就是快进半秒。 //快进按钮功能 private function qucikBtnEventvoid{ btnQuick.addEventListenerMouseEvent.CLICK, soudQuickPlay;} private function soudQuickPlayeMouseEventvoid{ if isSoundPlay return; testPosition testChannel.position; testChannel.stop; testChannel testSound.playtestPosition 500;} 8. 设定声音的音量增加。限制音量就须要soundTrans对象了,它其实是testChanel的soundTrans属性而已,通过它来限制音量。 //音量增加 private function volumeUpBtnEventvoid{ btnVocUp.addEventListenerMouseEvent.CLICK, upSoudVoc;} private function upSoudVoceMouseEventvoid{ if isSoundPlay return; testTrans testChannel.soundTrans; var addedVocNumber testTrans.volume 11testTrans.volume 0.05; testTrans.volume addedVoc; testChannel.soundTrans testTrans;} 9. 设定声音的音量减小。 //音量减小 private function volumeDownBtnEventvoid{ btnVocDown.addEventListenerMo