Flash CS4如何控制動畫聲音的停止和播放_Flash教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:分享Flash處理安全策略的經驗很多人在處理安全策略時遇到問題,我自己也總結了一些經驗,供大家參考吧.在 Flash Player 9.0.124.0 及以后的版本中,socket策略文件對于任何 Socket 連接都是必需的。也就是說,無論連接到哪一個端口(即使連接到提供 SWF 文件的同一主機上的端口),目標主
今天有閃友問到如何控制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();
}
}
}
}
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入門實例:制作傳統風格卷簾動畫本例為Flash鼠繪新手入門系列課程,今天我們來學習傳統風格卷簾畫的繪制,教程講解的比較詳細,適合Flash初學者學習,希望朋友們喜歡。 做了個簾畫,我把做的過程寫出來,供初學FLASHR 朋友們交流練習。 先看下效果吧: 一、畫簾片: 1.新建一圖形元件,名“
相關Flash教程:
- 相關鏈接:
- 教程說明:
Flash教程-Flash CS4如何控制動畫聲音的停止和播放。