问题描述
我需要在我的项目中播放SWF文件。这些SWF文件中有背景音乐。 Flash中此音乐的声音同步选项设置为流。这样做,如果你暂停Flash电影,音乐也会暂停。现在我加载这些SWF文件时遇到问题。为此,我使用了一个SWFLoader。
当我加载它时,SWF的音频已经开始播放,但是在一定时间内没有显示电影的视觉效果。连接速度越慢,显示电影的视觉效果所用的时间就越长。
音频和视觉效果是同步的,这是很好的,然而,闪光电影的第一帧是根本不显示。
我试图解决这个通过添加一个eventListener来确保电影在开始播放前已经完全加载。然而,当我这样做(下面的代码),一秒钟的音乐播放,然后停止,并重新启动时,电影是满载。
什么是解决这个问题的最好方法是什么?我在ProgressEvent.PROGRESS eventlistener的正确轨道上?
一些代码:
$ b $
code> 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 + + event.bytesTotal +字节加载); (MovieClip(swfLoader.content)&& event.bytesLoaded< event.bytesTotal)
{
MovieClip(swfLoader.content).gotoAndStop(0);
var soundTransform:SoundTransform = new SoundTransform(0);
swfLoader.soundTransform = soundTransform;
$ b 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);
可以开始播放而不必加载完整的文件。
1)你可以在加载的源文件中添加一个stop帧,以避免播放,然后在你的主要加载程序加载完成时添加一个调用gotoAndPlay的电影加载到你想要你的电影开始的框架。
2)您可以尝试将加载委托给URLLoader,然后在第一个完成时使用Loader.loadBytes。 p>
这是纯粹的AS3,但是您可以使用SWFLoader来适应FLEX Framework:
//持有要加载的东西的孩子
var holder:MovieClip = new MovieClip();
addChild(holder);
//载入器用于载入持有者中的字节
var holderLdr:Loader = new Loader();
holder.addChild(holderLdr);
函数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);
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.
Now I have a problem when I am loading these SWF files. For this I am using an SWFLoader.
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.
What is the best way to solve this problem? Am I on the right track with the ProgressEvent.PROGRESS eventlistener?
Some code:
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 file are streamed so they can start playing without having to load the full file.
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) You can try to delegate the load to an URLLoader and then use a Loader.loadBytes when the first one is complete.
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文件,而不需要加载完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!