欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Flash CS4控制AS3中动画声音的播放或停止

程序员文章站 2023-11-17 17:35:10
这篇教程是向大家介绍Flash CS4控制AS3中动画声音的播放或停止方法,教程很不错,又碰到同样的朋友可以参考一下,希望能对大家有所帮助... 15-04-09...
今天有闪友问到如何控制as3中的声音问题,用下面的小实例说明:


复制代码
代码如下:
/*
as3sound.as
*/
package {
import flash.display.sprite;
import flash.events.*;
import flash.media.sound;
import flash.media.soundchannel;
import flash.net.urlrequest;
import flash.utils.timer;
import flash.text.textfield;
import flash.text.textfieldautosize;
import flash.filters.dropshadowfilter;
public class as3sound extends sprite {
private var url:string = "http://sxl001.xfyun.com/music/lib/myrussia.mp3";
private var soundfactory:sound;
private var channel:soundchannel;
private var positiontimer:timer;
private var play_btn:sprite;
private var stop_btn:sprite;
private var d_filtersropshadowfilter=new dropshadowfilter(5,45,0x000000,80,8,8);
//用于记录音乐现在是否为暂停状态
private var bsoundstop:boolean = false;
public function as3sound() {
var sxl_txt:textfield = new textfield();
sxl_txt.text="cs4中如何控制声音的播放或停止的";
sxl_txt.autosize=textfieldautosize.left;
sxl_txt.x=stage.stagewidth/2-sxl_txt.width/2;
sxl_txt.y=20;
addchild(sxl_txt);
var mp3_request:urlrequest = new urlrequest(url);
soundfactory = new sound();
//成功加载数据后
soundfactory.addeventlistener(event.complete, completehandler);
//在存在可用于 mp3 声音的 id3 数据时
soundfactory.addeventlistener(event.id3, id3handler);
//加载音乐错误时
soundfactory.addeventlistener(ioerrorevent.io_error, ioerrorhandler);
//音乐加载中...
soundfactory.addeventlistener(progressevent.progress, progresshandler);
soundfactory.load(mp3_request);
channel = soundfactory.play();
//音乐播放完成
channel.addeventlistener(event.sound_complete, soundcompletehandler);
//用timer监听音乐的播放进度
positiontimer = new timer(1000);
positiontimer.addeventlistener(timerevent.timer, positiontimerhandler);
positiontimer.start();
//创建一个按钮,用于播放音乐
play_btn = new sprite();
play_btn.graphics.beginfill(0xffcc32);
play_btn.graphics.drawroundrect(0, 0, 70, 18, 10, 10);
play_btn.graphics.endfill();
var play_txt:textfield = new textfield();
play_txt.text = "播放";
play_txt.x=18;
play_btn.x=50;
play_btn.y=100;
play_txt.selectable = false;
play_btn.addchild(play_txt);
play_btn.filters=[d_filters];
play_btn.addeventlistener(mouseevent.click, soundplay);
addchild(play_btn);
//创建一个按钮,用于停止音乐
stop_btn = new sprite();
stop_btn.graphics.beginfill(0xffcc32);
stop_btn.graphics.drawroundrect(0, 0, 70, 18, 10, 10);
stop_btn.graphics.endfill();
stop_btn.x=130;
stop_btn.y=100;
var stop_txt:textfield = new textfield();
stop_txt.x=18;
stop_txt.text = "暂停";
stop_txt.selectable = false;
stop_btn.addchild(stop_txt);
stop_btn.filters=[d_filters];
stop_btn.addeventlistener(mouseevent.click, soundstop);
addchild(stop_btn);
}
//监听音乐的播放进度
private function positiontimerhandler(event:timerevent):void {
var ybf:int = channel.position.tofixed(0);
var zcd:int = soundfactory.length;
var bfs:int = math.floor(ybf/zcd*100);
//trace("音乐总长度:"+zcd, "音乐已播放:"+ybf, "播放进度为:"+bfs+"%");
}
//加载音乐完成时
private function completehandler(event:event):void {
//trace("加载音乐完成: " + event);
}
//在存在可用于mp3声音的id3数据时
private function id3handler(event:event):void {
//trace("音乐的id3信息如下:");
for (var s in soundfactory.id3) {
//trace("\t", s, ":", soundfactory.id3[s]);
}
//trace("关于id3信息介绍,请参见sound类-->属性-->id3");
}
//加载音乐错误时
private function ioerrorhandler(event:event):void {
//trace("加载音乐错误,错误信息如下:" + event);
positiontimer.stop();
}
//加载音乐时
private function progresshandler(eventrogressevent):void {
var yjz:int = event.bytesloaded;
var zcd:int = event.bytestotal;
var bfs:int = math.floor(yjz/zcd*100);
//trace("音乐总长度:"+zcd,"已加载: "+yjz, "加载进度为:"+bfs+"%");
}
//音乐播放完成
private function soundcompletehandler(event:event):void {
//trace("音乐播放完成: " + event);
positiontimer.stop();
}
//点击播放按钮事件
private function soundplay(event:mouseevent):void {
if (bsoundstop) {
bsoundstop = false;
channel = soundfactory.play(channel.position.tofixed(0));
}
}
//点击停止按钮事件
private function soundstop(event:mouseevent):void {
if (!bsoundstop) {
bsoundstop = true;
channel.stop();
}
}
}
}

以上就是flash cs4控制as3中动画声音的播放或停止方法,希望能对大家有所帮助!