Dailymotion Player Tools

Deprecated API

The API features listed below have been deprecated. We highly recommend you to visit our new Player API page.

Flash API

Overview

Dailymotion's Flash API functions on ActionScript 3.0. By loading Dailymotion players into a Flash application and enabling the Api, users can control them via ActionScript calls to the player's public API. Controls include but are not limited to play, pause, seek, mute and set volume.

Requirements

Flash Player 9 or higher must be installed.

Starting Out

Please refer to the starting out section of javascript api documentation.

Operations

The ActionScript API is identical to the JavaScript API, although there are additional methods:

player.isPlayerLoaded():Boolean

Returns true when the player SWF is loaded, initialized, and prepared API calls. False or undefined may be returned if the player is not fully loaded and initialized.

player.addEventListener(event:String, handler:Function)

This method can accept a function to handle the event (as opposed to a string in the javascript api), but otherwise behaves identically to that of the JavaScript API. Refer to the Events section of the JavaScript API documentation for subscribable events.

player.setSize(newWidth:Number, newHeight:Number):Void

Sets the player dimensions in pixels. This method should be used instead of setting the width and height properties of the MovieClip. Note that this method does not constrain the proportions of the video player, so you will need to maintain a acceptable aspect ratio.

When the fullscreen button of the player is hit, your application runs in fullscreen mode but player size will not be changed automatically. You must respond to a FullScreenEvent and resize the player as required by calling this method.

Examples

When the player is loaded and ready, all API calls may be made in an identical fashion to JavaScript API.

The sample code below demonstrates how to load and initialize the AS3 API player:

package
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.FullScreenEvent;
    import flash.net.URLRequest;
    import flash.system.Security;

    public class testApi extends Sprite
    {
        // The player SWF file on www.dailymotion.com needs to communicate with your host
        // SWF file. Your code must call Security.allowDomain() to allow this communication.
        Security.allowDomain("www.dailymotion.com");

        // This will hold the API player instance once it is initialized.
        private var player:Object;

        private var loader:Loader = new Loader();

        // Initial size and position of the player
        private var playerX:Number = 20;
        private var playerY:Number = 10;
        private var playerWidth:Number = 480;
        private var playerHeight:Number = 270;

        public function testApi()
        {
            this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
            this.loader.contentLoaderInfo.addEventListener(Event.INIT, this.onLoaderInit);
            this.loader.load(new URLRequest("http://www.dailymotion.com/swf?enableApi=1"));
        }

        private function onAddedToStage(event:Event):void
        {
            this.stage.addEventListener(FullScreenEvent.FULL_SCREEN, this.onFullscreen);
        }

        private function onFullscreen(event:FullScreenEvent):void
        {
            if (event.fullScreen)
            {
                // Eventually resize the player to fit the whole application
                this.player.x = 0;
                this.player.y = 0;
                this.player.setSize(this.stage.stageWidth, this.stage.stageHeight);
            }
            else
            {
                // Resize the player to its initial dimensions
                this.player.x = this.playerX;
                this.player.y = this.playerY;
                this.player.setSize(this.playerWidth, this.playerHeight);
            }
        }

        private function onLoaderInit(event:Event):void
        {
            addChild(this.loader);
            this.loader.content.addEventListener("onReady", this.onPlayerReady);
            this.loader.content.addEventListener("onError", this.onPlayerError);
            this.loader.content.addEventListener("onStateChange", this.onPlayerStateChange);
        }

        private function onPlayerReady(event:Event):void
        {
            // Event.data contains the event parameter, which is the Player API ID
            trace("player ready:", Object(event).data.playerId);

            // Save a reference to this player's instance
            this.player = this.loader.content;

            // Set appropriate player dimensions for your application
            this.player.x = this.playerX;
            this.player.y = this.playerY;
            this.player.setSize(this.playerWidth, this.playerHeight);

            // Once this event has been dispatched by the player, we can use
            // cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
            // to load a particular Dailymotion video.
            this.player.loadVideoById("xcv6dv");
        }

        private function onPlayerError(event:Event):void
        {
            // Event.data contains the event parameter, which is the error code
            trace("player error:", Object(event).data);
        }

        private function onPlayerStateChange(event:Event):void
        {
            // Event.data contains the event parameter, which is the new player state
            trace("player state:", Object(event).data);
        }
    }
}