AS3.0实现音乐的播放,停止,暂停,和音量的控制

一,新建flash文件,在舞台中拖入,Button和Slider组件,还有增加一个动态文本,并分别命名实例名为:playBtn,stopBtn,vol,volText

AS3.0实现音乐的播放,停止,暂停,和音量的控制_第1张图片

二.建一个as文件AS3.0实现音乐的播放,停止,暂停,和音量的控制_第2张图片

代码如下:

package ch14_1
{
	import fl.events.SliderEvent;
	import fl.managers.*;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.text.TextFormat;

	public class MusicVoice extends MovieClip
	{
		var isplay:Boolean =false;
		var currentPosition:uint =0;
		var channel:SoundChannel ;
		var sound:Sound = new Sound();
		var format:TextFormat = new TextFormat();
		public function MusicVoice()
		{
			
			format.size= 12;
			StyleManager.setStyle("textFormat",format);
			playBtn.label ="播放";
			stopBtn.label ="停止";
			stopBtn.enabled =false;
			vol.enabled =false;
		
			sound.load(new URLRequest("music/春暖花开.mp3"));
			
			sound.addEventListener(Event.COMPLETE,loadComplete);
		}
		private function loadComplete(e:Event):void
		{
			stage.addEventListener(MouseEvent.CLICK,onClick);
			vol.addEventListener(SliderEvent.THUMB_DRAG,onVolChange);
		}
		private function onClick(e:MouseEvent):void
		{
			if(e.target==playBtn)
			{
				if(!isplay)
				{
					channel = sound.play(currentPosition);
					playBtn.label ="暂停";
					stopBtn.enabled=true;
					vol.enabled = true;
				}else
				{
					currentPosition = channel.position;
					channel.stop();
					playBtn.label ="播放";
				}
				channel.soundTransform = new SoundTransform(vol.value);
				isplay =!isplay;
			}else if(e.target ==stopBtn)
			{
				channel.stop();
				currentPosition=0;
				isplay= true;
			}
		}
		private function onVolChange(e:SliderEvent):void
		{
			volText.text="音量:"+Math.floor(e.target.value*100)+"%";
			channel.soundTransform = new SoundTransform(e.target.value);
		}
	}
}

你可能感兴趣的:(AS3学习)