AS3 음향 효과 관리
8261 단어 AS3 의 음악 관리ActionScript3
XML 로 외부 MP3 를 불 러 와 MySoundModel 에 저장 하고 SoundBaseManager. as 로 음악 재생 관 리 를 통일 합 니 다.
코드:
mySounds.xml:
MySoundModel.as:
/**
* MODEL
* @author Aonaufly
* */
public class MySoundModel{
public var $id : int;
public var $name : String;
public var $url : String;
public var $type : String;
}
MySoundVO.as:
/**
* VO
* */
public class MySoundVO{
private var $data : Vector.;
private var $dic_sound : Dictionary;
private var $cur_Index : uint = 0;
private var $delay : int;
private var $try : uint;
private var $count : uint;
private var $isOK : Boolean = false;
//private var $sound : Sound;
/**
* @param $try :
* @param $delay :
* */
public function MySoundVO($try: uint = 2 , $delay : int = 1000){
this.$try = $try;
this.$delay = $delay;
loaderXML();
}
private function loaderXML() : void{
var $loader : URLLoader = new URLLoader();
$loader.addEventListener(Event.COMPLETE,__ $loader.addEventListener(IOErrorEvent.IO_ERROR,__onError);
$loader.load(new URLRequest(Global.baseUrl+"resources/mySounds.xml"));
}
private function __ ( e : Event ) :void{
try{
clearTimeout($count);
}catch( e : Error){}
var $myxml : XML = new XML(e.target.data);
trace(" : " + $myxml);
$data = new Vector.();
var $cell : MySoundModel;
for each( var $team : XML in $myxml.sounds.info){
$cell = new MySoundModel();
$cell.$id = $team.@id;
$cell.$name = $team.@name;
$cell.$url = $team.@url;
$cell.$type = $team.@type;
$data.push($cell);
}
$count = 0;
$dic_sound = new Dictionary();
creatSound();
}
/**
* Sound
* */
private function creatSound() : void{
if($data != null && $count < $data.length){
var $sound : Sound = new Sound();
$sound.addEventListener(Event.COMPLETE,__onSoundLoaded);
$sound.addEventListener(IOErrorEvent.IO_ERROR,__onSoundError);
$sound.load(new URLRequest(Global.baseUrl + $data[$count].$url));
//return $sound;
}else{
//return null;
$isOK = true;//
}
}
private function __onSoundLoaded( e : Event ) : void{
var $mySound : Sound = e.target as Sound;
var $id : int = $data[$count].$id;
$dic_sound[$id] = $mySound;
$count += 1;
$mySound.removeEventListener(Event.COMPLETE,__onSoundLoaded);
$mySound.removeEventListener(IOErrorEvent.IO_ERROR,__onSoundError);
creatSound();
}
private function __onSoundError( e : IOErrorEvent ) : void{
var $mySound : Sound = e.target as Sound;
$mySound.removeEventListener(Event.COMPLETE,__onSoundLoaded);
$mySound.removeEventListener(IOErrorEvent.IO_ERROR,__onSoundError);
var $id : int = $data[$count].$id;
trace(" ID : " + $id);
$count += 1;
creatSound();
}
private function __onError ( e : IOErrorEvent ) : void{
if($cur_Index < $try){
$count = setTimeout(loaderXML,$delay);
}
$cur_Index += 1;
trace(" !!!");
}
/**
* ID
* */
public function getSoundByID($soundID : int ) : Sound{
if( null != $dic_sound && $dic_sound[$soundID] != null){
return $dic_sound[$soundID];
}else{
return null;
}
}
/**
* Name
* */
public function getSoundByName($soundName : String ) : Sound{
if(null != $data && $data.length > 0){
var $$ii : int = 0;
var $$len : uint = $data.length;
while($$ii < $$len){
if($data[$$ii].$name == $soundName){
return $dic_sound[($data[$$ii].$id)];
break;
}
$$ii += 1;
}
return null;
}else{
return null;
}
}
private static var $instance : MySoundVO;
public static function get Ins() : MySoundVO{
if( null == $instance ) $instance = new MySoundVO();
return $instance;
}
private function clone(source:Object):Sound{
var copier:ByteArray=new ByteArray();
copier.writeObject(source);
copier.position=0;
return(copier.readObject());
}
}
SoundBaseManager.as:
/**
*
* @author Aonaufly
* */
public class SoundBaseManager{
private static var $instance : SoundBaseManager;
public static function get Ins() : SoundBaseManager{
if( null == $instance ) $instance = new SoundBaseManager();
return $instance;
}
public function SoundBaseManager() : void{
$soundChannelFormat = new SoundTransform();
$soundChannelFormat.volume = 1;
}
private var $len : uint;
private var $channel : SoundChannel;
private var $clear : uint = 0;
private var $soundChannelFormat : SoundTransform;
/***
*
* */
public function catchSounds() : void{
MySoundVO.Ins;
}
/**
*
* @param $id : ID
* @param $len :
* */
public function playById($id : int,$len : uint = 0) : void{
var $mySound : Sound = MySoundVO.Ins.getSoundByID($id);
if(null != $mySound){
this.$len = $len;
channelPaly($mySound);
}
}
/**
*
* @param $name :
* @param $len :
* */
public function playByName($name : String,$len : uint = 0) : void{
var $mySound : Sound = MySoundVO.Ins.getSoundByName($name);
if(null != $mySound){
this.$len = $len;
channelPaly($mySound);
}
}
private function channelPaly($mySound : Sound):void{
channelStop();
if($channel == null){
$channel = new SoundChannel();
$channel.soundTransform = $soundChannelFormat;
}
if(this.$len != 0 ){
if(this.$len > $mySound.length){
$len = $mySound.length;
}
$channel = $mySound.play(0,1);
$clear = setTimeout(channelStop,$len);
}else{
$channel = $mySound.play(0,1);
}
}
private function channelStop() : void{
if(null != $channel){
$channel.stop();
}
if($clear != 0){
try{
clearTimeout($clear);
$clear = 0;
}catch( e : Error ){}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
VS Code로 Apache Flex 데스크톱 앱 디버깅Visual Studio Code(VS Code)를 사용하여 Apache Flex 데스크톱 앱을 디버깅합니다. VS Code 1.50.1 Windows 10 Apache Flex 4.16.1 Adobe AIR 32....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.