flex加载jwplayer的解决办法

今天偶尔看到了这篇文章,转载一下,以备所需。之前尝试过在flex工程中加入jwplayer,以失败告终。以下转载的方法还没有亲身试验。

原文地址如下:

http://cookbooks.adobe.com/post_Embedding_JW_Player_Version_5_into_Flex-16707.html

Problem

By default JW Player Version does not support embedding into Flex application (http://developer.longtailvideo.com/trac/wiki/FlexEmbedding).

Solution

You just need to overlap original RootReference class with your own, when loading player in to your Flex application. You don't need to get JW Player source code from SVN and fix it to apply this solution, but this is one of the approach.

Detailed explanation

This is a workaroud to made Longtail FLV Player 5

(http://www.longtailvideo.com/players/jw-flv-player/), work within Adobe Flex.
 
This class is overlaps original class:
 com.longtailvideo.jwplayer.utils.RootReference compiled in Player.
 
WARNING:
 This class MUST be placed strictly in: com.longtailvideo.jwplayer.utils class path.
 
Usage example:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:com="com.*"
    layout="vertical" verticalAlign="middle" horizontalAlign="center"
    creationComplete="handleCreationComplete()">
    
  <mx:Script>
    <![CDATA[
      import com.longtailvideo.jwplayer.utils.RootReference;
      import mx.core.UIComponent;
      import mx.events.FlexEvent;


      private var _loader:Loader;
      private var _playerObject:DisplayObject;
      private var _flashVars:String;
      private var _autoStart:Boolean = true;
      private var _playerURL:String = "player.swf";


      private var _videoURL:String = "someVideo.flv";
      private var _uic:UIComponent;
      private var _jwPlayerHack:RootReference;


      protected function handleCreationComplete():void {
        // You can add Player container not only in some UIComponent but also on stage.
        _uic = UIComponent(canv.addChild (new UIComponent ()));


        _uic.width = 400;
        _uic.height = 300;


        _jwPlayerHack = new RootReference(_uic);
        
        videoSource = _videoURL;
      }


      public function set videoSource(url:String):void {
        if (!url) url = "";
        // Here you can set any FlashVars supported by player.
        // see http://developer.longtailvideo.com/trac/wiki/Player5FlashVars.
        _flashVars = "file=" + url + "&autostart=true";
        _flashVars += "&t=" + getTimer().toString();


        _loader = new Loader();
        _loader.contentLoaderInfo.addEventListener(Event.INIT, onLoadInit);


        var ldrContext:LoaderContext = new LoaderContext(false,
            ApplicationDomain.currentDomain);
        var request:URLRequest = new URLRequest(_playerURL);
        var urlVars:URLVariables = new URLVariables();
       
        urlVars.decode(_flashVars);
        request.data = urlVars;
       
        _loader.load(request, ldrContext);
      }
     
      private function onLoadInit(event:Event):void {   
        _playerObject = _loader.content as DisplayObject;
        _playerObject.addEventListener("jwplayerReady", onPlayerReady);
       
        RootReference.root = _playerObject.root;
        _uic.addChild(_loader);
      }


      private function onPlayerReady(event:*=null):void {
        _jwPlayerHack.fixMaskIssue();
      }
    ]]>
  </mx:Script>   
  
  <mx:Canvas id="canv" width="400" height="300" backgroundColor="0xffffff" />
</mx:Application>
 
  

RootReference.as:(也就是原文src.zip中的内容)


package com.longtailvideo.jwplayer.utils

{

import flash.debugger.enterDebugger;

import flash.display.DisplayObject;

import flash.display.DisplayObjectContainer;

import flash.display.MovieClip;

import flash.display.Stage;

import flash.display.StageDisplayState;

import flash.events.Event;

import flash.events.EventDispatcher;

import flash.events.FullScreenEvent;

import flash.geom.Point;

import flash.geom.Rectangle;

import flash.media.Video;

import flash.system.Capabilities;

import flash.system.Security;

import mx.core.Application;

import mx.core.UIComponent;

/**

* This is a workaroud to made Longtail FLV Player 5 (http://www.longtailvideo.com/players/jw-flv-player/), work within Adobe Flex.

* This class is overlaps original class com.longtailvideo.jwplayer.utils.RootReference compiled in Player.

* WARNING: This class MUST be placed strictly in com.longtailvideo.jwplayer.utils class path.

*

* Usage example:

* <?xml version="1.0" encoding="utf-8"?>

* <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:com="com.*"

* layout="vertical"

* verticalAlign="middle" horizontalAlign="center" 

* creationComplete="handleCreationComplete()">

*

* <mx:Script>

* <![CDATA[

* import com.longtailvideo.jwplayer.utils.RootReference;

*

* import mx.core.UIComponent;

* import mx.events.FlexEvent;

*

* private var _loader: Loader;

* private var _playerObject: DisplayObject;

* private var _flashVars: String;

* private var _autoStart: Boolean = true;

* private var _playerURL: String = "player.swf";

*

* private var _videoURL: String = "someVideo.flv";

*

* private var _uic: UIComponent;

* private var _jwPlayerHack: RootReference;

*

* protected function handleCreationComplete () : void

* {

* // You can add Player container not only in some UIComponent but also on stage.

* _uic = UIComponent( canv.addChild ( new UIComponent () ) );

*

* _uic.width = 400;

* _uic.height = 300;

*

* _jwPlayerHack = new RootReference( _uic );

*

* videoSource = _videoURL;

* }

*

* public function set videoSource( url : String ) : void

* {

* if ( ! url ) url = "";

*

* // Here you can set any FlashVars supported by player

* // see http://developer.longtailvideo.com/trac/wiki/Player5FlashVars

* _flashVars = "file=" + url + "&autostart=true";

* _flashVars += "&t=" + getTimer().toString();

*

* _loader = new Loader();

*

* _loader.contentLoaderInfo.addEventListener( Event.INIT, onLoadInit );

*

* var ldrContext: LoaderContext = new LoaderContext( false, ApplicationDomain.currentDomain );

* var request: URLRequest = new URLRequest( _playerURL );

* var urlVars: URLVariables = new URLVariables();

*

* urlVars.decode( _flashVars );

*

* request.data = urlVars;

*

* _loader.load( request, ldrContext );

* }

*

* private function onLoadInit ( event:Event ) : void

* {

* _playerObject = _loader.content as DisplayObject;

* _playerObject.addEventListener( "jwplayerReady", onPlayerReady );

*

* RootReference.root = _playerObject.root;

*

* _uic.addChild( _loader );

* }

*

* private function onPlayerReady ( event:* = null ) : void

* {

* _jwPlayerHack.fixMaskIssue();

* }

*

* ]]>

* </mx:Script>

*

* <mx:Canvas id="canv" width="400" height="300" backgroundColor="0xffffff" />

     *

* </mx:Application>

*

* @author Dmitry 'Reijii' Kochetov, Marat '7thsky' Atayev

* @version Jan 14, 2010

* @skype kodjii

*

* This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported

* @see http://creativecommons.org/licenses/by-sa/3.0/

*/

public class RootReferenceextends EventDispatcher

{

public static var root : DisplayObject;

public static var stage : RootReference;

public static var container : UIComponent;

public static var instance : RootReference;

public static var _stageInstance : Stage;

public static var _playerUI : MovieClip;

public function RootReference ( $displayObj : DisplayObject ) : void

{

if ( ! RootReference.instance )

{

RootReference.instance= this;

RootReference.container = $displayObjas UIComponent;

RootReference.stage= RootReference.instance;

try

{

Security.allowDomain("*" );

}

catch ( e : Error )

{

// This may not work in the AIR testing suite

}

if ( container.stage )

{

_stageInstance = container.stage;

_stageInstance.addEventListener( Event.ADDED, handleAdded );

}

else

{

container.addEventListener( Event.ADDED_TO_STAGE, handleAddedToStage,false, 0, true );

}

}

}

private function handleAddedToStage ( event:Event ) : void

{

container.removeEventListener( Event.ADDED_TO_STAGE, handleAddedToStage );

_stageInstance = container.stage;

_stageInstance.addEventListener( Event.ADDED, handleAdded );

}

private function handleAdded ( event:Event ) : void

{

if ( event.targetis Video )

{

enterDebugger();

}

}

public function fixMaskIssue () : void

{

var i: int;

var parent: MovieClip; 

var child: DisplayObject;

var mask: DisplayObject;

var tl: Point; 

var br: Point;

if ( ( parent = _playerUI ) !=null )

{

for ( i = 0; i < parent.numChildren; i++ )

{

if ( parent.getChildAt( i ).mask)

{

mask = parent.getChildAt( i ).mask;

break;

}

}

}

if ( mask )

{

tl = container.localToGlobal(new Point( 0, 0 ) );

br = container.localToGlobal(new Point( container.width, container.height ) );

mask.x = tl.x;

mask.y = tl.y;

mask.width = br.x - tl.x;

mask.height = br.y - tl.y;

}

}

// STAGE EMULATION ;)

public var stage : Object = {};

public function get stageWidth () : int

{

return container.width;

}

public function get stageHeight () : int

{

return container.height;

}

override public function addEventListener ( type:String, listener:Function, useCapture:Boolean =false, priority:int = 0, useWeakReference:Boolean = false ) : void

{

_stageInstance.addEventListener( type, listener, useCapture, priority, useWeakReference );

}

override public function removeEventListener ( type:String, listener:Function, useCapture:Boolean =false ) : void

{

_stageInstance.removeEventListener( type, listener, useCapture );

}

override public function hasEventListener ( type:String ) : Boolean

{

return _stageInstance.hasEventListener( type );

}

override public function willTrigger ( type:String ) : Boolean

{

return _stageInstance.willTrigger( type );

}

override public function dispatchEvent ( event:Event ) : Boolean

{

return _stageInstance.dispatchEvent( event );

}

public function addChildAt ( $child:DisplayObject, $index:int ) : DisplayObject

{

if ( $childis MovieClip )

{

_playerUI = MovieClip( $child );

}

return container.addChildAt( $child, $index );

}

public function addChild ( $child:DisplayObject ) : DisplayObject

{

return addChildAt( $child, container.numChildren ); 

}

public function removeChild ( $child:DisplayObject ) : DisplayObject

{

return container.removeChild( $child );

}

public function get numChildren () : uint

{

return container.numChildren;

}

public function get displayState () : String

{

return _stageInstance ? _stageInstance.displayState :'';

}

private var _savedParent : DisplayObjectContainer;

private var _savedLayout : String;

private var _savedWidth : Number;

private var _savedHeight : Number;

private var _savedX : Number;

private var _savedY : Number;

private var _savedIndex : Number;

private var _isLocked : Boolean = false;

public function set displayState ( $value:String ) : void

{

if ( $value == StageDisplayState.FULL_SCREEN )

{

_isLocked = false;

setFullScreen();

}

elseif ( _stageInstance.displayState == StageDisplayState.FULL_SCREEN )

{

_isLocked = true;

setNormalScreen();

}

}

private function handleFullScreen ( event:FullScreenEvent ) : void

{

if ( event.fullScreen ==false && _isLocked == false  )

{

setNormalScreen();

}

}

private function setFullScreen () : void

{

_savedWidth = container.width;

_savedHeight = container.height;

_savedLayout    = Application.application.layout;

_savedParent = container.parent;

Application.application.layout ="absolute";

_savedIndex = _savedParent.getChildIndex( container );

Application.application.addChild( _savedParent.removeChild( container ) );

_savedX = container.x;

_savedY = container.y;

container.x = 0;

container.y = 0;

_stageInstance.addEventListener( FullScreenEvent.FULL_SCREEN, handleFullScreen );

_stageInstance.fullScreenSourceRect =new Rectangle( 0, 0, Capabilities.screenResolutionX, Capabilities.screenResolutionY );

_stageInstance.displayState = StageDisplayState.FULL_SCREEN;

container.width = Capabilities.screenResolutionX;

container.height = Capabilities.screenResolutionY;

Object( RootReference.root ).redraw();

fixMaskIssue();

}

private function setNormalScreen () : void

{

_stageInstance.displayState = StageDisplayState.NORMAL;

Application.application.layout = _savedLayout;

_savedParent.addChildAt( Application.application.removeChild( container ),_savedIndex );

container.width = _savedWidth;

container.height = _savedHeight;

container.x = _savedX;

container.y = _savedY;

Object( RootReference.root ).redraw();

fixMaskIssue();

_stageInstance.removeEventListener( FullScreenEvent.FULL_SCREEN, handleFullScreen );

}

public function get scaleMode () : String

{

return _stageInstance ? _stageInstance.scaleMode :'';

}

public function set scaleMode ( $value:String ) : void

{

_stageInstance.scaleMode = $value;

}

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值