Swfc Code Snippets
From Swftools
- 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! ):
.action: mp3Sound = new Sound(); mp3Sound.loadSound("http://www.firstpr.com.au/slinky/audio/wind-1-detail.mp3",True); // mp3Sound.stop(); // NB Sound will start automatically. uncomment the above line to stop it. // mp3Sound.start(); .end
..this time using a variable to contain the url ( ideal for implementing 'play-lists' )
.action: mp3Sound = new Sound(); var sound_file="http://www.firstpr.com.au/slinky/audio/bang-3-very-loud.mp3"; mp3Sound.loadSound(sound_file ,True); // mp3Sound.stop(); // NB Sound will start automatically. uncomment the above line to stop it. // mp3Sound.start(); .end
Local audio files may be handled in exactly the same way. Obviously, in this instance you don't need to specify the the full url to the sound file.
NB Both of the above snippets use external urls. They may not work if you're attemting 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="snd3.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.
[Nissemand]
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
That of course is only the start. You need controls to step backward and forward through the frames of your loaded swf,
and further controls to enlarge, reduce, or otherwise manipulate the individual pages. While that exercise is mainly left
for you gentle reader to work through 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
For further examples of viewers, check out the swfs/ directory of the latest SWFTools source distribution
[Nissemand]
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
[Ricardo Pedroso]
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,
The same code is also integrated into the simple Media Player.
[Nissemand]
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