2011-09-13 14 views
0

以下AS3代码有时会导致音频播放多次,几乎同时像一个疯狂的回声。它通常适用于该URL,但是当我使用url时,它总是令人发狂。在极少数情况下,我认为这个问题甚至发生在本地文件上。我从别的地方复制了这段代码,所以我不完全理解它。你看到这个实现有问题,或者Flash只是疯了吗?在Flash中流式传输音频正在播放多次,重叠

var url:String = "http://md9.ca/portfolio/music/seaforth.mp3"; 

var request:URLRequest = new URLRequest(url); 
var s:Sound = new Sound(); 
s.addEventListener(Event.COMPLETE, completeHandler); 
s.load(request); var song:SoundChannel = s.play(); 
song.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler); 


var time:Timer = new Timer(20); 
time.start(); 

function completeHandler(event:Event):void {  
    event.target.play(); 
} 

function soundCompleteHandler(event:Event):void { 
    time.stop(); 
} 

回答

2

您的Sound对象上调用play()两次。一旦创建变量song并再次完成文件加载时。

您可能想要以不同的方式构造代码。

var url:String = "http://md9.ca/portfolio/music/seaforth.mp3"; 

var song:SoundChannel; 
var request:URLRequest = new URLRequest(url); 
var s:Sound = new Sound(); 
s.addEventListener(Event.COMPLETE, onLoadComplete); 
s.load(request); 

function onLoadComplete(event:Event):void 
{  
    song = s.play(); 
    song.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler); 
    s.removeEventListener(Event.COMPLETE, onLoadComplete); 
} 

function soundCompleteHandler(event:Event):void 
{ 
    trace('sound is complete'); 
    song.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler); 
} 

我删除了Timer代码,因为它没有做任何事情的功能。

+0

谢谢你,完美的作品!我想我应该看到这个游戏发生了两次。 – Moss