使用函数时出现奇怪的 NetStream 问题
此代码导致我的 f4v 文件过早停止播放。时间发生变化,但大约 8-10 秒。
loadSong();
function loadSong()
{
if(!songPlaying)
{
songPlaying = true;
var customClient:Object = new Object();
customClient.onCuePoint = cuePointHandler;
customClient.onMetaData = metaDataHandler;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = customClient;
ns.play("song.f4v");
}
trace("HERE");
}
function cuePointHandler(infoObject:Object):void{
trace(infoObject.name);
}
function metaDataHandler(infoObject:Object):void {
trace("metaData");
}
此代码让 f4v 播放到最后。卧槽!?看来当我通过函数调用它时会导致问题。仅供参考,代码存储在主时间线的第一帧中,F4v 仅是音频。任何帮助将不胜感激。
if(!songPlaying)
{
songPlaying = true;
var customClient:Object = new Object();
customClient.onCuePoint = cuePointHandler;
customClient.onMetaData = metaDataHandler;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = customClient;
ns.play("song.f4v");
}
This code causes my f4v file to stop playing prematurely. Time changes but roughly 8-10 seconds in.
loadSong();
function loadSong()
{
if(!songPlaying)
{
songPlaying = true;
var customClient:Object = new Object();
customClient.onCuePoint = cuePointHandler;
customClient.onMetaData = metaDataHandler;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = customClient;
ns.play("song.f4v");
}
trace("HERE");
}
function cuePointHandler(infoObject:Object):void{
trace(infoObject.name);
}
function metaDataHandler(infoObject:Object):void {
trace("metaData");
}
This code let's the f4v play till the end. WTF!? It seems that when I call it via a function it causes the issue. FYI the code is stored in the first frame of the main timeline, and the F4v is audio only. Any help would be appreciated.
if(!songPlaying)
{
songPlaying = true;
var customClient:Object = new Object();
customClient.onCuePoint = cuePointHandler;
customClient.onMetaData = metaDataHandler;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = customClient;
ns.play("song.f4v");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在函数内声明
NetConection
和NetStream
时,发生的情况是该变量的作用域是该函数的本地范围。这意味着没有其他任何东西引用您创建的 NetConnection,因此垃圾收集器会在下次运行期间将其清除(这就是您看到可变时间的原因)。当您仅在 if 语句中声明它时,变量位于电影的范围内,并且保存对它们的引用,因此不会被垃圾收集。
我不知道其余代码的架构是什么,但如果您想使用函数来保存代码,请尝试为
var nc:NetConnection = new NetConnection(); 声明> 就在
loadSong();
语句之前。从架构上来说,您可能希望在框架之外重构代码,但如果只是几行代码,那么可能真的不值得。仅取决于您的项目。
有关垃圾收集的详细信息,请查看了解 Flash Player 9 中的垃圾收集 (上面说的是 Flash Player 9,但这也适用于 10)。
What is happening, when you declare your
NetConection
andNetStream
inside the function, is that the scope for that variable is local to that function. That means that nothing else is referencing theNetConnection
you created and thus the garbage collector sweeps it up during its next run (that's why you are seeing the variable time).When you declare it in just an if statement the variables are in the scope of the movie and that holds a reference to them and thus aren't garbage collected.
I don't know what the architecture is for the rest of your code, but if you want to use functions to hold your code, try putting a declaration for the
var nc:NetConnection = new NetConnection();
just before theloadSong();
statement.Architecturally, you might want to refactor your code out of the frame, but it might really not be worth it, if it is just couple of lines of code. Just depends on your project.
For more information about garbage collection, check out Understanding garbage collection in Flash Player 9 (It's says Flash Player 9, but this is applicable to 10 as well).