Created
October 29, 2013 08:31
-
-
Save ohookins/7210927 to your computer and use it in GitHub Desktop.
RTMP test code. Commented sections are generally where the Flash graphical interface used to be, and was replaced with ExternalInterface commands to interact with Javascript functions in the web page.
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.NetStatusEvent; | |
import flash.external.ExternalInterface; | |
import flash.text.*; | |
import flash.net.NetConnection; | |
import flash.net.NetStream; | |
import flash.events.SecurityErrorEvent; | |
import flash.media.SoundTransform; | |
import flash.events.MouseEvent; | |
public class Audiotest extends Sprite { | |
// RTMP objects and timing stuff | |
internal var nc:NetConnection = new NetConnection; | |
internal var ns:NetStream; | |
internal var connectStart:Date; | |
internal var connectEnd:Date; | |
internal var playStart:Date; | |
internal var seekStart:Date; | |
internal var isPlaying:Boolean = false; | |
internal var isSeeking:Boolean = false; | |
internal var isPaused:Boolean = false; | |
// Informational display stuff | |
internal var statusText:TextField; | |
internal var playText:TextField; | |
internal var connectText:TextField; | |
internal var bufferText:TextField; | |
internal var seekText:TextField; | |
internal var format:TextFormat; | |
public function Audiotest() { | |
this.width = 0; | |
this.height = 0; | |
setupInterface(); | |
nc.client = this; | |
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); | |
nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); | |
connectStart = new Date(); | |
sendJSEvent("playStart"); | |
nc.connect("rtmp://localhost:1935/"); | |
} | |
private function createLine(line:String, x:Number, y:Number):void { | |
/* var textField:TextField = new TextField(); | |
textField.defaultTextFormat = format; | |
textField.text = line; | |
textField.x = x; | |
textField.y = y; | |
this.addChild(textField); | |
*/ | |
} | |
private function setupInterface():void { | |
/* | |
// Text initialisation | |
format = new TextFormat(); | |
format.font = "Verdana"; | |
format.size = 8; | |
// Headings | |
this.createLine("Status:", 30, 40); | |
this.createLine("Connect time:", 30, 50); | |
this.createLine("Play time:", 30, 60); | |
this.createLine("Buffer fill time:", 30, 70); | |
this.createLine("Seek time:", 30, 80); | |
statusText = new TextField(); | |
statusText.defaultTextFormat = format; | |
statusText.text = "Making connection..."; | |
statusText.width = 200; | |
statusText.x = 100; | |
statusText.y = 40; | |
this.addChild(statusText); | |
connectText = new TextField(); | |
connectText.defaultTextFormat = format; | |
connectText.x = 100; | |
connectText.y = 50; | |
this.addChild(connectText); | |
playText = new TextField(); | |
playText.defaultTextFormat = format; | |
playText.x = 100; | |
playText.y = 60; | |
this.addChild(playText); | |
bufferText = new TextField(); | |
bufferText.defaultTextFormat = format; | |
bufferText.x = 100; | |
bufferText.y = 70; | |
this.addChild(bufferText); | |
seekText = new TextField(); | |
seekText.defaultTextFormat = format; | |
seekText.x = 100; | |
seekText.y = 80; | |
this.addChild(seekText); | |
// Buttons | |
var button:Sprite = new Sprite(); | |
button.graphics.beginFill(0x11CC11, 1); | |
button.graphics.drawRect(30, 0, 30, 30); | |
button.graphics.endFill(); | |
this.addChild(button); | |
button.buttonMode = true; | |
button.mouseChildren = false; | |
button.addEventListener(MouseEvent.CLICK, bClick, false, 0, true); | |
*/ | |
// ExternalInterface | |
if (ExternalInterface.available) { | |
trace("ExternalInterface is available in object " + ExternalInterface.objectID); | |
logJS("ExternalInterface is available in object " + ExternalInterface.objectID); | |
ExternalInterface.addCallback("seek", seek); | |
ExternalInterface.addCallback("pause", pause); | |
} | |
} | |
private function sendJSEvent(event:String):void { | |
if (ExternalInterface.available) { | |
ExternalInterface.call("handleEvent", event); | |
} | |
} | |
private function logJS(text:String):void { | |
if (ExternalInterface.available) { | |
ExternalInterface.call("flashLog", text); | |
} | |
} | |
private function bClick(event:MouseEvent):void { | |
seek(30); | |
} | |
private function seek(relativePos:Number):void { | |
// statusText.text = "Seeking to " + (ns.time + relativePos) + " seconds"; | |
// logJS(statusText.text); | |
sendJSEvent("seekStart"); | |
seekStart = new Date(); | |
ns.seek(ns.time + relativePos); | |
} | |
private function pause():void { | |
if (isPaused) { | |
ns.resume(); | |
isPaused = false; | |
} else { | |
ns.pause(); | |
isPaused = true; | |
} | |
} | |
private function netStatusHandler(event:NetStatusEvent):void { | |
// statusText.text = event.info.code; | |
trace(event.info.code); | |
logJS(event.info.code); | |
switch(event.info.code) { | |
case "NetConnection.Connect.Success": | |
sendJSEvent("connected") | |
connectEnd = new Date(); | |
// statusText.text = "NetConnection established"; | |
logJS("NetConnection established"); | |
ns = new NetStream(nc); | |
ns.client = this; | |
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); | |
ns.soundTransform = new SoundTransform(); | |
ns.play("media.mp3"); | |
break; | |
case "NetStream.Play.Start": | |
if (!isPlaying) { | |
sendJSEvent("playing"); | |
playStart = new Date(); | |
// connectText.text = (connectEnd.valueOf() - connectStart.valueOf()) + "ms"; | |
// logJS("Connect: " + connectText.text); | |
// playText.text = (playStart.valueOf() - connectStart.valueOf()) + "ms"; | |
// logJS("Play: " + playText.text); | |
} | |
break; | |
case "NetStream.Buffer.Full": | |
if (!isPlaying) { | |
sendJSEvent("bufferFull"); | |
isPlaying = true; | |
// bufferText.text = ((new Date()).valueOf() - connectStart.valueOf()) + "ms"; | |
// logJS("Buffer full: " + bufferText.text); | |
} | |
if (isSeeking) { | |
sendJSEvent("seeked"); | |
isSeeking = false; | |
// seekText.text = ((new Date()).valueOf() - seekStart.valueOf()) + "ms"; | |
// logJS("Seek: " + seekText.text); | |
} | |
break; | |
case "NetStream.Seek.Notify": | |
if (!isSeeking) { | |
isSeeking = true; | |
} | |
break; | |
} | |
} | |
private function securityErrorHandler(event:SecurityErrorEvent):void { | |
trace("securityErrorHandler: " + event); | |
} | |
public function onBWDone(... rest):void { | |
trace("onBWDone() called") | |
var bandwidthTotal:Number; | |
if (rest.length > 0){ | |
bandwidthTotal = rest[0]; | |
// This code runs | |
// when the bandwidth check is complete. | |
trace("bandwidth = " + bandwidthTotal + " Kbps."); | |
} | |
} | |
public function onMetaData(info:Object):void { | |
trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate); | |
} | |
public function onCuePoint(info:Object):void { | |
trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment