使用函数时出现奇怪的 NetStream 问题

发布于 2024-10-04 16:30:33 字数 1233 浏览 6 评论 0原文

此代码导致我的 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 技术交流群。

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

发布评论

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

评论(1

攀登最高峰 2024-10-11 16:30:33

当您在函数内声明 NetConectionNetStream 时,发生的情况是该变量的作用域是该函数的本地范围。这意味着没有其他任何东西引用您创建的 NetConnection,因此垃圾收集器会在下次运行期间将其清除(这就是您看到可变时间的原因)。

当您仅在 if 语句中声明它时,变量位于电影的范围内,并且保存对它们的引用,因此不会被垃圾收集。

我不知道其余代码的架构是什么,但如果您想使用函数来保存代码,请尝试为 var nc:NetConnection = new NetConnection(); 声明> 就在 loadSong(); 语句之前。

从架构上来说,您可能希望在框架之外重构代码,但如果只是几行代码,那么可能真的不值得。仅取决于您的项目。

有关垃圾收集的详细信息,请查看了解 Flash Player 9 中的垃圾收集 (上面说的是 Flash Player 9,但这也适用于 10)。

What is happening, when you declare your NetConection and NetStream inside the function, is that the scope for that variable is local to that function. That means that nothing else is referencing the NetConnection 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 the loadSong(); 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).

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