Because of the constant attack by the wiki spam bots, accounts that are created and not used will be deleted. If you are a legitimate user, and your IP was blocked, please contact any active administrator - look at Special:RecentChanges and see who is busy fighting spam.

Swfc Code Snippets

From SWFTools
Jump to: navigation, search
swfc
fully supports Flash Player ( plug-in ) playback of mp3, wav, swf, flv, and other media file formats using most of their associated AS2 and AS3 commands. Below, you'll find some simple code snippets ( frequently given answers on the SWFTools Mailing List ) to get you started. Enjoy!


Sound

Streaming mp3 sound ( from an external url. Please read the NB postscript below sound code examples! ):

.flash bbox=800x600 version=8 name="stream_mp3.swf" compress
 .action:
   this.onMouseDown = function(){
    mp3Sound = new Sound();
    mp3Sound.loadSound("http://wiki.swftools.org/w/media/audio/ArpEchoes.mp3",True);
    // NB Sound will start automatically
    };
  .end
.end.
<--- Click on the back square to hear the compiled swf


Now using a variable to contain the url ( ideal for implementing 'play-lists' )

.flash bbox=800x600 version=8 name="stream_mp3.swf" compress
 .action:
   this.onMouseDown = function(){
     var sound_file="http://wiki.swftools.org/w/media/audio/ArpEchoes.mp3";
     mp3Sound.loadSound(sound_file ,True);
     // NB Sound will start automatically..
    };
  .end
.end.


Local audio files may be handled in exactly the same way. Obviously, in this instance you need to replace the url with the full path to the sound file.


NB Both of the above snippets use external am url. They may not work if you're attempting to stream to your local PC, which is by default, a non internet enabled location. This is an inbuilt security feature of the Flash Player plug-in. To change this default behaviour, set the Flash version to 7 or less in the initial line of your .sc file, so,

 .flash filename="stream_mp3.swf" fps=11025 version=7 bbox=300x300 compress

On execution of the resulting swf, the Flash Player plug-in should this time prompt you with a security warning dialog box. Choosing 'Settings', will redirect you to the Change Global Settings area on the Abobe web site, where you can make the appropriate settings change.

The benefits of streaming as against embedding?

-- decreased swf file size.
-- greater control ( over sound playback ).
-- easy file swapping, i.e. use of play.lists. 



Loading another SWF

   .flash filename="loadswf.swf" bbox=200x200 version=8 fps=60 compress
    .box spot width=1 height=1 color=black
    .sprite view_frame
      .put spot x=0 y=0
      .action:
       var dummy_value=0;
      .end
    .end
    .action:
       loadMovie("Your_SWF_file.swf",_root.view_frame);
       _root.onEnterFrame = function () {
       _root.view_frame.gotoAndStop(1);
                                       };
     //
     // .. because if your swf has mutiple pages with no stop commands in it,
     //    then you'll need to stop the swf playing. ;o)
     //
     .end
    .put view_frame x=10 y=20 scale=15%
 .end

If the loaded swf happens to be a converted pdf, then you have the basis for a * basic viewer Here is a simple example using swfc script

   .flash filename="viewer.swf" bbox=200x200 version=8 fps=60 compress
     .box spot width=1 height=1 color=aqua
     .sprite view_frame
        .put spot x=0 y=0
      .end
     .put view_frame x=10 y=10 scale=20%
    .end

Compile it with 'swfc',

 swfc viewer.sc

to give you the swf named 'viewer.swf'.

Now use swfcombine to add in the swf you wish to view,

   swfcombine -o combined.swf viewer.swf view_frame=your_file.swf

Load up 'combined.swf' and you should see the 'your_file.swf' framed inside the'view_frame' object of 'viewer.swf'

To get the code required to embed your swf viewer in a web page, use,

  swfdump -E combined.swf > index.html


Obviously, the above is not very useful. Additional control would be ideal, in order so step forward and backward through the individual frames of the loaded swf, and also enlarge, reduce, or otherwise manipulate them to enable better vieweing. While that exercise is mainly left for you gentle reader to work through by yourself, below is a expanded swfc/as2 example of the viewer snippet given above. This time, complete with simple navigation buttons,

   .flash filename="viewer.swf" bbox=200x200 version=8 fps=60 compress
   .outline rb:
      M 0 0 L 5 5 L 0 10
   .end
   .outline lb:
      M 10 0 L 5 5 L 10 10
   .end
   .box spot width=1 height=1 color=aqua
   .button forward
     .filled right outline=rb fill=green color=black
     .filled right_press outline=rb fill=grey color=black
     .show right as=shape x=10 y=10
     .show right as=hover x=10 y=10
     .show right_press as=pressed x=10 y=10
     .on_press:
       _root.view_frame.nextFrame();
      .end
   .end
   .button back
     .filled left outline=lb fill=red color=black
     .filled left_press outline=lb fill=grey color=black
     .show left as=shape x=10 y=10
     .show left as=hover x=10 y=10
     .show left_press as=pressed x=10 y=10
     .on_press:
       _root.view_frame.prevFrame();
     .end
   .end
   .sprite view_frame
     .put spot x=0 y=0
   .end
   .action:
      // this line stops your swf ( cycling through all it's frames )
      //
      _root.view_frame.gotoAndStop(1);
   .end
   .put view_frame x=10 y=10 scale=20%
     .put back x=0 y=0
     .put forward x=20 y=0
   .end


For further information on viewers see the creating viewers section on this wiki

Example viewer code is also available. Check out the swfs/ directory of the latest SWFTools source distribution




===Video=== ( Flash Player version 7 upward )

Streaming Video (.flv):

  .flash filename="video_as2.swf" fps=24 bbox=900x500 background=grey version=8
  .action:
    video_nc = new NetConnection();
    video_nc.connect(null);
    video_ns = new NetStream(video_nc);
    video_holder.attachVideo(video_ns);
    video_ns.play('600.flv');
    video_holder._x=50;
    video_holder._y=50;
   .end
  .video video_holder 400 400
  .put video_holder
  .end


Keyboard Key Strokes

The code spippet below uses a Key Listener object to listen for and display Key and ASCII codes:

  .flash filename="check_key.swf" bbox=841x545 version=8
  .font freesans "../fonts/FreeSans.ttf"
  .edittext keystring font=freesans width=300 height=30 color=green size=20pt text="??"
  .action:
      myListener = new Object();
      Key.addListener(myListener);
      myListener.onKeyDown = function () {
      if (Key.isDown(Key.DELETEKEY)){
         Stage["displayState"]="fullScreen";
                               };
      var kc= Key.getCode();
      var ka= Key.getAscii();
      keystring.text="Key Code:" + kc.toString() + " Ascii Code:'" + ka.toString() + "'";
                         };
      _root.enabled = true;
  .end
   .put keystring x=10 y=10
  .end

Very Simple Keyboard Viewer

Below is a link to a trivial Viewer, which in part makes use of the key routine given above,

Simple Keyboard Viewer

( with some mouse buttons for good measure )

Similar code is also integrated into the simple Media Player.


Statements above are purely personal opinion. Code snippets are examples, no guarantees! Their effectiveness may and quite possibly will vary between versions of the Flash plug-in and swfc. Brickbats, bouquets, comments, questions and requests, always welcome on the SWFTools Mailing List