首先在文档里面制作我们所需的元件,包括播放按钮,进度条,音量调节,暂停,等按钮。
1 、制作播放按钮
使用一个影片剪辑完成,当中里面设置两帧,一帧是用于放置播放按钮,一帧是用于显示暂停按钮,并为两帧设置标签。



一个可以切换播放或者暂停按钮的元件已经完成了。
当中,会使用到跳帧的方法切换按钮状态。所以在帧上里面写上了标签。有了这个后可以进行跳帧了,控制mc里面的元件显示。
2 、制作停止按钮
制作停止按钮的时候,只是需要画图工具,绘制一个圆角和矩形组合即可。
3 、制作可以拖放进度条
制作这个元件的时候,需要制作两个元件MC,一个用来显示底部(深绿色),一个用来显示进度(橙色)。
并为这橙色元件改名 controlBar,设置宽度为1。(一会用来显示进度)

完成之后,可以进行。
4、一些基本的方法:
在as3 cookbook 里面已经有很多关于mp3玩法。功能 是大同小异。
基本上功能有 播放,暂停,停止,拖放进度,静音,音量控制。主要的涉及到这些。
下面涉及到一些基本方法说明:
position 是位置记录。
播放音乐
channel=sound.play(position); 播放
停止音乐
channel.stop();停止
暂停的时候,先记录位置,再停止
position=channel.position;//记录当时的播放位置
channel.stop();
恢复播放的时候,利用这个位置Postion进行播放。
channel=sound.play(position); 播放
5、进度条交互
进度条的拖放交互,使用的鼠标按下,鼠标移动,鼠标松开的组合方式交互。
当鼠标按下的时候,可以让进度条宽度改变,产生进度改变。
progressBar.controlBar.width=progressBar.mouseX;
在鼠标移动的时候,使用鼠标坐标改变进度条的宽度。这样子进度条就像被拖放一样,实际上是改变了控制条的宽度。
progressBar.controlBar.width=progressBar.mouseX;
鼠标松开的时候,删除移动的监听事件
stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUPHandler);
完整代码:
一些说明:
当中一些跳转的帧,如
volumeBtn.gotoAndStop("stop"); 需要在元件里面设置帧标签。(volumeBtn 为音量控制按钮,用于静音设置)
volumeBar :音量控制条
progressBar:进度显示条
stopBtn:停止按钮
playBtn:播放按钮
- package
- {
- import flash.display.Sprite;
- import flash.display.MovieClip;
- import flash.events.*;
- import flash.net.*;
- import flash.media.*;
- import flash.geom.*;
- import flash.display.SimpleButton;
- public class Main extends Sprite
- {
- private var position:Number=0;
- private var channel:SoundChannel;
- private var sound:Sound;
- private var songUrl:String="1.mp3";
- private var isPlay:Boolean=false;
- public function Main()
- {
- init();
- }
- private function init():void
- {
-
- sound=new Sound();
- sound.addEventListener(IOErrorEvent.IO_ERROR,onErrorHandler);
- sound.addEventListener(Event.COMPLETE,onLoadSoundComplete);
- sound.load(new URLRequest(songUrl));
-
- playBtn.stop();
- volumeBtn.stop();
- playBtn.buttonMode=true;
- stopBtn.buttonMode=true;
- progressBar.controlBar.mouseEnabled=false;
- volumeBar.controlBar.mouseEnabled=false;
- progressBar.buttonMode=true;
- volumeBtn.buttonMode=true;
- volumeBar.buttonMode=true;
-
- volumeBar.controlBar.width=volumeBar.width;
-
-
- playBtn.addEventListener(MouseEvent.CLICK,onPlaySoundHandler);
- stopBtn.addEventListener(MouseEvent.CLICK,onStopSoundHandler);
- progressBar.addEventListener(MouseEvent.MOUSE_DOWN,onStartDragBarHandler);
- volumeBtn.addEventListener(MouseEvent.CLICK,onMuteSoundHandler);
- volumeBar.addEventListener(MouseEvent.MOUSE_DOWN,onStartDragVolomeBarHandler);
- }
- private function onErrorHandler(event:Event):void
- {
- trace("发生错误");
- }
- private function onLoadSoundComplete(event:Event):void
- {
- sound.removeEventListener(Event.COMPLETE,onLoadSoundComplete);
- channel=sound.play(position);
- playBtn.gotoAndStop("pause");
- isPlay=true;
- addEventListener(Event.ENTER_FRAME,onProgressHandler);
- }
- private function onProgressHandler(event:Event):void
- {
- if (channel==null)return;
- if (isPlay)
- {
- progressBar.controlBar.width=channel.position/sound.length*progressBar.width;
- }
-
- if (progressBar.controlBar.width<=progressBar.width && progressBar.controlBar.width>=progressBar.width-2)
- {
- stopSound();
- }
- }
-
- private function onPlaySoundHandler(event:MouseEvent):void
- {
- if (playBtn.currentLabel=="start")
- {
- if (isPlay==false)
- {
- isPlay=true;
- playBtn.gotoAndStop("pause");
- playSound();
- }
- }
- else
- {
- if (isPlay)
- {
- isPlay=false;
- playBtn.gotoAndStop("start");
- pauseSound();
- }
- }
- }
- private function onStopSoundHandler(event:MouseEvent):void
- {
- stopSound();
- }
- private function onStartDragBarHandler(event:MouseEvent):void
- {
- channel.stop();
- playBtn.gotoAndStop("pause");
- progressBar.controlBar.width=progressBar.mouseX;
- event.updateAfterEvent();
- position=progressBar.controlBar.width/progressBar.width*sound.length;
-
- channel=sound.play(position);
- progressBar.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveHandler);
- stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUPHandler);
- }
- private function onMouseMoveHandler(event:MouseEvent):void
- {
- channel.stop();
- progressBar.controlBar.width=progressBar.mouseX;
-
- position=progressBar.controlBar.width/progressBar.width*sound.length;
- channel=sound.play(position);
- event.updateAfterEvent();
- }
- private function onMouseUPHandler(event:MouseEvent):void
- {
- if (progressBar.hasEventListener(MouseEvent.MOUSE_MOVE))
- {
- progressBar.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMoveHandler);
- }
-
- if (volumeBar.hasEventListener(MouseEvent.MOUSE_MOVE))
- {
- volumeBar.removeEventListener(MouseEvent.MOUSE_MOVE,onChangeVolumeHandler);
- }
- stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUPHandler);
- }
-
- private function onMuteSoundHandler(event:MouseEvent):void
- {
- if (channel==null)return;
-
- if (volumeBtn.currentLabel=="start")
- {
- volumeBtn.gotoAndStop("stop");
- channel.soundTransform=new SoundTransform(0);
- }
- else
- {
- volumeBtn.gotoAndStop("start");
- channel.soundTransform=new SoundTransform(1);
- }
- }
-
- private function onStartDragVolomeBarHandler(event:MouseEvent):void
- {
- volumeBar.controlBar.width=volumeBar.mouseX;
- var volume:Number=volumeBar.controlBar.width/volumeBar.width;
- channel.soundTransform=new SoundTransform(volume);
- event.updateAfterEvent();
- volumeBar.addEventListener(MouseEvent.MOUSE_MOVE,onChangeVolumeHandler);
- stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUPHandler);
- }
-
-
- private function onChangeVolumeHandler(event:MouseEvent):void
- {
- volumeBar.controlBar.width=volumeBar.mouseX;
- trace(progressBar.controlBar.width);
- var volume:Number=volumeBar.controlBar.width/volumeBar.width;
- channel.soundTransform=new SoundTransform(volume);
- event.updateAfterEvent();
- stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUPHandler);
- }
-
- private function stopSound():void
- {
- if (channel==null)return;
- channel.stop();
- playBtn.gotoAndStop("start");
- position=0;
- isPlay=false;
- progressBar.controlBar.width=1;
-
- }
-
- private function playSound():void
- {
- if (channel==null)return;
- channel=sound.play(position);
- }
-
- private function pauseSound():void
- {
- if (channel==null)return;
- position=channel.position;
- channel.stop();
- }
- }
- }