Actionscript 3 声音流停止加载
我这里有一个小的声音流脚本,但有时如果在当前曲目加载完成之前按play键播放下一首曲目,则无法完成下一首曲目的加载Actionscript 3 声音流停止加载,actionscript-3,Actionscript 3,我这里有一个小的声音流脚本,但有时如果在当前曲目加载完成之前按play键播放下一首曲目,则无法完成下一首曲目的加载 package player { import flash.events.Event; import flash.display.Sprite; import flash.external.ExternalInterface; import flash.media.Sound; import flash.media.SoundChannel;
package player {
import flash.events.Event;
import flash.display.Sprite;
import flash.external.ExternalInterface;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import player.Loader_bar;
public class Stream extends Sprite {
private var _Sound = null;
private var _Channel = null;
private var isLoading = false;
private var _Loader_bar = null;
public var loader_color = null;
public var loader_width = 0;
public var loader_height = 0;
private var i = 0;
public function Stream(){
this._Loader_bar = new Loader_bar();
addChild(this._Loader_bar);
}
public function cnstr(){
this._Loader_bar.color = this.loader_color;
this._Loader_bar.w = this.loader_width;
this._Loader_bar.h = this.loader_height;
this._Loader_bar.cnstr();
}
public function play(url){
this.stop();
this.close();
this._Sound = new Sound();
this.isLoading = true;
this.addEventListener(Event.ENTER_FRAME, listener_bytesLoaded);
this._Sound.addEventListener(Event.COMPLETE, listener_loadedComplete);
this._Sound.load(new URLRequest(url));
this._Channel = this._Sound.play();
}
public function stop(){
if(this._Channel){
this._Channel.stop();
}
}
private function close(){
if(this.isLoading){
this._Sound.close();
}
}
private function listener_loadedComplete(event){
this.close();
this.isLoading = false;
this.removeEventListener(Event.ENTER_FRAME, listener_bytesLoaded);
}
private function listener_bytesLoaded(event){
var float = this._Sound.bytesLoaded / this._Sound.bytesTotal;
this._Loader_bar.progress(float);
var data = {
i : this.i,
float : float,
loaded : this._Sound.bytesLoaded,
total : this._Sound.bytesTotal
};
ExternalInterface.call('swf2js', 'tst_progress', data);
this.i++;
}
}
}
close():void
关闭流,导致任何数据下载停止
试试这个:
创建一个类似于hasload的布尔值,将其设置为false。成功加载声音后,将其设置为true
然后,当您播放声音时,可以在play()函数中测试hasload。如果在加载上一个声音之前调用了play(),hasload将为false,在这种情况下,在创建和加载新声音之前调用。\u sound.close()。测试而不只是调用close()的原因是,如果您暂停了流,就不必重新加载流来再次播放
补充:
关于负载未正确报告,您的进度逻辑设置不正确。试试这个:
1) 导入flash.events.ProgressEvent
2) 对于侦听器,替换此.addEventListener(Event.ENTER_FRAME,listener_bytesload);在您的play()方法中使用此命令。_Sound.addEventListener(ProgressEvent.PROGRESS,listener_bytesload)
3) 将listener_bytesLoaded()方法更改为以下内容:
private function listener_bytesLoaded(event:ProgressEvent)
{
var percent = event.bytesLoaded / event.bytesTotal;
this._Loader_bar.progress(percent);
var data = {
i : this.i,
float : percent,
loaded : event.bytesLoaded,
total : event.bytesTotal
};
ExternalInterface.call('swf2js', 'tst_progress', data);
this.i++;
}
4) 更改此选项。removeEventListener(Event.ENTER_FRAME,listener_bytesloadded);
在侦听器中,将\u loadedComplete()方法加载到此中。\u Sound.removeEventListener(ProgressEvent.PROGRESS,listener\u bytesload);然后将其移出该方法,并将其放入close()方法中的条件中
注意-我实际上并没有编译这个,但我认为它很好。希望有帮助。:) 谢谢。。现在它加载了整个音轨:)但是你能告诉我为什么已经加载的音轨在这里返回0吗?即使已完成加载(chached),加载程序状态也为0%。。我已经更新了我的代码:)我已经在上面的评论中添加了关于您的问题的编辑。