flash - Flash中的流音频播放多次,重叠

标签 flash actionscript-3 audio stream

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

最佳答案

您正在play()对象上两次调用Sound。在创建变量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代码,因为它没有任何功能。

关于flash - Flash中的流音频播放多次,重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7395568/

相关文章:

java - 如何在 Swing 组件中显示 Flash 动画?

actionscript-3 - Adobe AIR Android FileStream 问题

actionscript-3 - 如何使我的PHP Socket Server将策略文件发送到Flash客户端?

linux - 用于具有立体声或多个音频流的媒体流的音频持续时间的特定 mediainfo 命令

xcode - SimpleAudioEngine:手机休眠后没有音乐

java - 从网站将带照片的身份证打印到塑料证卡打印机

flash - 谷歌字体 API 和 Actionscript

iphone - 在没有 iTunes/越狱/AppleDev 的情况下在 iOS 上测试 Flash 游戏?

Flash Builder - 如何构建或包含 SWC

android - Android Record Audio没有SDCARD