views:

538

answers:

3

Hello guys, I just wanna know if it is possible to pause a playing SWF file in adobe flex? I have an SWF Loader and it plays my SWF file however, it does not have any capability (or built in function) that pauses the file.

Can anyone help me with this please? I'll appreciate some code to start with. :) Thanks in advance.

A: 

You can use the stop();

The following is an example of a swf playing and control is given to the play and pause and gotoandstop buttons.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            private function playHandler():void {

                var file_mc:MovieClip = fileswf.content as MovieClip;
                file_mc.play();

            }

            private function pauseHandler():void {

                var file_mc:MovieClip = fileswf.content as MovieClip;
                file_mc.stop();
            }

            private function pauseat(frame:Number):void {

                var file_mc:MovieClip = fileswf.content as MovieClip;
                file_mc.gotoAndStop(frame);
            }

        ]]>
    </fx:Script>


    <mx:SWFLoader x="0" y="0" source="abc.swf" id="fileswf"/>
    <s:Button x="0" y="200" label="Play" id="playbtn" click="playHandler()"/>
    <s:Button x="100" y="200" label="Pause" id="pausebtn" click="pauseHandler()"/>
    <s:Button x="100" y="250" label="Pause at A" id="pauseAbtn" click="pauseat(1)"/>
    <s:Button x="200" y="250" label="Pause at B" id="pauseBbtn" click="pauseat(2)"/>
    <s:Button x="300" y="250" label="Pause at C" id="pauseCbtn" click="pauseat(3)"/>
    <s:Button x="400" y="250" label="Pause at D" id="pauseDbtn" click="pauseat(4)"/>
    <s:Button x="500" y="250" label="Pause at E" id="pauseEbtn" click="pauseat(5)"/>

</s:Application>

For completeness I have placed in the method for @Embed (it is not possible to get the original swf directly via SWFLoader [runtime vs compile time] but you can you can load the Bytes from a Class)

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="955" minHeight="600" addedToStage="init()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[

            [Embed(source="abc.swf", mimeType="application/octet-stream") ]
            public var abc_cls:Class;

            public var ldr:Loader = new Loader();

            private var file_mc:MovieClip;

            protected function init():void
            {
                ldr.loadBytes( new abc_cls() as ByteArray );
                ldr.contentLoaderInfo.addEventListener(Event.INIT, onSwfLoaded);
                swfcontainer.addChild(ldr);  
            }

            private function onSwfLoaded(e:Event):void {
                file_mc = ldr.content as MovieClip;
            }

            private function playHandler():void {

                file_mc.play();

            }

            private function pauseHandler():void {

                file_mc.stop();
            }

            private function pauseat(frame:Number):void {


                file_mc.gotoAndStop(frame);
            }       

        ]]>
    </fx:Script>


    <mx:Image id="swfcontainer" />
    <s:Button x="0" y="200" label="Play" id="playbtn" click="playHandler()"/>
    <s:Button x="100" y="200" label="Pause" id="pausebtn" click="pauseHandler()"/>
    <s:Button x="100" y="250" label="Pause at A" id="pauseAbtn" click="pauseat(1)"/>
    <s:Button x="200" y="250" label="Pause at B" id="pauseBbtn" click="pauseat(2)"/>
    <s:Button x="300" y="250" label="Pause at C" id="pauseCbtn" click="pauseat(3)"/>
    <s:Button x="400" y="250" label="Pause at D" id="pauseDbtn" click="pauseat(4)"/>
    <s:Button x="500" y="250" label="Pause at E" id="pauseEbtn" click="pauseat(5)"/>

</s:Application>

I am sure now that it is the embed that was giving the problem sorry for any confusions. So if you need your swf as part of your file below should the trick.

The last way you can achieve getting assets into your flex workspace is to use SWC Assets Method.

Good Luck ! :D

phwd
yes i can use the stop() function but it stops the whole playing SWF file. What i really need is just to pause it then after clicking on the play button, it will just resume to where it was playing right before it was paused. Thanks for the code. :)
Kim
If it is timeline based you may need the gotoAndStop(last frame); Is it timeline based ?
phwd
sorry but i think this will sound stupid. i do not really know about that since i'm really new to using flex. i am trying the code you pasted above and on page load, it automatically plays the swf file so i don't really need to rollover/rollout. plus, i had to change the <mx:Script> into <fx:Script> for a particular reason which i really don't know. I hope you can help me out. Thanks.
Kim
The Change from `<mx:Script>` to `<fx:Script>` means you are using Flex 4 SDK no worries. A timeline based swf uses the timeline provided in Flash CS3 software. Example if I just did a simple animation of a shape moving in the timeline, the above the code will say on rollover of the swf play the animation and as soon as it rolls out, pause it. If this is not working for you, you may either have to describe what your swf is doing (is it a simple animation ? is it a photo album ? etc) or use the gotoAndStop()
phwd
it is a video-like swf file. it plays exactly like the way a video does. anyway, this swf is just a test video i am using but the real swf file i would be using on my project is a flash-created rusting effect (corrosion) its simply like spraypaint being sprayed in small densities at a time.what i observed was that on pageload, it automatically plays so i have to put an autoLoad="false" so that it wouldn't play on page load. this, however, will make the swf file not play at all. though i rollover my mouse, it still won't play.Thanks a lot for helping me out. I appreciate your kindness.
Kim
haha. now this new code of yours confused me. But this is just the perfect timing since i was about to ask about some error I get whenever I publish my application. i'll try this one out. thanks :)
Kim
A: 

by the way, if you have concrete examples on how to use gotoAndStop can you please give me some tips? I am using the code below but it doesn't give me anything.

protected function btnGoToAndStop_clickHandler(event:MouseEvent):void
{
    var file_mc:MovieClip = fileswf.content as MovieClip;
    file_mc.gotoAndStop(txtFrame.text);
}
<mx:SWFLoader x="0" y="0" source="@Embed(source='assets/sample.swf')" id="fileswf" />
<s:Button x="10" y="191" label="gotoAndPlay" id="btnGoToAndStop"    
click="btnGoToAndStop_clickHandler(event)"/>
<s:TextInput x="107" y="191" width="42" id="txtFrame"/>

though I use file_mc.stop(), it still won't stop. By the way the swf file i am using now is an swf file which spells out my name by frame (it actually has 10 frames which is the character length of my full name).

Thanks :)

Kim
Sorry for the delay, [gotoAndStop](http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/display/MovieClip.html "AS 3.0 gotoAndStop") works using the frames e.x. `file_mc.gotoAndStop(5)` says go to frame 5 in the timeline and stop. So lets see if we can get on the same page. Create a simple Fla (say abc.fla) with 5 keyframes each with a letter using the Text Tool (T) (I chose abcde). If you run that movie, you will see changing of the letters from a to e. Move the movie (abc.swf) to your source (src) folder in Flex/Flash Builder and try the code I gave in my answer.
phwd
Sorry i just read your answer just now since i was away in a vacation for a couple'o days. I tried what you said and everytime I run my project with the "abc.swf", i get a page with moving ABCDE letters and even though I tried to click my button with a code file_mc.gotoAndStop(5) it still won't work. :(
Kim
I have updated the code [below](http://stackoverflow.com/questions/3003217/is-it-possible-to-pause-a-playing-swf-file-in-adobe-flex-how/3003807#3003807) to show a full example in Flex4 with play pause gotoAndStop if the code below does not work for you then you may need to clean your project.
phwd
A: 

Thank you very much for the code you gave me. It is working perfectly the way I wanted it to work. Now this gave me an idea on how to do my project. Thanks a lot for the help. :)

Kim
No problems I think the reason you were not getting it to play/stop is because you were using the `@Embed` which is perfectly fine if you need the swf to load immediately I have updated the code once again just in case you decide you really need to embed your swf. Good luck :D
phwd
yeah. i figured that one out when I tried removing my @Embed and it was playing and stopping accordingly. thanks thanks thanks. :D
Kim