您当前的位置是:首页-flash as2.0

FLASH MP3制作代码

【发表于】:2010/7/10 13:38:00  已被访问:303次
//开始加载音乐 
stop(); 
var songpath:String="http://video.17173.com/av/movie/war3/other/pikaxiu/dongfengpo.mp3"; 
var songname:String="东风破"; 
var soundurl:URLRequest=new URLRequest(songpath); 
var sound:Sound=new Sound(); 
sound.load(soundurl); 
//加载成功以后创建以下的内容 
var border_mc:MovieClip=new MovieClip(); 
var play_btn:MovieClip=new MovieClip(); 
var pause_btn:MovieClip=new MovieClip(); 
var stop_btn:MovieClip=new MovieClip(); 
var volumehd_btn:MovieClip=new MovieClip(); 
var volumehk_btn:MovieClip=new MovieClip(); 
var songname_txt:TextField=new TextField(); 
//添加到场景 
this.addChild(border_mc); 
this.addChild(play_btn); 
this.addChild(pause_btn); 
this.addChild(stop_btn); 
this.addChild(volumehd_btn); 
this.addChild(volumehk_btn); 
this.addChild(songname_txt); 
//初始位置这个主要设置播放器在舞台上的位置 
var soundchannel:SoundChannel=new SoundChannel(); 
var position:Number=0; 
var X:Number=10; 
var Y:Number=10; 
//绘制外边框 
border_mc.graphics.lineStyle(3,0x0088cc); 
border_mc.graphics.drawRect(Y-2.5,Y-2.5,320,25); 
//绘制播放按钮并实现其功能 
play_btn.name="play_btn"; 
play_btn.graphics.lineStyle(1,0x0088cc); 
play_btn.graphics.beginFill(0x0088cc); 
play_btn.graphics.moveTo(X,Y); 
play_btn.graphics.lineTo(X,Y+20); 
play_btn.graphics.lineTo(X+20,Y+10); 
play_btn.graphics.lineTo(X,Y); 
play_btn.graphics.endFill(); 
play_btn.buttonMode=true; 
play_btn.addEventListener(MouseEvent.CLICK,playsong); 
function playsong(evt:MouseEvent):void { 
 if (evt.target.name=="play_btn") { 
  soundchannel=sound.play(position); 
  play_btn.visible=false; 
  pause_btn.visible=true; 
 } 

//绘制暂停按钮并实现其功能 
pause_btn.name="pause_btn"; 
pause_btn.visible=false; 
pause_btn.graphics.lineStyle(1,0x0088cc); 
pause_btn.graphics.beginFill(0x0088cc); 
pause_btn.graphics.drawRect(X,Y,5,19); 
pause_btn.graphics.drawRect(X+10,Y,5,19); 
pause_btn.graphics.endFill(); 
pause_btn.buttonMode=true; 
pause_btn.visible=false; 
pause_btn.addEventListener(MouseEvent.CLICK,pausesong); 
function pausesong(evt:MouseEvent):void { 
 if (evt.target.name=="pause_btn") { 
  position=soundchannel.position; 
  soundchannel.stop(); 
  pause_btn.visible=false; 
  play_btn.visible=true; 
 } 

//绘制停止按钮并实现其功能 
stop_btn.name="stop_btn"; 
stop_btn.graphics.lineStyle(1,0x0088cc); 
stop_btn.graphics.beginFill(0x0088cc); 
stop_btn.graphics.drawRect(X+40,Y,19,19); 
stop_btn.graphics.endFill(); 
stop_btn.buttonMode=true; 
stop_btn.addEventListener(MouseEvent.CLICK,stopplaying); 
function stopplaying(evt:MouseEvent):void { 
 if (evt.target.name=="stop_btn") { 
  position=0; 
  soundchannel.stop(); 
  play_btn.visible=true; 
  pause_btn.visible=false; 
 } 

//绘制滑道 
volumehd_btn.name="volumehd_btn"; 
volumehd_btn.graphics.lineStyle(1,0x0088cc); 
volumehd_btn.graphics.beginFill(0x0088cc); 
volumehd_btn.graphics.moveTo(X+250,Y+18); 
volumehd_btn.graphics.lineTo(X+310,Y+18); 
volumehd_btn.graphics.lineTo(X+310,Y+8); 
volumehd_btn.graphics.lineTo(X+250,Y+18); 
volumehd_btn.graphics.endFill(); 
//绘制滑块 
volumehk_btn.name="volumehk_btn"; 
volumehk_btn.graphics.lineStyle(1,0x0088cc); 
volumehk_btn.graphics.beginFill(0x0088cc); 
volumehk_btn.graphics.drawRect(X+280,Y,5,20); 
volumehk_btn.graphics.endFill(); 
volumehk_btn.buttonMode=true; 
//构造一个rectangle对象控制滑块的拖动范围 
var T:Number=volumehk_btn.y; 
var L:Number=volumehd_btn.x; 
var B:Number=0; 
var R:Number=volumehd_btn.width-volumehk_btn.width; 
var rectangle:Rectangle=new Rectangle(L-30,T,R,B); 
//实现拖动 
volumehk_btn.addEventListener(MouseEvent.MOUSE_DOWN,onmousedown); 
volumehk_btn.addEventListener(MouseEvent.MOUSE_UP,onmouseup); 
volumehk_btn.addEventListener(MouseEvent.MOUSE_OUT,onmouseout); 
stage.addEventListener(Event.ENTER_FRAME,onenterframe); 
//拖动的范围 
function onmousedown(evt:Event):void { 
 if (evt.target.name=="volumehk_btn") { 
  volumehk_btn.startDrag(false,rectangle); 
 } 

//停止拖动 
function onmouseup(evt:Event):void { 
 if (evt.target.name=="volumehk_btn") { 
  volumehk_btn.stopDrag(); 
 } 

function onmouseout(evt:Event):void { 
 if (evt.target.name=="volumehk_btn") { 
  volumehk_btn.stopDrag(); 
 } 

//滑块与声音关联并显示播放状态 
function setvolume(volume:Number):void { 
 var soundvolume:SoundTransform=soundchannel.soundTransform; 
 soundvolume.volume=volume; 
 soundchannel.soundTransform=soundvolume; 

function onenterframe(evt:Event):void { 
 var songvolume:Number=((volumehk_btn.x-volumehd_btn.x+30)/R); 
 setvolume(songvolume);//调用上面的函数 
 var total=sound.length/(sound.bytesLoaded/sound.bytesTotal); 
 var percentage=soundchannel.position/total; 
  
 var songtimem:int=Math.floor(total/1000)/60; 
 var songtimes:int=Math.floor((total/1000)%60); 
 var stimem:int=Math.floor(soundchannel.position/1000/60); 
 var stimes:int=Math.floor((soundchannel.position/1000)%60); 
  
 //定义文本的格式,显示音乐播放状态 
 var textformat:TextFormat=new TextFormat(); 
 textformat.color=0xff6600; 
 textformat.size=14; 
 songname_txt.x=X+65; 
 songname_txt.y=Y+2; 
 songname_txt.width=180; 
 songname_txt.Selectable=false; 
 songname_txt.text=songname+"| |"+stimem+":"+stimes+"—"+songtimem+":"+songtimes+"| | kuzau.com"; 
 songname_txt.setTextFormat(textformat); 
 //播放完重复播放 
 if (int(percentage*100)==99) { 
  soundchannel=sound.play(); 
 } 
 
上下篇: 调用外部图片(加载到影片剪辑中)

最新推荐信息