Is it possible to pause a playing SWF file in Adobe Flex? How

apache-flexflashloaderplayback

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.

Best Answer

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

Related Topic