Flash CS4控制AS3中動(dòng)畫聲音的播放或停止
發(fā)布時(shí)間:2015-04-09 16:36:44 作者:佚名
我要評(píng)論

這篇教程是向大家介紹Flash CS4控制AS3中動(dòng)畫聲音的播放或停止方法,教程很不錯(cuò),又碰到同樣的朋友可以參考一下,希望能對(duì)大家有所幫助
今天有閃友問(wèn)到如何控制AS3中的聲音問(wèn)題,用下面的小實(shí)例說(shuō)明:
以上就是Flash CS4控制AS3中動(dòng)畫聲音的播放或停止方法,希望能對(duì)大家有所幫助!
復(fù)制代碼
代碼如下:/*
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);
//用于記錄音樂(lè)現(xiàn)在是否為暫停狀態(tài)
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();
//成功加載數(shù)據(jù)后
soundFactory.addEventListener(Event.COMPLETE, completeHandler);
//在存在可用于 MP3 聲音的 ID3 數(shù)據(jù)時(shí)
soundFactory.addEventListener(Event.ID3, id3Handler);
//加載音樂(lè)錯(cuò)誤時(shí)
soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
//音樂(lè)加載中...
soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler);
soundFactory.load(mp3_request);
channel = soundFactory.play();
//音樂(lè)播放完成
channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
//用Timer監(jiān)聽音樂(lè)的播放進(jìn)度
positionTimer = new Timer(1000);
positionTimer.addEventListener(TimerEvent.TIMER, positionTimerHandler);
positionTimer.start();
//創(chuàng)建一個(gè)按鈕,用于播放音樂(lè)
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);
//創(chuàng)建一個(gè)按鈕,用于停止音樂(lè)
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);
}
//監(jiān)聽音樂(lè)的播放進(jìn)度
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("音樂(lè)總長(zhǎng)度:"+zcd, "音樂(lè)已播放:"+ybf, "播放進(jìn)度為:"+bfs+"%");
}
//加載音樂(lè)完成時(shí)
private function completeHandler(event:Event):void {
//trace("加載音樂(lè)完成: " + event);
}
//在存在可用于MP3聲音的ID3數(shù)據(jù)時(shí)
private function id3Handler(event:Event):void {
//trace("音樂(lè)的ID3信息如下:");
for (var s in soundFactory.id3) {
//trace("\t", s, ":", soundFactory.id3[s]);
}
//trace("關(guān)于ID3信息介紹,請(qǐng)參見Sound類-->屬性-->id3");
}
//加載音樂(lè)錯(cuò)誤時(shí)
private function ioErrorHandler(event:Event):void {
//trace("加載音樂(lè)錯(cuò)誤,錯(cuò)誤信息如下:" + event);
positionTimer.stop();
}
//加載音樂(lè)時(shí)
private function progressHandler(eventrogressEvent):void {
var yjz:int = event.bytesLoaded;
var zcd:int = event.bytesTotal;
var bfs:int = Math.floor(yjz/zcd*100);
//trace("音樂(lè)總長(zhǎng)度:"+zcd,"已加載: "+yjz, "加載進(jìn)度為:"+bfs+"%");
}
//音樂(lè)播放完成
private function soundCompleteHandler(event:Event):void {
//trace("音樂(lè)播放完成: " + event);
positionTimer.stop();
}
//點(diǎn)擊播放按鈕事件
private function soundPlay(event:MouseEvent):void {
if (bSoundStop) {
bSoundStop = false;
channel = soundFactory.play(channel.position.toFixed(0));
}
}
//點(diǎn)擊停止按鈕事件
private function soundStop(event:MouseEvent):void {
if (!bSoundStop) {
bSoundStop = true;
channel.stop();
}
}
}
}
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);
//用于記錄音樂(lè)現(xiàn)在是否為暫停狀態(tài)
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();
//成功加載數(shù)據(jù)后
soundFactory.addEventListener(Event.COMPLETE, completeHandler);
//在存在可用于 MP3 聲音的 ID3 數(shù)據(jù)時(shí)
soundFactory.addEventListener(Event.ID3, id3Handler);
//加載音樂(lè)錯(cuò)誤時(shí)
soundFactory.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
//音樂(lè)加載中...
soundFactory.addEventListener(ProgressEvent.PROGRESS, progressHandler);
soundFactory.load(mp3_request);
channel = soundFactory.play();
//音樂(lè)播放完成
channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
//用Timer監(jiān)聽音樂(lè)的播放進(jìn)度
positionTimer = new Timer(1000);
positionTimer.addEventListener(TimerEvent.TIMER, positionTimerHandler);
positionTimer.start();
//創(chuàng)建一個(gè)按鈕,用于播放音樂(lè)
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);
//創(chuàng)建一個(gè)按鈕,用于停止音樂(lè)
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);
}
//監(jiān)聽音樂(lè)的播放進(jìn)度
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("音樂(lè)總長(zhǎng)度:"+zcd, "音樂(lè)已播放:"+ybf, "播放進(jìn)度為:"+bfs+"%");
}
//加載音樂(lè)完成時(shí)
private function completeHandler(event:Event):void {
//trace("加載音樂(lè)完成: " + event);
}
//在存在可用于MP3聲音的ID3數(shù)據(jù)時(shí)
private function id3Handler(event:Event):void {
//trace("音樂(lè)的ID3信息如下:");
for (var s in soundFactory.id3) {
//trace("\t", s, ":", soundFactory.id3[s]);
}
//trace("關(guān)于ID3信息介紹,請(qǐng)參見Sound類-->屬性-->id3");
}
//加載音樂(lè)錯(cuò)誤時(shí)
private function ioErrorHandler(event:Event):void {
//trace("加載音樂(lè)錯(cuò)誤,錯(cuò)誤信息如下:" + event);
positionTimer.stop();
}
//加載音樂(lè)時(shí)
private function progressHandler(eventrogressEvent):void {
var yjz:int = event.bytesLoaded;
var zcd:int = event.bytesTotal;
var bfs:int = Math.floor(yjz/zcd*100);
//trace("音樂(lè)總長(zhǎng)度:"+zcd,"已加載: "+yjz, "加載進(jìn)度為:"+bfs+"%");
}
//音樂(lè)播放完成
private function soundCompleteHandler(event:Event):void {
//trace("音樂(lè)播放完成: " + event);
positionTimer.stop();
}
//點(diǎn)擊播放按鈕事件
private function soundPlay(event:MouseEvent):void {
if (bSoundStop) {
bSoundStop = false;
channel = soundFactory.play(channel.position.toFixed(0));
}
}
//點(diǎn)擊停止按鈕事件
private function soundStop(event:MouseEvent):void {
if (!bSoundStop) {
bSoundStop = true;
channel.stop();
}
}
}
}
以上就是Flash CS4控制AS3中動(dòng)畫聲音的播放或停止方法,希望能對(duì)大家有所幫助!
相關(guān)文章
flash cs6鼠標(biāo)跟隨效果實(shí)現(xiàn)代碼分享
flash cs6想要實(shí)現(xiàn)鼠標(biāo)跟隨效果?該怎么制作呢?今天我們就來(lái)看看使用as2.0實(shí)現(xiàn)鼠標(biāo)跟隨效果的教程,需要的朋友可以參考下2019-05-19- Flash cs6怎么使用代碼輸入中英文文本?Flash cs6中可以使用文字工具直接輸入文本,也可以使用代碼來(lái)輸入文本,該怎么使用代碼輸入文本呢?請(qǐng)看下文詳細(xì)的教程,需要的朋友2018-03-11
- flash as3.0抽象類怎么定義? as3.0中有很多抽象類,該怎么定義抽象類和抽象方法呢?下面我們就來(lái)看看簡(jiǎn)單的例子,需要的朋友可以參考下http://www.dbjr.com.cn/softs/408402.2018-02-28
flash cs6中怎么使用ActionScript3.0?
flash cs6中怎么使用ActionScript3.0?flash cs6中想要使用ActionScript3.0功能,該怎么使用呢?下面我們就來(lái)看看詳細(xì)的教程,需要的朋友可以參考下2018-01-25Flash中怎么實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊決定圖像位置?
本教程給大家分享一個(gè)Flash小教程,教大家在Flash CS6中怎么實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊決定圖像位置?方法很簡(jiǎn)單,感興趣的朋友歡迎前來(lái)一起分享學(xué)習(xí)2018-01-12Flash中如何用代碼將圖片放在自己想要的舞臺(tái)位置?
本教程教腳本之家的ActionScript教程學(xué)習(xí)者在Flash中如何用代碼將圖片放在自己想要的舞臺(tái)位置,教程講解的詳細(xì),感興趣的朋友歡迎前來(lái)分享學(xué)習(xí)2017-11-20在Flash CS6中使用with函數(shù)繪制背景圖教程
本教程教腳本之家的ActionScript教程學(xué)習(xí)者如何在Flash CS6中使用with函數(shù)繪制背景圖?教程一步步講解的挺詳細(xì),方法也不難,非常適合Flash新手入門學(xué)習(xí)2017-11-18Flash怎么設(shè)置元件坐標(biāo)?flash使用代碼設(shè)置元件的坐標(biāo)的教程
Flash怎么設(shè)置元件坐標(biāo)?flash中導(dǎo)如的元件需要添加坐標(biāo),該怎么定位元件坐標(biāo)呢?下面我們就來(lái)看看flash使用代碼設(shè)置元件的坐標(biāo)的教程,需要的朋友可以參考下2017-10-11Flash怎么制作來(lái)回?fù)u擺的花朵的動(dòng)畫?
Flash怎么制作來(lái)回?fù)u擺的花朵的動(dòng)畫?Flash中想要給花朵制作一段搖擺的動(dòng)畫效果,該怎么制作呢?下面我們就來(lái)看看詳細(xì)的教程,很簡(jiǎn)單,需要的朋友可以參考下2017-05-23- Flash怎么制作流動(dòng)七彩色的文字?想要讓文字動(dòng)起來(lái),該怎么使用flash給文字制作一個(gè)流動(dòng)七彩色的動(dòng)畫呢?下面我們就來(lái)看看詳細(xì)的教程,需要的朋友可以參考下2017-04-23