[AS3.0编程教学]最全的声音控制方法
1、首先我们新建一个文件,在舞台上摆出下面这些按钮,我们今天对这个声音文件的操纵就如按钮所示:
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/b666b2530688912ce32f655b1b4800fc76f79778.jpg)
2、动手之前我们按下Ctrl+Shift+F12,打开ActionScript设置,将“自动申明舞台对象”打钩取消,我们将每个对象自己用Public声明,这样做的好处是开发时每个元件的属性方便引用和提醒。
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/a48bc2e8904800fc3d1221bdd42043715edb9378.jpg)
3、我们新建一个文档类,首先声明舞台上这些按钮,并定义声音变量:testSound,控制变量testChannel,testTrans,testPosition。public var btnPlay:SimpleButton;public var btnPause:SimpleButton;public var btnStop:SimpleButton;public var btnQuick:SimpleButton;public var btnVocUp:SimpleButton;public var btnVocDown:SimpleButton;public var btnPanUp:SimpleButton;public var btnPanDown:SimpleButton;private var testSound:Sound;private var testChannel:SoundChannel;private var testTrans:SoundTransform;private var testPosition:Number=0;
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/76b6860e5f204371113e8f93323acd8921c58f78.jpg)
4、首先用下面代码訇咀喃谆将一首叫做“test.mp3"的音乐加载到舞台。public function TestSoundMain(){testSound = new Sou荏鱿胫协nd();testChannel=new SoundChannel();testTrans = new SoundTransform();testSound.load(new URLRequest("test.mp3"));testSound.addEventListener(Event.COMPLETE,soundLoadOver);}private function soundLoadOver(e:Event):void{testSound.removeEventListener(Event.COMPLETE, soundLoadOver);soudLoad = true;}
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/423041db3620b93a9c6c9976ad0f8835dc8a8a78.jpg)
5、播放按钮功能。控制音乐播放的按钮,单击后音乐开始播放,并记录音乐的SoundChannel属性。为了防止连击,我们定义一个isSoundPlay布尔变量判断音乐是否在播放中。//播放按钮功能private function playBtnEvent():void{btnPlay.addEventListener(MouseEvent.CLICK, soundPlay);}private function soundPlay(e:MouseEvent):void{if (isSoundPlay) return;isSoundPlay = true;testChannel = testSound.play(testPosition);}
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/ccc83ec5260f8835886fe06dce07880139708678.jpg)
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();}
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/dccb47de4507880161956cf2b18ca608a40f8278.jpg)
7、停止按钮功能,单击后音乐停止播放,记录位置归0.//停止按钮功能private function stopBtnEvent():void{btnStop.addEventListener(MouseEvent.CLICK, soundStop);}private function soundStop(e:MouseEvent):void{isSoundPlay = false;testPosition = 0testChannel.stop();}
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/444696013870d54161691fbb2e0f94fc518c8078.jpg)
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);}
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/89402670d5413a8cfaed1cbc1ffc508c9ace8178.jpg)
9、设定声音的音量增加。控制音量就酋篚靶高需要soundTransform对象了,它其实是testChanel的soundTransform属佣甲昭宠性而已,通过它来控制音量。//音量增加private function volumeUpBtnEvent():void{btnVocUp.addEventListener(MouseEvent.CLICK, upSoudVoc);}private function upSoudVoc(e:MouseEvent):void{if (!isSoundPlay) return;testTrans = testChannel.soundTransform;var addedVoc:Number = testTrans.volume > 1?1:(testTrans.volume +0.05);testTrans.volume = addedVoc;testChannel.soundTransform = testTrans;}
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/d400248ca608a50fca19e93f10cec7f88b77ff78.jpg)
10、设定声音的音量减小。//音量减小private function volumeDownBtnEvent():void{btnVocDown.addEventListener(MouseEvent.CLICK, downSoundVoc);}private function downSoundVoc(e:MouseEvent):void{if (!isSoundPlay) return;testTrans = testChannel.soundTransform;var downVoc:Number = testTrans.volume < 0?0:(testTrans.volume -0.05);testTrans.volume = downVoc;testChannel.soundTransform = testTrans;}
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/a44e8afc508c9bcea61d33c4d6dd884ce44afa78.jpg)
11、设定声音的平衡度向右,点击此按钮声音的平衡性会右移,直到变成右声道。//平衡向右移动private function panRightBtnEvent():void{btnPanUp.addEventListener(MouseEvent.CLICK, upSoundPan);}private function upSoundPan(e:MouseEvent):void{if (!isSoundPlay) return;testTrans = testChannel.soundTransform;var addedPan:Number = testTrans.pan > 1?1:(testTrans.pan + 0.05);testTrans.pan = addedPan;testChannel.soundTransform = testTrans;}
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/c6b994775ddd884c80af96946cef28066a01f678.jpg)
12、设定声音的平衡度向左,点击此按鸵晚簧柢钮声音的平衡性会左移,直到变成左声道。//平衡向左移动private function panLeftBtnEvent():void{btnPanDown.ad颊俄岿髭dEventListener(MouseEvent.CLICK, downSoundPan);}private function downSoundPan(e:MouseEvent):void{if (!isSoundPlay) return;testTrans = testChannel.soundTransform;var downPan:Number = testTrans.pan < 0?0:(testTrans.pan - 0.05);testTrans.pan = downPan;testChannel.soundTransform = testTrans;}
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/2947750192dd3340da162834881c99c0aefcf178.jpg)
13、最后别忘了所有你定义的函数都要写到音乐加载完成的那个函数里执行,或者构造函数也可以。就像下面这样子://加载音乐并控制播放private function soundLoadOver(e:Event):void{testSound.removeEventListener(Event.COMPLETE, soundLoadOver);soudLoad = true;playBtnEvent();pauseBtnEvent();stopBtnEvent();qucikBtnEvent();volumeUpBtnEvent();volumeDownBtnEvent();panRightBtnEvent();panLeftBtnEvent();}
![[AS3.0编程教学]最全的声音控制方法](https://exp-picture.cdn.bcebos.com/025d87c0affce18656ca11c11f1fbee435daeb78.jpg)
14、所有这些一个个代码合并到一起,就是我们主的文档类。制作完毕!