Gapless sound looping in ActionScript 3.0
보통 사운드 루핑을 만들 때 Event.SOUND_COMPLETE 이벤트나 Sound.play(0, 999);를 사용할텐데요. 이 경우에 음악이 재생되는 사이에 약간의 틈이 발생합니다. 다음 플래시 무비처럼 말이죠.
이 파일은 아래의 코드와 같이, Event.SOUND_COMPLETE를 사용하여 사운드를 루프시키고 있습니다.
import flash.media.*;
import flash.events.*;
import flash.utils.Timer;
var snd:Sound = new loop();
var chn:SoundChannel;
function playSound():void
{
if(chn){
chn.removeEventListener(Event.SOUND_COMPLETE, sndListener);
}
chn = snd.play();
chn.addEventListener(Event.SOUND_COMPLETE, sndListener);
}
function sndListener(e:Event):void
{
playSound();
}
lbl.text = "Loop with Event.SOUND_COMPLETE";
btn2.enabled = false;
btn1.addEventListener(MouseEvent.CLICK, clickListener1);
btn2.addEventListener(MouseEvent.CLICK, clickListener2);
function clickListener1(e:MouseEvent):void
{
playSound();
btn1.enabled = false;
btn2.enabled = true;
}
function clickListener2(e:MouseEvent):void
{
btn1.enabled = true;
btn2.enabled = false;
chn.stop();
}
이 문제를 해결하기 위해서 이것저것 테스트 해 보다가, Timer를 사용하여 현재 재생중인 음악이 끝나기 전에 미리 새로운 음악을 재생시켜 주어서, 틈이 생기지 않도록 하는 방법을 생각하게 되었습니다. 아래의 플래시 무비에서 그 결과를 확인할 수 있습니다.
사용된 코드는 다음과 같습니다.
import flash.media.*;
import flash.events.*;
import flash.utils.Timer;
var snd:Sound = new loop();
var chn:SoundChannel;
var timer:Timer = new Timer(10);
timer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener(e:TimerEvent):void
{
if(chn && chn.position > snd.length - 450){
playSound();
}
}
function playSound():void
{
if(! timer.running){
timer.start();
}
chn = snd.play();
}
lbl.text = "Loop with TimerEvent.TIMER";
btn2.enabled = false;
btn1.addEventListener(MouseEvent.CLICK, clickListener1);
btn2.addEventListener(MouseEvent.CLICK, clickListener2);
function clickListener1(e:MouseEvent):void
{
playSound();
btn1.enabled = false;
btn2.enabled = true;
}
function clickListener2(e:MouseEvent):void
{
btn1.enabled = true;
btn2.enabled = false;
chn.stop();
timer.reset();
}
답글 남기기