actionscript中的sound类控制音频文件的操作示例
package
{
import fl.controls.Button;
import fl.controls.Label;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.media.SoundTransform;
import fl.controls.Slider;
import fl.events.SliderEvent;
/**
* ...
* @author chb
*/
public class progressSoundExample extends Sprite
{
var sound:Sound;
var change:SoundChannel;
var lab:Label;//显示加载进度
var labCurrentTime:Label;//显示当前时间
var labTotalTime:Label;//显示总时间
var playBtn:Button;//开始按钮
var stopBtn:Button;//停止按钮
//记录是否播放,默认是播放
var play_tag:uint = 1;
//当前播放位置记录,用于继续播放
var position:int = 0;
//声音控制
var sliderVolume:Slider;
public function progressSoundExample():void
{
trace("执行了构造函数");
lable_Init();
btn_Init();
sliderVolume_Init();
setMain();
}
public function setMain():void
{
//千年等一回http://zhangmenshiting.baidu.com/service/938ae783a3bc193e6d48ea25348e2a93.mp3?xcode=bbd275a68b5dbe3b23d614f88ac17286
//雪中情《雪山飞狐》http://mul1.waptx.com/music/group/bbs/mp3/43/090116/1232049304420.mp3?z=906727070
//梦中人《雪山飞狐》http://61.136.63.144/70/%E8%BF%BD%E6%A2%A6%E4%BA%BA.mp3
var url:URLRequest = new URLRequest("http://zhangmenshiting.baidu.com/service/938ae783a3bc193e6d48ea25348e2a93.mp3?xcode=bbd275a68b5dbe3b23d614f88ac17286");
sound = new Sound(url);
change = sound.play(position);
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
/**
* ...........................
* 初始化标签
*/
public function lable_Init():void
{
lab= new Label();
lab.text = "播放进度:";
lab.move(10, 50);
lab.width = 200;
stage.addChild(lab);
labCurrentTime = new Label();
labCurrentTime.move(10, 26);
stage.addChild(labCurrentTime);
labTotalTime = new Label();
labTotalTime.move(480, 26);
stage.addChild(labTotalTime);
}
/**
* ...........................
* 初始化按钮
*/
public function btn_Init():void
{
playBtn = new Button();
playBtn.label = "暂停";
playBtn.move(10, 100);
playBtn.width = 60;
stage.addChild(playBtn);
playBtn.addEventListener(MouseEvent.CLICK,playBtnClick);
stopBtn = new Button();
stopBtn.label = "停止";
stopBtn.move(90, 100);
stopBtn.width = 60;
stage.addChild(stopBtn);
stopBtn.addEventListener(MouseEvent.CLICK,stopBtnClick);
}
/**
* ...........................
* 初始化音频音量控件
*/
public function sliderVolume_Init():void
{
sliderVolume= new Slider();
sliderVolume.move(200, 100);
sliderVolume.value = 1;//设置音量默认为1
stage.addChild(sliderVolume);
sliderVolume.addEventListener(SliderEvent.CHANGE,sliderVolumeChange);
}
/**
* ...........................
* 音频音量控件值改变事件
*/
public function sliderVolumeChange(e:SliderEvent):void
{
var transFrom:SoundTransform = new SoundTransform();
transFrom.volume = sliderVolume.value;
change.soundTransform = transFrom;
trace("音量为---------------------------:"+sliderVolume.value);
}
/**
* ...........................
* 播放暂停按钮点击事件
*/
public function playBtnClick(e:MouseEvent):void
{
play_tag = 1;
if (playBtn.label == "播放")
{
change=sound.play(position);
playBtn.label = "暂停";
}
else
{
change.stop();
playBtn.label = "播放";
}
}
/**
* ...........................
* 停止按钮点击事件
*/
public function stopBtnClick(e:MouseEvent):void
{
change.stop();
position = 0;
playBtn.label = "播放";
play_tag = 0;
}
/**
* ...........................
* 设置播放时间和音频总时间显示
*/
public function getStrTime(num:int):String
{
var minutes:int;//分钟
var seconds:int;//秒
var minStr:String;//字符窜分钟
var secStr:String;//字符窜秒
minutes = Math.round(Math.round(num / 1000) / 60);
seconds = Math.round(Math.round(num / 1000) % 60);
minStr =minutes.toString();
if (minutes < 10)
{
minStr = "0" + minutes;
}
secStr = seconds.toString();
if (seconds < 10)
{
secStr = "0" + seconds;
}
return minStr + ":" + secStr;
}
/**
* ...........................
* 侦听事件
*/
public function enterFrameHandler(e:Event):void
{
//设置进度条的宽度和高度
var barWidth:int = 500;
var barHeight:int = 15;
//已经加载字节
var loaded:int = sound.bytesLoaded;
//总字节
var total:int = sound.bytesTotal;
//获取音频总大小
var length:int = sound.length;//当前加载声音的长度
trace("length前:" + length);
//绘制进度条
graphics.clear();
graphics.beginFill(0x000000);
graphics.drawRect(10, 10, barWidth, barHeight);
graphics.endFill();
if (total > 0)
{
//加载进度
var precentBuffered:Number = loaded / total;
//绘制加载进度条
graphics.beginFill(0xcccccc);
graphics.drawRect(10, 10, barWidth * precentBuffered, barHeight)
graphics.endFill();
if (play_tag == 1)
{
//获取当前播放位置
position = change.position;
trace("position:" + position);
//设置音频总大小
length /= precentBuffered;
trace("length后:" + length);
//获取播放比例
var barpos:Number = position / length;
//绘制播放进度条
graphics.beginFill(0x666666);
graphics.drawRect(10, 10, barWidth * barpos, barHeight);
graphics.endFill();
lab.text = "播放进度:" + barpos * 100 + "%";
}
//设置播放时间和音频总时间显示
labCurrentTime.text = getStrTime(position);
labTotalTime.text = getStrTime(length);
trace("加载进度为:" + Math.round(precentBuffered * 100) + "%");
trace("play_tag:......................"+play_tag);
}
}
}
}
推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架