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

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

二.建一个as文件

代码如下:

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);
		}
	}
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值