为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > Flash学习As3音乐播放器教程

Flash学习As3音乐播放器教程

2022-10-18 2页 doc 31KB 7阅读

用户头像 个人认证

我的狼我的豹

暂无简介

举报
Flash学习As3音乐播放器教程Flash学习As3音乐播放器教程那么开始吧!大家先下载一下源文件。对着源文件的结构看教程。我将整个播放器做到一个元件中(Control_panel),再给它绑定一个类(sound_lib.Control_panel),所有的代码就写在这个类里面了。这样做的好处是:日后要用到它时,直接把Control_panel元件拖到你要用的地方就Ok了!接下来就是code了:要做音乐播放器,最重要的两个类当然就是Sound和SoundChannel。先试一下,怎样让一首mp3播放起来://建立一个Sound类sound=newSound(...
Flash学习As3音乐播放器教程
Flash学习As3音乐播放器教程那么开始吧!大家先下载一下源文件。对着源文件的结构看教程。我将整个播放器做到一个元件中(Control_panel),再给它绑定一个类(sound_lib.Control_panel),所有的代码就写在这个类里面了。这样做的好处是:日后要用到它时,直接把Control_panel元件拖到你要用的地方就Ok了!接下来就是code了:要做音乐播放器,最重要的两个类当然就是Sound和SoundChannel。先试一下,怎样让一首mp3播放起来://建立一个Sound类sound=newSound()//加载mp3文件sound.load(newURLRequest(“你的mp3路径”))//播放sound.play()Ok了!完整代码:packagesound_lib{importflash.display.*importflash.media.Soundimportflash.media.SoundChannelimportflash.net.URLRequestpublicclassControl_panelextendsMovieClip{privatevarsound:Soundprivateconstsound_url:String="sound_data/m01.mp3"publicfunctionControl_panel(){init()11.}privatefunctioninit(){sound_init()14.}functionsound_init(){sound=newSound()sound.load(newURLRequest(sound_url))sound.play()19.}20.}21.}22.Ctrl+Enter一下。是不是听到音乐了?啊?没听到?看看sound_data目录下是否放了mp3文件没?如果一切正常的话,你现在应该在享受你的音乐了。呵呵。不过别忘了大多数播放器是要用在web上的。有时候声音播放的速度可能比下载的速度还要快,这种情况下声音播放就会暂停等待数据下载,为了更好的处理这个环节,我们可以设置一个数据缓冲区,当声音数据下载到一定数量时再进行播放,这样的话即使下载速度偶尔变慢也不会影响正常播放。默认下Sound对象只创建1秒钟的缓冲,也就是说要想立即播放也需要等待1秒钟的缓冲,缓冲区的数据播放完后要想再次播放还要至少等1秒钟缓冲时间。显然如果网速较慢的话且缓冲为1秒时,在这种情况下音乐的播放将会给人很”卡”的感觉。所以我们必需手动设置一下缓冲:设置缓冲需要用到SoundLoaderContext类:建立一个SoundLoaderContext对象,然后设置它的bufferTime属性就行了。最后将SoundLoaderContext对象传给sound对象的load()方法的参数就行了。//建立一个SoundLoaderContext类,设置bufferTime为5秒varbuffer:SoundLoaderContext=newSoundLoaderContext(5000)sound=newSound()sound.load(newURLRequest(sound_url),buffer)sound.play()声音的暂停与回放这样,即使放到web上也不成问题了。不过现在只是能正常播放了,我们还得控制它才行嘛!最常见的控制就是暂停与回放了。声音的暂停与回放不像视频流那样,可以直接pause和resume来操作。看看Soun类,大家可能会调用Sound对象的close()方法可以停止播放,但是这样也停止了声音流,要想重新播放,必须再次调用load()方法。这显然不是我们想要的。这里我们终于要用到上面所提到的SoundChannel了,其实对音频的绝大多数操作都是SoundChannel类来完成的。SoundChannel的stop()方法可以让音乐暂停而不影响声音流的中断。于是我们可以用SoundChannel对象的stop()方法与Sound对象的play()方法来暂停与回流音频流。但是,当再次调用play()方法时,音乐会从头开始播放而不是从暂停的地方开始,这个时候就要用到SoundChannel类的position属性了:position属性是用来音乐播放头的。我们可以在暂停时,记录下position的值,然后再次调play()时,将它传给play()方法的参数。这样就达到了最终目:sound_channel.stop()//记录position属性position=sound_channel.positionsound_channel=sound.play(position)完整代码:1.packagesound_lib{importflash.display.*importflash.media.Soundimportflash.media.SoundChannelimportflash.media.SoundLoaderContextimportflash.events.*importflash.net.URLRequestpublicclassControl_panelextendsMovieClip{privatevarsound:Soundprivateconstsound_url:String="sound_data/m01.mp3"privatevarsound_channel:SoundChannelprivatevarposition:intprivatevaris_play:BooleanpublicfunctionControl_panel(){addEventListener(Event.ADDED_TO_STAGE,add_to_stage)16.}privatefunctionadd_to_stage(_evt:Event):void{init()19.}privatefunctioninit(){sound_init()control_init()23.}24.//==================soundpart====================//functionsound_init(){varbuffer:SoundLoaderContext=newSoundLoaderContext(5000)sound=newSound()sound.load(newURLRequest(sound_url),buffer)sound_channel=sound.play()is_play=trueplay_mc.visible=false32.}33.//===================controlpart=================//functioncontrol_init(){pause_mc._btn.addEventListener(MouseEvent.CLICK,is_play_Handler)play_mc._btn.addEventListener(MouseEvent.CLICK,is_play_Handler)37.}privatefunctionis_play_Handler(_evt:MouseEvent):void{if(is_play){is_play=falseposition=sound_channel.positionsound_channel.stop()play_mc.visible=truepause_mc.visible=false}else{is_play=truesound_channel=sound.play(position)play_mc.visible=falsepause_mc.visible=true50.}51.}52.}53.}Ctrl+Enter一下。Ok了吧?跟踪声音的加载进度与播放进度要得到声音的加载进度是很简单的,Sound类本身有bytesLoaded属性与bytesTotal属性。只要在progress事件中监测到这个属性值就Ok了具体操作如下:privatefunctionsound_loading_Handler(_evt:ProgressEvent):void{varloaded_per=Math.ceil(100*_evt.bytesLoaded/_evt.bytesTotal)/100scroll_bar.loading_bar.scaleX=loaded_per4.}5.//loaded_per就是加载进度的百分比了,简单吧!不过用户肯定不满足于只看到加载进度,他们更希望能看到声音的播放进度并能控制播放进度。要得到播进度,必须知道两个值:音乐的长度和当前的播放位置。这两个属性分别在两不同的类里。长度属性在sound类里,播放位置在SoundChannel类里,这两个值相除就是播放进度百分比。呵呵,就这么简单!不过好像高兴得有点早了:sound类的length属性是不确定的,直到声音被下载完才确定,也就说它只表示已经被下载的那部分数据的长度,举个例子说,如果一个10分钟的音乐已下载了10%,那么length歌曲长度为1分钟(length和position的单位都是毫秒)。不过从我上面的例子不难看出,只要长度除以加载进度百分比即可算出实际长度即sound_length/loaded_per。嗯原理搞清楚了就来试试吧:varloaded=sound.bytesLoadedvartotal=sound.bytesTotalsound_length=sound.lengthif(total>0){varloaded_per=loaded/totalscroll_bar.loading_bar.scaleX=loaded_persound_length=length/loaded_perposition=sound_channel.positionvarplayed_per=position/sound_length10.}好了。跟据以上思路再整理一下你的code:1.packagesound_lib{45.46.47.48.49.50.51.52.53.position=sound_channel.positionsound_channel.stop()play_mc.visible=truepause_mc.visible=false}else{is_play=truesound_channel=sound.play(position)play_mc.visible=falsepause_mc.visible=true40.41.42.43.44.addEventListener(Event.ENTER_FRAME,progress_Handler)}privatefunctionis_play_Handler(_evt:MouseEvent):void{if(is_play){is_play=falseimportflash.display.*importflash.media.Soundimportflash.media.SoundChannelimportflash.media.SoundLoaderContextimportflash.events.*importflash.net.URLRequestpublicclassControl_panelextendsMovieClip{privatevarsound:Soundprivateconstsound_url:String="sound_data/m01.mp3"privatevarsound_channel:SoundChannelprivatevarposition:intprivatevarsound_lengthprivatevaris_play:BooleanprivatevarloadEnd:Boolean=falsepublicfunctionControl_panel(){addEventListener(Event.ADDED_TO_STAGE,add_to_stage)18.}privatefunctionadd_to_stage(_evt:Event):void{init()21.}privatefunctioninit(){sound_init()control_init()25.}26.//==================soundpart====================//functionsound_init(){varbuffer:SoundLoaderContext=newSoundLoaderContext(5000)sound=newSound()sound.load(newURLRequest(sound_url),buffer)sound_channel=sound.play()sound.addEventListener(ProgressEvent.PROGRESS,sound_loading_Handler)is_play=trueplay_mc.visible=false35.}36.//===================controlpart=================//functioncontrol_init(){pause_mc._btn.addEventListener(MouseEvent.CLICK,is_play_Handler)play_mc._btn.addEventListener(MouseEvent.CLICK,is_play_Handler)54.}55.}privatefunctionsound_loading_Handler(_evt:ProgressEvent):void{varloaded_per=Math.ceil(100*_evt.bytesLoaded/_evt.bytesTotal)/100if(loaded_per>=100)loadEnd=truescroll_bar.loading_bar.scaleX=loaded_per60.}privatefunctionprogress_Handler(_evt:Event):void{varloaded=sound.bytesLoadedvartotal=sound.bytesTotalsound_length=sound.lengthif(total>0){varloaded_per=loaded/totalif(loaded_per>=1)loadEnd=truescroll_bar.loading_bar.scaleX=loaded_persound_length=sound_length/loaded_perposition=sound_channel.positionvarplayed_per=position/sound_lengthscroll_bar.dragger.x=(scroll_bar.scroll_bg.width-scroll_bar.dragger.width)*played_per73.}74.}75.}76.}呼。。。我是不是讲得太啰嗦了?继续吧。这才只能让用户看到播放进度而以。下面我们还要让用户能控制它。要控制它用到了我们常见的滚动条。由于滚动条是我们很常用的东东,所以我把它写成了一个类。(Sound_lib/Scroll_bar.as),类很简单,如果看不懂的话,可以参考控制音量的滚动条。其实这里用Scroll_bar.as不大合适,不过我还是想偷一下懒!说到这里,怎么定位播放头呢?想想“声音的暂停与回放”是怎么做的,我们就可以得找出办法了用sound.play(position)。Position的值就更容易得到了:sound_length*per。但是这里值得注意的是play()之前要把之前的sound_channel停止,要不就出现多重声音了。。OK了。再跟据以上思路完善一下你的code吧!1.packagesound_lib{45.46.47.48.49.50.51.52.53.position=sound_channel.positionsound_channel.stop()play_mc.visible=truepause_mc.visible=false}else{is_play=truesound_channel=sound.play(position)play_mc.visible=falsepause_mc.visible=true40.41.42.43.44.addEventListener(Event.ENTER_FRAME,progress_Handler)}privatefunctionis_play_Handler(_evt:MouseEvent):void{if(is_play){is_play=falseimportflash.display.*importflash.media.Soundimportflash.media.SoundChannelimportflash.media.SoundLoaderContextimportflash.events.*importflash.net.URLRequestpublicclassControl_panelextendsMovieClip{privatevarsound:Soundprivateconstsound_url:String="sound_data/m01.mp3"privatevarsound_channel:SoundChannelprivatevarposition:intprivatevarsound_lengthprivatevaris_play:BooleanprivatevarloadEnd:Boolean=falsepublicfunctionControl_panel(){addEventListener(Event.ADDED_TO_STAGE,add_to_stage)18.}privatefunctionadd_to_stage(_evt:Event):void{init()21.}privatefunctioninit(){sound_init()control_init()25.}26.//==================soundpart====================//functionsound_init(){varbuffer:SoundLoaderContext=newSoundLoaderContext(5000)sound=newSound()sound.load(newURLRequest(sound_url),buffer)sound_channel=sound.play()sound.addEventListener(ProgressEvent.PROGRESS,sound_loading_Handler)is_play=trueplay_mc.visible=false35.}36.//===================controlpart=================//functioncontrol_init(){pause_mc._btn.addEventListener(MouseEvent.CLICK,is_play_Handler)play_mc._btn.addEventListener(MouseEvent.CLICK,is_play_Handler)54.}55.}privatefunctionsound_loading_Handler(_evt:ProgressEvent):void{varloaded_per=Math.ceil(100*_evt.bytesLoaded/_evt.bytesTotal)/100if(loaded_per>=100)loadEnd=truescroll_bar.loading_bar.scaleX=loaded_per60.}privatefunctionprogress_Handler(_evt:Event):void{varloaded=sound.bytesLoadedvartotal=sound.bytesTotalsound_length=sound.lengthif(total>0){varloaded_per=loaded/totalif(loaded_per>=1)loadEnd=truescroll_bar.loading_bar.scaleX=loaded_persound_length=sound_length/loaded_perposition=sound_channel.positionvarplayed_per=position/sound_lengthscroll_bar.dragger.x=(scroll_bar.scroll_bg.width-scroll_bar.dragger.width)*played_per73.}74.}75.}76.}Ctrl+Enter一下!再拖动一下滑块,别拖到音量骨块上去哦。呵呵。。音量的控制要改调节音量需用到SoundChannel对象的soundTransform属性,它是SoundTransform的类实例。操用步骤:1.建立一个soundTransform对象。2.设置soundTransform对象的volume值(0-1)3.将soundTransform对象赋给SoundChannel的soundTransform属性属性。为了方便我将其写成函数functionset_volume(temp:SoundChannel,temp_value:Number){varsound_trf:SoundTransform=newSoundTransform()sound_trf.volume=temp_valuetemp.soundTransform=sound_trf5.}再看一下控制音量的滑块。也写成了一个类(sound_lib/Vol_scroll_bar.as)packagesound_lib{importflash.display.*importflash.events.*importflash.geom.*publicclassVol_scroll_barextendsMovieClip{publicstaticconstSCROLL:String="scroll"publicvarperpublicfunctionVol_scroll_bar(){this.addEventListener(Event.ADDED_TO_STAGE,add_to_stage)10.}privatefunctionadd_to_stage(_evt:Event):void{init()13.}privatefunctioninit(){dragger._btn.addEventListener(MouseEvent.MOUSE_DOWN,press_Handler)dragger._btn.addEventListener(MouseEvent.MOUSE_UP,release_Handler)17.}privatefunctionpress_Handler(_evt:MouseEvent):void{dragger.startDrag(false,newRectangle(scroll_bg.x,scroll_bg.y,scroll_bg.width-dragger.width,0))scroll_bg.addEventListener(Event.ENTER_FRAME,move_Handler)stage.addEventListener(MouseEvent.MOUSE_UP,release_Handler)22.}privatefunctionrelease_Handler(_evt:MouseEvent):void{dragger.stopDrag()scroll_bg.removeEventListener(Event.ENTER_FRAME,move_Handler)stage.removeEventListener(MouseEvent.MOUSE_UP,release_Handler)27.}privatefunctionmove_Handler(_evt:Event):void{per=Math.ceil(100*dragger.x/(scroll_bg.width-dragger.width))/100this.dispatchEvent(newEvent(SCROLL))31.}32.}33.}该类就公开了一个属性per(百分比)那么怎么实时获取per呢?看到this.dispatchEvent(newEvent(SCROLL))这句没?在拖动滑块的同时给该类加入了一个事件,所以我们只需在父类中贞听这个事件就可以了:privatefunctionVol_scroll_init(){vol_scroll_bar.addEventListener(Vol_scroll_bar.SCROLL,get_per_Handler)3.}privatefunctionget_per_Handler(_evt):void{trace(vol_scroll_bar.per)6.}Ctrl+Enter一下!拖动滑块(别拖错了哦)查看trace出来的值。这到里你应该明天怎么做了吧:再结合上面给出的调节音量的函数。privatefunctionVol_scroll_init(){vol_scroll_bar.addEventListener(Vol_scroll_bar.SCROLL,get_per_Handler)3.}privatefunctionget_per_Handler(_evt):void{set_volume(sound_channel,Number(vol_scroll_bar.per))trace(vol_scroll_bar.per)7.}functionset_volume(temp:SoundChannel,temp_value:Number){varsound_trf:SoundTransform=newSoundTransform()sound_trf.volume=temp_valuetemp.soundTransform=sound_trf12.}至此音量的控制搞定读取声音文件的ID3数据MP3文件大多包含一些如songname,artist,album,genre,year等元数据,不过这些并不是都有。通过Sound对象的id3属性可获得这些数据。这个属性其实是ID3Info的类实例,它包含下列属性:artist(歌手名)、songName(歌曲名).........等。获取文件的ID3数据很简单:通过监听Sound对象的ID3事件就行了sound.addEventListener(Event.ID3,ID3_Handler)privatefunctionID3_Handler(_evt:Event):void{trace(sound.id3.songName)4.}将以上代码加到之前的代码Ctrl+Enter一下!很不幸,你很可能看到的是乱码。编码问题,不想多说了,用这个函数转一下就行了privatefunctionEncodeUtf8(str:String):String{varoriByteArr:ByteArray=newByteArray();oriByteArr.writeUTFBytes(str);vartempByteArr:ByteArray=newByteArray();for(vari=0;i
/
本文档为【Flash学习As3音乐播放器教程】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索