Last active
October 13, 2015 19:28
-
-
Save bouze/4244433 to your computer and use it in GitHub Desktop.
Ustream Flash Client APIをゴニョゴニョする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Loader; | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.net.URLRequest; | |
import flash.system.ApplicationDomain; | |
import flash.system.LoaderContext; | |
import flash.system.Security; | |
import flash.system.SecurityDomain; | |
import flash.utils.getDefinitionByName; | |
[SWF(width = "1024", height = "768", frameRate = "60", backgroundColor = "#ffffff")] | |
public class Main extends Sprite | |
{ | |
//ローカル実行用のAPIを持ったswf | |
private const LOCAL_API_PATH:String = "viewer.rsl.swf"; | |
//オンライン実行時のAPI | |
private var api:String = "http://www.ustream.tv/flash/viewer.rsl.swf"; | |
public function Main() | |
{ | |
//クラス予約 | |
UstreamPlayer; | |
//クロスドメインを許可 | |
Security.allowDomain("*") | |
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain) | |
//ローカル環境だったらローカルのswfを読み込む | |
if (Security.sandboxType == "localTrusted") | |
{ | |
api = LOCAL_API_PATH; | |
} | |
else | |
{ | |
//セキュリティドメインを設定 | |
context.securityDomain = SecurityDomain.currentDomain; | |
} | |
var loader:Loader = new Loader() | |
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void | |
{ | |
try | |
{ | |
//swcの中身を読み込まないようにStringでクラスをインスタンス化する | |
var UstreamPlayerClass:Class = getDefinitionByName("UstreamPlayer") as Class | |
var player:* = new UstreamPlayerClass(); | |
stage.addChild(player); | |
} | |
catch (e:Error) | |
{ | |
trace(e); | |
} | |
}); | |
loader.load(new URLRequest(api), context) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.geom.Rectangle; | |
import tv.ustream.tools.Debug; | |
import tv.ustream.viewer.logic.Logic; | |
public class UstreamPlayer extends Sprite | |
{ | |
static public const LOADED:String = "loaded" | |
static public const FINISH:String = "finish" | |
static public const ONLINE:String = "online" | |
static public const OFFLINE:String = "offline" | |
private const CHANNEL_ID:String = "6623665"; | |
private const RECORDED_ID:String = "27440854"; | |
private var viewer:Logic | |
private var isOnline:Boolean = false; | |
private var view:Sprite; | |
public function UstreamPlayer() | |
{ | |
if (stage) | |
{ | |
onAddedToStage() | |
} | |
else | |
{ | |
addEventListener('addedToStage', onAddedToStage) | |
} | |
} | |
private function onAddedToStage(e:Event = null):void | |
{ | |
viewer = new Logic() | |
Debug.enabled = false | |
view = new Sprite(); | |
view.addChild(viewer.display); | |
addChild(view); | |
stage.addEventListener(Event.RESIZE, resizeHandler); | |
playVideo(); | |
} | |
public function playVideo():void | |
{ | |
createChannel(); | |
resizeHandler(); | |
} | |
public function stopVideo():void | |
{ | |
viewer.destroy(); | |
} | |
private function createChannel():void | |
{ | |
viewer.destroy(); | |
viewer.createChannel(CHANNEL_ID); | |
viewer.channel.addEventListener("createStream", resizeHandler); | |
viewer.channel.addEventListener("finish", finishHandler); | |
viewer.channel.addEventListener("online", onlineHandler); | |
viewer.channel.addEventListener("offline", offlineHandler); | |
loadWait(); | |
} | |
private function onlineHandler(e:Event):void | |
{ | |
isOnline = true; | |
dispatchEvent(new Event(ONLINE)); | |
} | |
private function offlineHandler(e:Event):void | |
{ | |
isOnline = false; | |
createRecorded(); | |
dispatchEvent(new Event(OFFLINE)); | |
} | |
private function createRecorded():void | |
{ | |
viewer.createRecorded(RECORDED_ID); | |
viewer.channel.addEventListener("createStream", resizeHandler); | |
viewer.recorded.addEventListener("createStream", resizeHandler); | |
viewer.recorded.addEventListener("finish", finishHandler); | |
loadWait(); | |
} | |
private function finishHandler(e:Event):void | |
{ | |
dispatchEvent(new Event(FINISH)); | |
} | |
private function loadWait():void | |
{ | |
removeEventListener(Event.ENTER_FRAME, enterFrameHandler); | |
addEventListener(Event.ENTER_FRAME, enterFrameHandler); | |
} | |
private function stopWait():void | |
{ | |
removeEventListener(Event.ENTER_FRAME, enterFrameHandler); | |
} | |
private function enterFrameHandler(e:Event):void | |
{ | |
if (viewer.channel && viewer.channel.streamRect) | |
{ | |
loadedHandler(); | |
} | |
} | |
private function loadedHandler():void | |
{ | |
if (viewer.recorded) viewer.recorded.seek(0); | |
stopWait(); | |
dispatchEvent(new Event(LOADED)); | |
resizeHandler(); | |
} | |
private function resizeHandler(e:Event = null):void | |
{ | |
if (viewer && viewer.channel && viewer.channel.streamBounds) | |
{ | |
var targetRate:Number; | |
var targetWidth:Number; | |
var targetHeight:Number; | |
var streamBounds:Rectangle = viewer.channel.streamBounds; | |
if (streamBounds.width / streamBounds.height > stage.stageWidth / stage.stageHeight) | |
{ | |
targetRate = stage.stageHeight / streamBounds.height; | |
targetWidth = streamBounds.width * targetRate; | |
targetHeight = stage.stageHeight; | |
} | |
else | |
{ | |
targetRate = stage.stageWidth / streamBounds.width; | |
targetWidth = stage.stageWidth; | |
targetHeight = streamBounds.height * targetRate; | |
} | |
var targetX:Number = (stage.stageWidth - targetWidth) / 2; | |
var targetY:Number = (stage.stageHeight - targetHeight) / 2; | |
viewer.display.x = targetX; | |
viewer.display.y = targetY; | |
viewer.display.width = targetWidth; | |
viewer.display.height = targetHeight; | |
} | |
} | |
public function get progress():Number | |
{ | |
var value:Number = 0 | |
if (viewer) | |
{ | |
if (isOnline) | |
{ | |
value = viewer.channel? 1: 0 | |
} | |
else | |
{ | |
value = viewer.recorded? viewer.recorded.progress: 0 | |
} | |
} | |
return value | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment