SWFLoader 在加载未完成的情况下开始播放 SWF [英] SWFLoader starts to play SWF without the loading being complete

查看:26
本文介绍了SWFLoader 在加载未完成的情况下开始播放 SWF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的项目中播放 SWF 文件.这些 SWF 文件中有背景音乐.Flash 中此音乐的哪些声音同步选项设置为流".这样做是为了如果您暂停 Flash 电影,音乐也会暂停.

I need to play SWF files in my project. These SWF files have background music in them. The which Sound Sync Options of this music in Flash is set to "stream". This is done so that if you pause the Flash movie, the music will pause as well.

现在我在加载这些 SWF 文件时遇到了问题.为此,我使用了 SWFLoader.

Now I have a problem when I am loading these SWF files. For this I am using an SWFLoader.

当我加载它时,SWF 的音频已经开始播放,但在一段时间内没有显示电影的视觉效果.您的连接速度越慢,显示电影视觉效果所需的时间就越长.影音同步,挺好的,只是flash电影的第一帧根本不显示.

When I load it, the audio of the SWF starts playing already, but no visuals of the movie are shown for a certain time. The slower your connection is, the longer it takes before visuals of the movie is shown. The audio and visuals are in sync, that is good, however, the first frames of the flash movie are simply not displayed.

我试图通过添加一个事件监听器来解决这个问题,以确保电影在开始播放之前已完全加载.但是,当我这样做时(下面的代码),音乐播放了几分之一秒,然后停止,并在电影完全加载后重新启动.

I tried to solve this by adding an eventListener to make sure that the movie is fully loaded before it starts to play. However, when I do this (code below), for a fraction of a second the music plays, then stops, and restarts when the movie is fully loaded.

解决这个问题的最佳方法是什么?我是否在使用 ProgressEvent.PROGRESS 事件侦听器的正确轨道上?

What is the best way to solve this problem? Am I on the right track with the ProgressEvent.PROGRESS eventlistener?

一些代码:

private function loadSWF():void
{
    swfLoader.source = source;
    swfLoader.addEventListener(ProgressEvent.PROGRESS, loadProgress);
    swfLoader.addEventListener(Event.COMPLETE, startSWF);
    swfLoader.load();
    var soundTransform:SoundTransform = new SoundTransform(0);
    swfLoader.soundTransform = soundTransform;
}

private function loadProgress(event:ProgressEvent):void
{
    applicationModel.addToConsoleOutput("SWFPlayer.loadProgress(): " + event.bytesLoaded + " of " + event.bytesTotal + "bytes loaded");
    if (MovieClip(swfLoader.content) && event.bytesLoaded < event.bytesTotal)
    {
        MovieClip(swfLoader.content).gotoAndStop(0);
        var soundTransform:SoundTransform = new SoundTransform(0);
        swfLoader.soundTransform = soundTransform;
    }
}

private function startSWF(event:Event):void
{
    swfLoader.removeEventListener(ProgressEvent.PROGRESS, loadProgress);
    swfLoader.removeEventListener(Event.COMPLETE, startSWF);
    dispatchEvent(new Event("loadComplete", true));

    var soundTransform:SoundTransform = new SoundTransform(volume);
    swfLoader.soundTransform = soundTransform;

    cardMovieClip = MovieClip(swfLoader.content);
    cardMovieClip.addEventListener(Event.ENTER_FRAME, endSWFHandler);
    cardMovieClip.gotoAndPlay(0);
}

推荐答案

SWF 文件是流式传输的,因此他们无需加载完整文件即可开始播放.

SWF file are streamed so they can start playing without having to load the full file.

1) 您可以在加载的源文件中在第一帧添加 stop() 以避免播放,然后在加载完成后在主加载器中添加对 gotoAndPlay 的调用电影加载到您希望电影开始的帧.

1) You can add in your loaded source file a stop() on the first frame to avoid the playing, and then in your main loader when the load is complete add a call to gotoAndPlay on the movie loaded to the frame you want your movie start from.

2) 您可以尝试将加载委托给 URLLoader,然后在第一个加载完成后使用 Loader.loadBytes.

2) You can try to delegate the load to an URLLoader and then use a Loader.loadBytes when the first one is complete.

这是纯 AS3,但您可以适应使用 SWFLoader 的 FLEX 框架:

This is pure AS3, but you can adapt for FLEX Framework where you are using a SWFLoader:

// child who hold the stuff to be loaded
var holder:MovieClip=new MovieClip();
addChild(holder);

// the loader used to load byte within the holder
var holderLdr:Loader=new Loader();
holder.addChild(holderLdr);


function load(url:String):void {
 var ldr:URLLoader=new URLLoader();
 ldr.dataFormat=URLLoaderDataFormat.BINARY;
 ldr.addEventListener(Event.COMPLETE, onComplete);
 ldr.load(new URLRequest(url));
}

function onComplete(e:Event):void {
    var ldr:URLLoader=URLLoader(e.target);

    ldr.removeEventListener(e.type, onComplete);

    if (holderLdr.hasOwnProperty("unloadAndStop")){
        holderLdr["unloadAndStop"]();
    } else {
        holderLdr.unload();
    }
    holderLdr.loadBytes(ldr.data);
    ldr.data=null;
}

load("mySWF.swf");

这篇关于SWFLoader 在加载未完成的情况下开始播放 SWF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆