[AS3.0编程教学]最全的声音控制方法_第1页
[AS3.0编程教学]最全的声音控制方法_第2页
[AS3.0编程教学]最全的声音控制方法_第3页
[AS3.0编程教学]最全的声音控制方法_第4页
[AS3.0编程教学]最全的声音控制方法_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

精品文档AS3.0编程教学最全的声音控制方法网上做flash音乐播放器的人不少,这个作品主要是对声音的外部读取,然后保存,然后控制播放,暂停,停止等操作,今天这个作品就是向大家展示这些操作的方法。1. 首先我们新建一个文件,在舞台上摆出下面这些按钮,我们今天对这个声音文件的操纵就如按钮所示:动手之前我们按下Ctrl+Shift+F12,打开ActionScript设置,将“自动申明舞台对象”打钩取消,我们将每个对象自己用Public声明,这样做的好处是开发时每个元件的属性方便引用和提醒。2. 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;3. 4首先用下面代码将一首叫做“test.mp3的音乐加载到舞台。public function TestSoundMain()testSound = new Sound();testChannel=new SoundChannel();testTrans = new SoundTransform();testSound.load(new URLRequest(test.mp3);testSound.addEventListener(Event.COMPLETE,soundLoadOver);private function soundLoadOver(e:Event):voidtestSound.removeEventListener(Event.COMPLETE, soundLoadOver);soudLoad = true;4. 播放按钮功能。控制音乐播放的按钮,单击后音乐开始播放,并记录音乐的SoundChannel属性。为了防止连击,我们定义一个isSoundPlay布尔变量判断音乐是否在播放中。/播放按钮功能private function playBtnEvent():voidbtnPlay.addEventListener(MouseEvent.CLICK, soundPlay);private function soundPlay(e:MouseEvent):voidif (isSoundPlay) return;isSoundPlay = true;testChannel = testSound.play(testPosition);5. 暂停 按钮功能,该按钮让音乐暂停掉,为了能继续播放,我们需要记录下此时testChannel的位置,然后播放按钮单击时可以继续播放/暂停按钮功能private function pauseBtnEvent():voidbtnPause.addEventListener(MouseEvent.CLICK, soudPause);private function soudPause(e:MouseEvent):voidif (!isSoundPlay) return;isSoundPlay = false;testPosition = testChannel.position;testChannel.stop();6. 停止按钮功能,单击后音乐停止播放,记录位置归0./停止按钮功能private function stopBtnEvent():voidbtnStop.addEventListener(MouseEvent.CLICK, soundStop);private function soundStop(e:MouseEvent):voidisSoundPlay = false;testPosition = 0testChannel.stop();7. 快进声音。单击该按钮时,我们让声音从当前位置向前播放500毫秒,也就是快进半秒。/快进按钮功能private function qucikBtnEvent():voidbtnQuick.addEventListener(MouseEvent.CLICK, soudQuickPlay);private function soudQuickPlay(e:MouseEvent):voidif (!isSoundPlay) return;testPosition = testChannel.position;testChannel.stop();testChannel = testSound.play(testPosition + 500);8. 设定声音的音量增加。控制音量就需要soundTransform对象了,它其实是testChanel的soundTransform属性而已,通过它来控制音量。/音量增加private function volumeUpBtnEvent():voidbtnVocUp.addEventListener(MouseEvent.CLICK, upSoudVoc);private function upSoudVoc(e:MouseEvent):voidif (!isSoundPlay) return;testTrans = testChannel.soundTransform;var addedVoc:Number = testTrans.volume 1?1:(testTrans.volume +0.05);testTrans.volume = addedVoc;testChannel.soundTransform = testTrans;9. 设定声音的音量减小。/音量减小private function volumeDownBtnEvent():voidbtnVocDown.addEventListener(MouseEvent.CLICK, downSoundVoc);private function downSoundVoc(e:MouseEvent):voidif (!isSoundPlay) return;testTrans = testChannel.soundTransform;var downVoc:Number = testTrans.volume 1?1:(testTrans.pan + 0.05);testTrans.pan = addedPan;testChannel.soundTransform = testTrans;11. 设定声音的平衡度向左,点击此按钮声音的平衡性会左移,直到变成左声道。/平衡向左移动private function panLeftBtnEvent():voidbtnPanDown.addEventListener(MouseEvent.CLICK, downSoundPan);private function downSoundPan(e:MouseEvent):voidif (!isSoundPlay) return;testTrans = testChannel.soundTransform;var downPan:Number = testTrans.pan 0?0:(testTrans.pan - 0.05);testTrans.pan = downPan;testChannel.soundTransform = testTrans;12. 最后别忘了所有你定义的函数都要写到音乐加载完成的那个函数里执行,或者构造函数也可以。就像下面这样子:/加载音乐并控制播放private function soundLoadOver(e:Event):voidtestSound.removeEventListener(Event.COMPLETE, soundLoadOver);soudLoad = true;playBtnEvent();pauseBtnEvent();stopBtnEvent();qucikBtnEvent();volumeUpBtnEvent();volumeDownBtnEvent();panRightBtnEvent();panLeftBtnEvent();13. 所有这些一个个代码合

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论