Skip to content

Commit 137cec3

Browse files
committed
Always use StageVideo instead of Video
Reasoning based on Adobe's [Best Practices for High Performing and Efficient Flash Video](http://blogs.adobe.com/flashplayer/2015/04/best-practices-for-high-performing-and-efficient-flash-video.html).
1 parent cb9ad59 commit 137cec3

File tree

9 files changed

+90
-55
lines changed

9 files changed

+90
-55
lines changed

src/VideoJS.as

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ package{
1111
import flash.display.StageScaleMode;
1212
import flash.events.Event;
1313
import flash.events.MouseEvent;
14+
import flash.events.StageVideoAvailabilityEvent;
1415
import flash.events.TimerEvent;
1516
import flash.external.ExternalInterface;
1617
import flash.geom.Rectangle;
18+
import flash.media.StageVideoAvailability;
1719
import flash.system.Security;
1820
import flash.ui.ContextMenu;
1921
import flash.ui.ContextMenuItem;
@@ -50,9 +52,6 @@ package{
5052
registerExternalMethods();
5153
}
5254

53-
_app = new VideoJSApp();
54-
addChild(_app);
55-
5655
_app.model.stageRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
5756

5857
// add content-menu version info
@@ -64,6 +63,7 @@ package{
6463
_ctxMenu.customItems.push(_ctxVersion, _ctxAbout);
6564
this.contextMenu = _ctxMenu;
6665

66+
6767
}
6868

6969
private function registerExternalMethods():void{
@@ -168,11 +168,26 @@ package{
168168
}
169169

170170
private function onAddedToStage(e:Event):void{
171+
ExternalInterface.call('console.log', "onAddedToStage");
171172
stage.addEventListener(MouseEvent.CLICK, onStageClick);
172173
stage.addEventListener(Event.RESIZE, onStageResize);
173174
stage.scaleMode = StageScaleMode.NO_SCALE;
174175
stage.align = StageAlign.TOP_LEFT;
175-
_stageSizeTimer.start();
176+
stage.addEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, onStageVideoAvailability);
177+
}
178+
179+
private function onStageVideoAvailability(e:StageVideoAvailabilityEvent):void {
180+
if (e.availability != StageVideoAvailability.AVAILABLE) {
181+
ExternalInterface.call('console.error', 'StageVideo is not avalable.');
182+
ExternalInterface.call('window.alert', 'You must update your version of Adobe Flash to view this video.');
183+
// TODO: throw?
184+
} else {
185+
ExternalInterface.call('console.log', 'StageVideo is avalable.');
186+
_app = new VideoJSApp(stage);
187+
addChild(_app);
188+
_stageSizeTimer.start();
189+
stage.removeEventListener(StageVideoAvailabilityEvent.STAGE_VIDEO_AVAILABILITY, onStageVideoAvailability);
190+
}
176191
}
177192

178193
private function onStageSizeTimerTick(e:TimerEvent):void{

src/com/videojs/VideoJSApp.as

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package com.videojs{
22

33
import flash.display.Sprite;
4+
import flash.display.Stage;
45

56
public class VideoJSApp extends Sprite{
67

78
private var _uiView:VideoJSView;
89
private var _model:VideoJSModel;
910

10-
public function VideoJSApp(){
11+
public function VideoJSApp(stage:Stage){
1112

12-
_model = VideoJSModel.getInstance()
13+
_model = VideoJSModel.getInstance(stage);
1314

14-
_uiView = new VideoJSView();
15+
_uiView = new VideoJSView(stage);
1516
addChild(_uiView);
1617

1718
}

src/com/videojs/VideoJSModel.as

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,26 @@ package com.videojs{
1212
import com.videojs.structs.PlaybackType;
1313
import com.videojs.structs.PlayerMode;
1414

15+
import flash.display.Stage;
1516
import flash.events.Event;
1617
import flash.events.EventDispatcher;
1718
import flash.external.ExternalInterface;
1819
import flash.geom.Rectangle;
1920
import flash.media.SoundMixer;
2021
import flash.media.SoundTransform;
21-
import flash.media.Video;
22+
import flash.media.StageVideo;
2223
import flash.utils.ByteArray;
2324

2425
public class VideoJSModel extends EventDispatcher{
2526

2627
private var _masterVolume:SoundTransform;
2728
private var _currentPlaybackType:String;
28-
private var _videoReference:Video;
29+
private var _videoReference:StageVideo;
2930
private var _lastSetVolume:Number = 1;
3031
private var _provider:IProvider;
3132

33+
private var _stage:Stage;
34+
3235
// accessible properties
3336
private var _mode:String;
3437
private var _stageRect:Rectangle;
@@ -60,13 +63,21 @@ package com.videojs{
6063
}
6164
}
6265

63-
public static function getInstance():VideoJSModel {
66+
public static function getInstance(stage:Stage):VideoJSModel {
6467
if (_instance === null){
6568
_instance = new VideoJSModel(new SingletonLock());
69+
_instance.stage = stage;
6670
}
6771
return _instance;
6872
}
6973

74+
public function get stage():Stage{
75+
return _stage;
76+
}
77+
public function set stage(stage:Stage):void {
78+
_stage = stage;
79+
}
80+
7081
public function get mode():String{
7182
return _mode;
7283
}
@@ -146,10 +157,10 @@ package com.videojs{
146157
}
147158
}
148159

149-
public function get videoReference():Video{
160+
public function get videoReference():StageVideo{
150161
return _videoReference;
151162
}
152-
public function set videoReference(pVideo:Video):void {
163+
public function set videoReference(pVideo:StageVideo):void {
153164
_videoReference = pVideo;
154165
}
155166

@@ -668,7 +679,7 @@ package com.videojs{
668679
__src = {
669680
path: _src
670681
};
671-
_provider = new HTTPVideoProvider();
682+
_provider = new HTTPVideoProvider(stage);
672683
_provider.attachVideo(_videoReference);
673684
_provider.init(__src, _autoplay);
674685
}
@@ -677,7 +688,7 @@ package com.videojs{
677688
connectionURL: _rtmpConnectionURL,
678689
streamURL: _rtmpStream
679690
};
680-
_provider = new RTMPVideoProvider();
691+
_provider = new RTMPVideoProvider(stage);
681692
_provider.attachVideo(_videoReference);
682693
_provider.init(__src, _autoplay);
683694
}
@@ -686,7 +697,7 @@ package com.videojs{
686697
m3u8: _src,
687698
parameters: _parameters
688699
};
689-
_provider = new HLSProvider();
700+
_provider = new HLSProvider(stage);
690701
_provider.attachVideo(_videoReference);
691702
_provider.init(__src, _autoplay);
692703
}
@@ -696,7 +707,7 @@ package com.videojs{
696707
__src = {
697708
path:_src
698709
};
699-
_provider = new HTTPAudioProvider();
710+
_provider = new HTTPAudioProvider(stage);
700711
_provider.init(__src, _autoplay);
701712
break;
702713
default:

src/com/videojs/VideoJSView.as

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,29 @@ package com.videojs{
77
import flash.display.Bitmap;
88
import flash.display.Loader;
99
import flash.display.Sprite;
10+
import flash.display.Stage;
1011
import flash.events.Event;
1112
import flash.events.IOErrorEvent;
1213
import flash.events.SecurityErrorEvent;
14+
import flash.events.StageVideoEvent;
1315
import flash.external.ExternalInterface;
1416
import flash.geom.Rectangle;
15-
import flash.media.Video;
17+
import flash.media.StageVideo;
1618
import flash.net.URLRequest;
1719
import flash.system.LoaderContext;
1820

1921
public class VideoJSView extends Sprite{
2022

21-
private var _uiVideo:Video;
23+
private var _uiVideo:StageVideo;
2224
private var _uiPosterContainer:Sprite;
2325
private var _uiPosterImage:Loader;
2426
private var _uiBackground:Sprite;
2527

2628
private var _model:VideoJSModel;
2729

28-
public function VideoJSView(){
30+
public function VideoJSView(stage:Stage){
2931

30-
_model = VideoJSModel.getInstance();
32+
_model = VideoJSModel.getInstance(stage);
3133
_model.addEventListener(VideoJSEvent.POSTER_SET, onPosterSet);
3234
_model.addEventListener(VideoJSEvent.BACKGROUND_COLOR_SET, onBackgroundColorSet);
3335
_model.addEventListener(VideoJSEvent.STAGE_RESIZE, onStageResize);
@@ -50,16 +52,18 @@ package com.videojs{
5052

5153
addChild(_uiPosterContainer);
5254

53-
_uiVideo = new Video();
54-
_uiVideo.width = _model.stageRect.width;
55-
_uiVideo.height = _model.stageRect.height;
56-
_uiVideo.smoothing = true;
57-
addChild(_uiVideo);
55+
_uiVideo = stage.stageVideos[0];
56+
_uiVideo.addEventListener(StageVideoEvent.RENDER_STATE, onStageVideoRender);
5857

5958
_model.videoReference = _uiVideo;
6059

6160
}
6261

62+
private function onStageVideoRender(e:StageVideoEvent):void {
63+
ExternalInterface.call("console.log", "Decoder: " + e.status);
64+
sizeVideoObject();
65+
}
66+
6367
/**
6468
* Loads the poster frame, if one has been specified.
6569
*
@@ -93,6 +97,7 @@ package com.videojs{
9397
private function sizeVideoObject():void{
9498

9599
var __targetWidth:int, __targetHeight:int;
100+
var __targetY:int, __targetX:int;
96101

97102
var __availableWidth:int = _model.stageRect.width;
98103
var __availableHeight:int = _model.stageRect.height;
@@ -126,12 +131,10 @@ package com.videojs{
126131
__targetHeight = __availableHeight;
127132
}
128133

129-
_uiVideo.width = __targetWidth;
130-
_uiVideo.height = __targetHeight;
131-
132-
_uiVideo.x = Math.round((_model.stageRect.width - _uiVideo.width) / 2);
133-
_uiVideo.y = Math.round((_model.stageRect.height - _uiVideo.height) / 2);
134-
134+
__targetX = Math.round((_model.stageRect.width - __targetWidth) / 2);
135+
__targetY = Math.round((_model.stageRect.height - __targetHeight) / 2);
136+
137+
_uiVideo.viewPort = new Rectangle(__targetX, __targetY, __targetWidth, __targetHeight);
135138

136139
}
137140

src/com/videojs/providers/HLSProvider.as

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.videojs.providers{
22

3-
import flash.media.Video;
3+
import flash.display.Stage;
4+
import flash.media.StageVideo;
45
import flash.utils.ByteArray;
56
import flash.net.NetStream;
67
import flash.events.Event;
@@ -28,7 +29,7 @@ package com.videojs.providers{
2829
private var _hls:HLS;
2930
private var _src:Object;
3031
private var _model:VideoJSModel;
31-
private var _videoReference:Video;
32+
private var _videoReference:StageVideo;
3233
private var _metadata:Object;
3334
private var _mediaWidth:Number;
3435
private var _mediaHeight:Number;
@@ -49,11 +50,12 @@ package com.videojs.providers{
4950
private var _bytesTotal:Number = 0;
5051
private var _bufferedTime:Number = 0;
5152

52-
public function HLSProvider() {
53+
public function HLSProvider(stage:Stage) {
5354
Log.info("https://github.com/mangui/flashls/releases/tag/v0.4.1.1");
5455
_hls = new HLS();
55-
_model = VideoJSModel.getInstance();
56+
_model = VideoJSModel.getInstance(stage);
5657
_metadata = {};
58+
_hls.stage = stage;
5759
_hls.addEventListener(HLSEvent.PLAYBACK_COMPLETE,_completeHandler);
5860
_hls.addEventListener(HLSEvent.ERROR,_errorHandler);
5961
_hls.addEventListener(HLSEvent.MANIFEST_LOADED,_manifestHandler);
@@ -496,10 +498,9 @@ package com.videojs.providers{
496498
* For providers that employ an instance of NetStream, this method is used to connect that NetStream
497499
* with an external Video instance without exposing it.
498500
*/
499-
public function attachVideo(pVideo:Video):void {
501+
public function attachVideo(pVideo:StageVideo):void {
500502
_videoReference = pVideo;
501503
_videoReference.attachNetStream(_hls.stream);
502-
_hls.stage = pVideo.stage;
503504
_videoReference.addEventListener(Event.ENTER_FRAME, _onFrame);
504505
_model.broadcastEvent(new VideoPlaybackEvent(VideoPlaybackEvent.ON_STREAM_READY, {ns:_hls.stream as NetStream}));
505506
return;
@@ -513,7 +514,7 @@ package com.videojs.providers{
513514
stop();
514515

515516
if(_videoReference) {
516-
_videoReference.clear();
517+
//_videoReference.clear();
517518
}
518519
}
519520

src/com/videojs/providers/HTTPAudioProvider.as

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ package com.videojs.providers{
44
import com.videojs.structs.ExternalErrorEventName;
55
import com.videojs.structs.ExternalEventName;
66

7+
import flash.display.Stage;
78
import flash.events.Event;
89
import flash.events.IOErrorEvent;
910
import flash.events.ProgressEvent;
1011
import flash.events.TimerEvent;
1112
import flash.external.ExternalInterface;
1213
import flash.media.Sound;
1314
import flash.media.SoundChannel;
14-
import flash.media.Video;
15+
import flash.media.StageVideo;
1516
import flash.net.URLRequest;
1617
import flash.utils.ByteArray;
1718
import flash.utils.Timer;
@@ -48,8 +49,8 @@ package com.videojs.providers{
4849

4950
private var _model:VideoJSModel;
5051

51-
public function HTTPAudioProvider(){
52-
_model = VideoJSModel.getInstance();
52+
public function HTTPAudioProvider(stage:Stage){
53+
_model = VideoJSModel.getInstance(stage);
5354
_metadata = {};
5455
_throughputTimer = new Timer(250, 0);
5556
_throughputTimer.addEventListener(TimerEvent.TIMER, onThroughputTimerTick);
@@ -347,7 +348,7 @@ package com.videojs.providers{
347348
}
348349
}
349350

350-
public function attachVideo(pVideo:Video):void{}
351+
public function attachVideo(pVideo:StageVideo):void{}
351352

352353
public function die():void
353354
{

0 commit comments

Comments
 (0)