目标:建立一个音乐播放器,从别的网站下载音乐来播!
解决方案:
第一步:
舞台上拖放一个progress,一个按钮,一个inputbox,做相关设置如下:
图一
图二
图三
图四
第二步:
把下面的代码添加到第一帧中:
import fl.controls.Button;import fl.controls.ProgressBar;import fl.controls.ProgressBarMode;import flash.net.URLRequest;import flash.events.*;import flash.media.Sound;import flash.media.SoundChannel;import flash.media.SoundLoaderContext;System.useCodePage = true;var myURL:String =new String();var Isplaying:Boolean = new Boolean(false);var sound=new Sound();var channel:SoundChannel=new SoundChannel();var loadvar:SoundLoaderContext = new SoundLoaderContext(1000,true);var myrequest:URLRequest;var volcomplete:Boolean =new Boolean();//_root.filesArray = ["null","11362977554da.mp3","temp3","null","temp5","temp6"];//_root.filesNote = ["源语复述", "译入与交传", "试译", "二次源语复述", "同传","交传"];//var label[]:Label = new Label();myURL = myURLtxt.text;myButton.addEventListener(MouseEvent.CLICK,onload);function onload(evt:Event){ if (myURLtxt.text == "") { myURLtxt.text = "请在此输入mp3文件的地址"; } else { myURL = myURLtxt.text; loadmusic(myURL); }}function loadmusic(str:String){ myrequest = new URLRequest(str); if (channel.position > 0) { if (! volcomplete) { sound.close(); } sound=new Sound(); } sound.addEventListener(ProgressEvent.PROGRESS, progressHandler); sound.addEventListener(Event.COMPLETE, completeHandler); sound.load(myrequest,loadvar); volcomplete = false; channel = sound.play(); Isplaying = true;}function progressHandler(event:ProgressEvent){ myProgress.indeterminate = false; myProgress.mode = ProgressBarMode.MANUAL; myProgress.maximum = 200; myProgress.setProgress(event.bytesLoaded*200/event.bytesTotal, myProgress.maximum);}function completeHandler(event:Event){ volcomplete = true;}
测试,放在网页中测试也不会出现警告。
(注意:要放在服务器上)