手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

[AS3编程教学]利用xml加载并控制声音

时间:2024-10-12 00:26:22

1、首先我们新建一个舞台,在舞台上创建三个按钮:加载btnLoad,播放btnPlay,暂停btnPause。,加载按钮用来加载xml和音乐文件,播放用来继续播放声音,暂停用来暂停声音,如下图所示:

[AS3编程教学]利用xml加载并控制声音

2、首先我们要写配置声音资源的xml文件,因为要加一个音乐,所以xml非常简单。<?xml version="1.0" encoding="utf-8" ?><data> <snd path="mSound.mp3"> </snd></data>

[AS3编程教学]利用xml加载并控制声音

3、按照顺序,我们先写“加载”按钮上的函数功能。加载按钮主要完成两步工作,第一是读取sndData.xml里面的音乐路径,第二是通过读取到得路径把音乐加载进来并播放。因此我们要定义一个声音类变量。var loadSound:Sound = new Sound();//-------------------------------addMouseEvent();function addMouseEvent():void{btnLoad.addEventListener(MouseEvent.CLICK, loadSoundData);}function loadSoundData(e:MouseEvent):void{var xLoader:URLLoader = new URLLoader();xLoader.addEventListener(Event.COMPLETE,completeLoadData);xLoader.load(new URLRequest("sndData.xml"));}function completeLoadData(e:Event):void{var mXML:XML = XML(e.target.data);var sndPath:String = String(mXML.snd.@path);trace(sndPath);loadSound.load(new URLRequest(sndPath));loadSound.addEventListener(Event.COMPLETE, completeLoadSound);}function completeLoadSound(e:Event):void{loadSound.play();}单击“加载”按钮,当这段代码被执行时,声音便会被加载进来并且自动播放!

[AS3编程教学]利用xml加载并控制声音

4、接下来写声音的暂停和播放按钮。在AS3里,为了控制声音播放暂停,我们需要另外定义一个SoundChannel对象和一个记录声音播放位置的变量,来达到这一目的。var loadSound:Sound = new Sound();var sndChannel:SoundChannel;var sndPostion:int;//-------------------------------addMouseEvent();function addMouseEvent():void{btnLoad.addEventListener(MouseEvent.CLICK, loadSoundData);btnPlay.addEventListener(MouseEvent.CLICK, continuPlaySnd);btnPause.addEventListener(MouseEvent.CLICK, pauseSnd);}//播放暂停function continuPlaySnd(e:MouseEvent):void{sndChannel = loadSound.play(sndPostion);}function pauseSnd(e:MouseEvent):void{sndPostion = sndChannel.position;sndChannel.stop();}

[AS3编程教学]利用xml加载并控制声音

5、经过测试,似乎功能已经满足,但是也有问题,当声音在播放时,点击“播放按钮”,声音会二次播放重叠,其实,当声音正在播放的时候,播放不需要处理,只有暂停的时候才让它继续播放;同理,声音只有在播放的时候,暂停按钮才能起作用。所以我们定义需要一个是否播放的状态布尔变量,当播放时为true,暂停时为false,在按钮单击时加以判断:var isSoundPlay:Boolean = false;//播放暂停function continuPlaySnd(e:MouseEvent):void{if(isSoundPlay)return;isSoundPlay = true;sndChannel = loadSound.play(sndPostion);}function pauseSnd(e:MouseEvent):void{if(!isSoundPlay)return;isSoundPlay = false;sndPostion = sndChannel.position;sndChannel.stop();}

[AS3编程教学]利用xml加载并控制声音

6、此时测试音乐,一切都正常,但是舞台上空白了很多,我们加一个波普效果,随着音乐的播放暂停同步运动,这样很明显地看到你对音乐的控制了,如图所示我们做一个简单的波形动画:

[AS3编程教学]利用xml加载并控制声音

7、给波形动画命名为mcWave,然后修改控制函数。//播放暂停function continuPlaySnd(e:MouseEvent):void{if(isSoundPlay)return;isSoundPlay = true;sndChannel = loadSound.play(sndPostion);mcWave.play();}function pauseSnd(e:MouseEvent):void{if(!isSoundPlay)return;isSoundPlay = false;sndPostion = sndChannel.position;sndChannel.stop();mcWave.stop();}

8、此时再测试,就会有一个波形直观地显示音乐的播放暂停了。

[AS3编程教学]利用xml加载并控制声音

9、此时源文件目录如下:

[AS3编程教学]利用xml加载并控制声音

10、完整代码如下:var loadSound:Sound = new Sound();var sndCh锾攒揉敫annel:SoundChannel;var sndPostion:int;var isSoundPlay:Boolean = false;mcWave.stop();//-------------------------------addMouseEvent();function addMouseEvent():void{ btnLoad.addEventListener(MouseEvent.CLICK, loadSoundData); btnPlay.addEventListener(MouseEvent.CLICK, continuPlaySnd); btnPause.addEventListener(MouseEvent.CLICK, pauseSnd);}//加载音乐function loadSoundData(e:MouseEvent):void{ var xLoader:URLLoader = new URLLoader(); xLoader.addEventListener(Event.COMPLETE,completeLoadData); xLoader.load(new URLRequest("sndData.xml"));}function completeLoadData(e:Event):void{ var mXML:XML = XML(e.target.data); var sndPath:String = String(mXML.snd.@path); loadSound.load(new URLRequest(sndPath)); loadSound.addEventListener(Event.COMPLETE, completeLoadSound);}function completeLoadSound(e:Event):void{ sndChannel = loadSound.play(); isSoundPlay = true mcWave.play();}//播放暂停function continuPlaySnd(e:MouseEvent):void{ if(isSoundPlay)return; isSoundPlay = true; sndChannel = loadSound.play(sndPostion); mcWave.play();}function pauseSnd(e:MouseEvent):void{ if(!isSoundPlay)return; isSoundPlay = false; sndPostion = sndChannel.position; sndChannel.stop(); mcWave.stop();}

© 手抄报圈