WARNING * THE FLV CONTAINED WITHIN THIS DEMO TUTORIAL CONTAINS MILDLY OFFENSIVE LANGUAGE FROM A RECORD BEING PLAYED WHILST BEING SCRATCHED IN A RHYTHMIC MANNER ☺
** NOTE ** This app uses the pre V2 PMVC package ** update coming soon!
Requirements:
Software: Flex Builder 3 + Flash CS3
Plugins: PureMVC.swc
Overview:
This is a really basic application constructed using Flex Builder for all ActionScript code and Flash CS3 for the visual components. The purpose of the tutorial is to show how to construct a simple Flash CS3 application using the PureMVC framework. The tutorial should give you working knowledge of how to interact with components on the stage in Flash from within the PureMVC framework. We will use the FLVPlayback component as an example and create an event listener in a mediator to listen out for a click on the FLVPlayback component. This tutorial will not be going into great depth about the framework but simply give you a step by step guide of how to get the framework up and running in a real life application.
1) Start Flex Builder 3 and create a new ActionScript project named CS3VideoPMVC, keep the default settings on the first new project page and click next. We need to have access to the FLVPlayback component classes so click to add another swc and navigate to the FLVPlaybackAS3.swc located within Configuration > Components > Video in the default Flash CS3 installation directory. Click browse on the main source folder option, select the CS3VideoPMVC folder and click new, this will create a standard src folder for all the source files we will create. Click finish. Right click the project folder and select ActionScript compiler, deselect HTML wrapper, we can leave the generation of the HTML page to Flash. Finally create a new folder in the src folder named assets and place an flv for the application to play.
2) Start Flash CS3 and follow the instructions provided by Cliff for importing the framework into Flash (or you can copy the whole PureMVC org directory into the src folder in Flex). Next create a new flash file (ActionScript 3), save it as CS3VideoPMVC in the src folder in the CS3VideoPMVC directory created in Flex. Set the document properties to: width = 320, height = 240, frame rate = 31. Create a new movie clip and name it VideoMC, ensure the linkage properties are displayed by clicking advanced and check export for ActionScript, assigning CS3VideoPMVC as the class, click OK. Drag and instance of this movie clip onto the stage and give it an instance name of player. Double click on the clip and drag an FLVPlayback component onto the stage and give it an instance name of flv_player. In the component inspector set the skin to SkinOverPlaySeekMute.swf. In publish settings select the Flash tab and click ActionScript version settings, deselect automatically declare stage instances and click ok. We are now finished editing in the Flash IDE, however, keep Flash open for publishing and publish preview of the application.
3) In Flex create a new AS3 class named CS3VideoPMVC. This is the class, which will tie the components on the stage in the fla to the PureMVC framework. Create 2 public variables, one named player data typed to MovieClip and the second named flv_player data typed to FLVPlayback. These are the instances of the components on the stage in flash, we now want to get a reference to the PureMVC framework inside this class.
4) Within in the src folder create a new folder called com, then within that folder create a new folder named nutrixinteractive. Create 3 new folders within the puremvc folder named model, view and controller.
5) In the nutrixinteractive folder create a new ActionScript class named ApplicationFacade, click browse next to superclass and type Façade, this will import the PureMVC Façade class. Click add next to interfaces and type IFacade to implement the IFacade class. Deselect generate constructor from super class and generate functions inherited from interfaces then click finish.
6) Type the following to complete your ApplicationFacade class:
-
package com.nutrixinteractive
-
{
-
import com.nutrixinteractive.controller.StartupCommand;
-
-
import org.puremvc.interfaces.IFacade;
-
import org.puremvc.patterns.facade.Facade;
-
import org.puremvc.patterns.observer.Notification;
-
-
public class ApplicationFacade extends Facade implements IFacade
-
{
-
// Notification name constants
-
public static const STARTUP:String = "startup";
-
-
/**
-
* Singleton ApplicationFacade factory method
-
*/
-
public static function getInstance():ApplicationFacade
-
{
-
if ( instance == null ) instance = new ApplicationFacade();
-
return instance as ApplicationFacade;
-
}
-
-
public function startup( app:CS3VideoPMVC ):void
-
{
-
notifyObservers( new Notification( STARTUP, app ) );
-
}
-
-
/**
-
* Register Commands with the Controller
-
*/
-
override protected function initializeController():void
-
{
-
super.initializeController();
-
-
registerCommand( STARTUP, StartupCommand );
-
}
-
}
-
}
What is happening here is a check to see if there is already and instance of the ApplicationFacade, if not create one, this is called a creating a Singleton Class. Next is our startup method that expects to me passed the instance of our main application. A notification named STARTUP is then sent to anyone listening (or Observing) and the main application instance is passed as a parameter. The ApplicationFacade is listening and has STARTUP as a named constant and has also registered it to a command. The command to be run is StartupCommand.
7) Within the controller folder create a new ActionScript class named StartupCommand with a super class of SimpleCommand and the interface ICommand. Deselect generate constructor from super class and generate functions inherited from interfaces then click finish.
Ensure your StartupCommand looks like below:
-
package com.nutrixinteractive.controller
-
{
-
import com.nutrixinteractive.view.PlayerMediator;
-
-
import org.puremvc.interfaces.ICommand;
-
import org.puremvc.interfaces.INotification;
-
import org.puremvc.patterns.command.SimpleCommand;
-
-
public class StartupCommand extends SimpleCommand implements ICommand
-
{
-
/**
-
* Register the Proxies and Mediators
-
*
-
* Get the View components for the Mediators from the app,
-
* which passed a reference to itself on the notification.
-
*/
-
override public function execute( note:INotification ):void
-
{
-
var app:CS3VideoPMVC = note.getBody() as CS3VideoPMVC;
-
-
// Register Mediators
-
facade.registerMediator( new PlayerMediator( app ) );
-
}
-
}
-
}
The ApplicationFacade (our hub) calls this command. When notifyObservers() was run from the startup method it passed an instance of the main application that we can retrieve using getBody() method on this notification. It is from this command we can now initiate (register) a mediator to handle interaction with the main application.
9) Within the view folder create a new ActionScript class named PlayerMediator with a super class of Mediator and the interface I Mediator. Deselect generate constructor from super class and generate functions inherited from interfaces then click finish.
10) Ensure your PlayerMediator looks as below:
-
package com.nutrixinteractive.view
-
{
-
import fl.video.*;
-
-
import flash.display.Sprite;
-
import flash.events.MouseEvent;
-
-
import org.puremvc.interfaces.IMediator;
-
import org.puremvc.patterns.mediator.Mediator;
-
-
public class PlayerMediator extends Mediator implements IMediator
-
{
-
// Mediator name
-
public static const NAME:String = 'PlayerMediator';
-
-
// Visuals on stage
-
private var flv_player:FLVPlayback;
-
-
/**
-
* Constructor
-
*/
-
public function PlayerMediator( viewComponent:Object )
-
{
-
// Pass the viewComponent to the superclass
-
super( viewComponent );
-
-
assignPlayer();
-
}
-
-
override public function getMediatorName():String
-
{
-
return PlayerMediator.NAME;
-
}
-
-
protected function get player():CS3VideoPMVC
-
{
-
return viewComponent as CS3VideoPMVC;
-
}
-
-
private function assignPlayer():void
-
{
-
flv_player = player.flv_player;
-
-
flv_player.addEventListener( MouseEvent.CLICK, onClick );
-
-
playVideo();
-
}
-
-
private function playVideo():void
-
{
-
flv_player.play("assets/krome.flv");
-
}
-
-
private function onClick( e:MouseEvent ):void
-
{
-
trace(" CLICK ");
-
}
-
}
-
}
The mediator is passed an instance of the main application and using a getter method enables the main application to be referenced as ‘player’. We create a local variable to the FLVPlayback component and now have direct access to add an event listener.
11) Finally to tie the application together ensure your CS3VideoPMVC.as class looks as below:
-
package
-
{
-
import com.nutrixinteractive.ApplicationFacade;
-
-
import fl.video.FLVPlayback;
-
import flash.display.MovieClip;
-
-
public class CS3VideoPMVC extends MovieClip
-
{
-
public var player:MovieClip;
-
public var flv_player:FLVPlayback;
-
-
public function CS3VideoPMVC()
-
{
-
ApplicationFacade.getInstance().startup(this);
-
}
-
}
-
}
Here we create the 2 local variables, which are the component instances on the stage. We then instantiate the ApplicationFacade directly calling the startup method passing a single parameter, the instance of the documentClass which is home to our player MovieClip and flv_player FLVPlayback component.
12) Run the application in Flash. And click the controls on the component. A trace should display in the Flash output window.
Where next?
Now you can access components on the stage from within the PureMVC framework why not add your own controls. You can also set up a Proxy to get an external play list and pass that to the mediator for the FLVPlayback component to play. Now the basic structure is in place adding to the framework is a synch.
Source Files: source
*** NOTE UPDATE 1 ***
- Package restructured.
- Mac OS X structures deleted
- Swf, javascript and html files removed
*** NOTE UPDATE 2 ***
- Added the puremvc org directory to the src folder in source files.
- Amended tutorial accordingly.
*** copyright newtriks 2008 ***

30 Comments
Thanks for the tutorial, but after runnig the application in Flash I got 3 errors:
1017: The definition of base class Facade was not found.
1045: Interface IFacade was not found.
1020: Method marked override must override another method.
Thanks.
Hi andrey, have you imported imported the puremvc framework into your project? I have found some one had a similar problem and I may not have had the issue due to me importing it following these instructions puremvc flash install, an easy way to get puremvc is to copy the org directory from puremvc and paste it into the src folder in Flex, I will amend the tutorial accordingly
Thank you! It works!
Thanx for a nice intro into PMVC. I like the twich of doing a Flash-app with code repository in Flex. I usually do it the other way around. I see a lot of similarities between PMVC and Cairngorm, however being a Cairngormer PMVC still is new to me, but this looks really promising.
Thanx again NewTriks!
I am getting this error that I have gotten before on other examples
"Cannot access a property or method of a null object reference at com.infusion.view::PlayerMediator/assignPlayer()"
It seems that the viewComponent isn't getting registered before it is being accessed. Like the PlayerMediator is not really sending the viewComponent to it's parent?
played around with it for a while, but I couldn't get it to work.
It seems I only get this error when creating a Flash movie, not Flex.
I guess I will check out the PureMVC forums
Russ
yeah it's always the little things that shut you down, like spelling....
veiwComponent != viewComponent
I went back and checked the other example I a was following, and I had mad the same mistake...
Alexie has translated my tutorial into Russian for all adept to the lingo
Thanks Alexie http://injun.ru/?p=450
nice
hey
Really nice tips!
need to try to do it myself, implemeting your ideas
thanks!
Hi,
looking good, nice article
thanks for sharing this!
Responding to Russ above. i had the same problem. A colleague looked it over and pointed out that a change in PureMVC_AS3_2_0_1 requires an update to PlayerMediator() in PlayerMediator.as:
public function PlayerMediator( viewComponent:CS3VideoPMVC )
{
//pass the viewComponent to the superclass
super (NAME, viewComponent ); //modified this
assignPlayer();
}
Summary: super() now requires two parameters--the first parameter is the name.
Here is the link to the migration from 1.7.1 to 2.0 for reference http://trac.puremvc.org/PureMVC_AS3/wiki/ReleaseNotes
Simon
Thanks!
Thanks!
override public function execute( note:INotification ):void
{
var app:CS3VideoPMVC = note.getBody() as CS3VideoPMVC;
// Register Mediators
facade.registerMediator( new PlayerMediator( app ) );
}
>> How did the method find the class CS3VideoPMVC even w/o importing it?
var app:CS3VideoPMVC = note.getBody() as CS3VideoPMVC;
Hi Joel,
This is because the CS3VideoPMVC lives in the default package, all classes based there are not required for import, HTH
Thank you.
This is because the CS3VideoPMVC lives in the default package, all classes based there are not required for import, HTH
>> I see. I thought package ownership is strict (i.e. classes on com.pack1.subpack doesn't "live" relative to com.pack1 ..
Thank you Sir
Hey all,
PureMVC is definitely lacking tutorials, so I’ve done my bit to help out by writing one
Check it out at http://www.actionscriptdeveloper.co.uk/puremvc-tutorial-flex-puremvc-jabber-and-xiff-3-introduction/
Enjoy,
Dave
http://www.actionscriptdeveloper.co.uk
Hi Simon, thanks a ton for taking the time to share your expertise of PureMVC and proper OOP! I have a noob question for ya.. when you call super(viewComponent); from the PlayerMediator constructor, how does Mediator differentiate between parameters? I see Mediator takes two, mediatorName:String and viewComponent:Object - I can see that their dataTypes match what is going on but there is no call to that first string parameter. I'm sure this is a compiler thing that is makes perfect sense to you guys, but its rather abstract to me at this point. Would someone please explain how the compiler knows to skip the first argument and move onto the second? Thank you much for your time!
Heh heh its much simpler than that mate and your not going loopo on this one, this demo uses the pre V2 PMVC release
V2 brought in the two arguments NAME and viewComponent
NAME is the static const declared as the reference for the Mediator and the viewComponent is as before in pre V2. So your super statement should look like this:
super( NAME, viewComponent );
Sorry, my bad really I should get my self round to updating this app!
AH, sweet! Thanks Simon - I hadn't actually got to the point of writing it up and trying to compile against my version so I'm glad I asked. I was thinking "wow, have I been ambivalent to some vague caveat with actionscript arguments this whole time or what?!"... I'm glad to hear I might still be "getting it"... Best regards,
Very useful and understandable article, many thanks to author!
It works well, thanks very much, I am very fond of make SWF, I will make our flv player onto web and use easily. I learn much from you!
Hi
nice content..
thanx man for sharing
Nice example, however I think you miss one key point of PureMVC, portability of view components.
I would argue that all the logic for the flv player to be contained within the view component itself.
Well thats the beauty of this framework, really its totally up to you how you use it. I personally hate having to dip back into the View repeatedly and would keep logic most definitely out of the view. Most of my players have logic handled differently, whether it be controls acting/responding differently dependent on media handling i.e. Akamai or FMS etc, or various other event response's throughout the application. Also keeping all logic in the View will actually make it less portable, if thats how you suggest handling it then I would be more inclined to built the player as a swc and use that in various applications but the limitations will soon arise in my opinion.
This is just my perspective, others can argue for and against but like I said, its good to have the flexibility to use it how you so choose!
nice example, i prefer having more control over everything that is going on, an flv player is a relatively simple task, i don't really see the value in splitting it up so much.
Cheers Tad,
This was simply an example into the framework usage in a familiar flash based application. An flv player is as simple or complicated as you want or need it to be at the end of the day. The average joe developer out there may only need to stick in a flvplayback component and be done with it. However, most of my client video apps involve way more logic and splitting this up enables more flexibility in my application, just standard OOP practice, nothing fancy (wanna see extensibility and division of logic then check under the hood of Jeroen Wijering JW player
). TBH though this ain't even split up this much, navigation separated from the video view, and a couple of proxies for video control and streaming, I don't really see this as overkill. Now the beauty is if you wanted to rip out the flvplayback and add you own custom component your in a simple seat to drive on to accomplish this.
6 Trackbacks
[...] Check out Simon Baily’s ‘How to Build a Simple FLV Player in Flash and Flex using the PureMVC Framework.’ [...]
[...] to my previous tutorial I want to detail another way into a Flash CS3 application with PureMVC. In the above tutorial I [...]
[...] FLV Player with PMVC [...]
[...] on from my last PureMVC tutorial for Flash CS3 I have written a next step tutorial on how to build and implement a Proxy to the FLV player to [...]
[...] have blogged recently about how to build a simple flv player in flash and flex using PureMVC and thought I would extend the functionality to the player by adding the ability to retrieve a [...]
[...] How to Build a Simple FLV Player in Flash and Flex using the PureMVC Framework [...]