290 likes | 411 Views
10. Multimedia Coding. Topic. Objectives of this topic:. You can …. import movies, sound and flash video using AS3 code. control the playback of those items. Overview. Loading External Images and SWFs Communicating with Loaded Movies Loading Sound Starting and Stopping Sound
E N D
10 Multimedia Coding Topic
Objectives of this topic: You can … import movies, sound and flash video using AS3 code control the playback of those items
Overview • Loading External Images and SWFs • Communicating with Loaded Movies • Loading Sound • Starting and Stopping Sound • Managing the Volume of Sound • Loading Video • Controlling Video Playback
Loading External Images and SWFs • Load a variety of images and external swf files into a Flash movie using AS3 • The files are external which are not on the library panel • URLRequest class is used to get the location of the external image file var imageRequest:URLRequest = new URLRequest("snowboard.jpg");
Followed by loading instruction to create an image loader – Loader class var imageLoader:Loader = new Loader(); imageLoader.load(imageRequest); addChild(imageLoader); • load() is method instructing the Loader to load the imageRequest file, ‘snowboard.jpg’
Type of image files that can be loaded in Flash movie – jpeg, png, and gif files • GIF files – best suited for graphics and logos, rather than photographs or more complicated images, because of the limited color palette. It uses 256 colors in the 24-bit RGB
PNG files – used mainly for less color intensive graphics. Do not support animation as GIFs, but support more transparency and produce smaller file sizes than GIFs • JPEG files – smaller file sizes for photograph image. The most popular choice for photographs that are destined for the Web.
External SWF file load a SWF into another SWF file when intend to use the movie throughout the site. • SWF files are type of reusable asset that can be used in different project or replicated throughout a single project.
Communicating with Loaded Movies • Modify objects in an external SWF file that has been loaded into another Flash movie • The modification can be done when only the external SWF file is completed loading.
The following is the code that will trigger the onComplete event when SWF file is finished loading imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
contentLoaderInfo- property of the Loader class that contains all the information about the target file (the external file that is being loaded) • To communicate with the loaded SWF file, export the class definition for that object at run time.
Write the AS3 code to activate the external SWF file. function onComplete(event:Event):void { event.target.content.boarder_mc.y -= 100; }
event.target the object being targeted by the event listener which is called contentLoaderInfo. • content property referring to the actual object in the external movie • boarder_mc instance name of the movie clip residing in the external movie
Loading Sounds • Using the same code, URLRequest class to points to the sound file. var soundReq:URLRequest = new URLRequest("free_fade.mp3"); var sound:Sound = new Sound(); sound.load(soundReq);
Three types of sound files in Flash CS3, and another six more files if QuickTime 4 is installed on users PC • Capable to import 8-bit or 16-bit sounds
sound.addEventListener(Event.COMPLETE, onComplete); function onComplete(event:Event):void { sound.play(); }
Starting and Stopping Sound • Use SoundChannel class to control the sound playback var soundControl:SoundChannel = new SoundChannel(); • Create new instance of the SoundChannel class named soundControl
addEventListener is used to add interactivity to the external movie through the button play_btn.addEventListener(MouseEvent.CLICK, playSound); stop_btn.addEventListener(MouseEvent.CLICK, stopSound);
Activate the playSound and stopSound event handler function playSound(event:MouseEvent):void { soundControl = sound.play(); } function stopSound(event:MouseEvent):void { soundControl.stop(); }
Managing the Volume of Sound • Use SoundTransform class to deal the volume of a sound var volumeControl:SoundTransform = new SoundTransform(); • Create new instance of the SoundTransform class
Volume in sound property has a possible value between 0 – 1. volumeControl.volume += .1; • ‘+= .1’ means increase the current volume by 10% every time the button is clicked
Loading Video var videoConnection:NetConnection = new NetConnection(); videoConnection.connect(null); var videoStream:NetStream = new NetStream(videoConnection); videoStream.play("short_jump.flv"); var video:Video = new Video(); video.attachNetStream(videoStream); addChild(video); • Loading video with NetConnection object to stream a video from local drive or server • Specify the address source. ‘null’ same folder
NetStream object connects to the hard drive using NetConnection path and streams the video passsed through in the parentheses var videoStream:NetStream = new NetStream(videoConnection); videoStream.play("short_jump.flv");
Need to attach the stream of the video and add it to the movie’s display list var video:Video = new Video(); video.attachNetStream(videoStream); addChild(video);
Controlling Video Playback • Control the playback of loaded FLV files and handle the error messages during playback • Video playback do not use addEventListener method to listen for or process • It is applied the listener to the object that the client property of a NetStream instance references
var metaListener:Object = new Object(); metaListener.onMetaData = onMetaData; • metaListener is an instance of the Object class which accepts all the methods and properties of any object descended from it • Second statement associates the new onMetaData() method (or event listener) with the generic object metaListener
videoStream.client = metaListener; • videoStream.client is one of the only objects that can be applied the event listener to in the external file. • By associating it with the generic object, than we can listen for onMedaData
function onMetaData(data:Object):void { play_btn.addEventListener(MouseEvent.CLICK, playMovie); stop_btn.addEventListener(MouseEvent.CLICK, stopMovie); } • Put the event listener in the function to make sure the external data (video) is fully loaded