Flash 中的流式音频多次播放且重叠

发布于 2024-12-04 12:08:02 字数 778 浏览 0 评论 0原文

以下 AS3 代码有时会导致音频多次播放,几乎同时播放,就像疯狂的回声一样。它通常可以正常使用该 URL,但是当我使用 https://soundcloud.com url 时,它总是会崩溃。在极少数情况下,我认为甚至本地文件也会出现此问题。我从其他地方复制了这段代码,所以我不完全理解它。您是否发现此实现有问题,还是 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();  
}

The following AS3 code is sometimes causing the audio to play multiple times, almost simultaneously like a crazy echo. It usually works ok with that URL but when I use a https://soundcloud.com url it always freaks out. On rare occasions I think the issue has happened with local files even. I copied this code from somewhere else so I don't entirely understand it. Do you see a problem with this implementation or is Flash just crazy?

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();  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独闯女儿国 2024-12-11 12:08:02

您在 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 代码,因为它没有执行任何功能。

You are calling play() twice on the Sound object. Once when you create the variable song and again when the file is done loading.

You might want to structure your code differently.

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 );
}

I removed the Timer code as it did not do anything functional.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文