kamaitachi example live_streaming

by typester
♥0 | Line 83 | Modified 2010-10-16 03:46:56 | MIT License
play

ActionScript3 source code

/**
 * Copyright typester ( http://wonderfl.net/user/typester )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/4a8I
 */

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
    <mx:Script><![CDATA[
import flash.net.*;
import flash.events.*;
import flash.media.*;
import mx.core.*;

private var nc:NetConnection;
private var ns:NetStream;

private function status_handler(event:NetStatusEvent):void {
    switch (event.info.code) {
    case "NetConnection.Connect.Success":
        setStatus("Connected.");
        break;
    default:
        setStatus(event.info.code);
    }
}

private function setStatus(text:String):void {
    status.text = text;
}

private function connectConn():void {
    var host_name:String = host.text;
    if (!host_name) return; 

    nc = new NetConnection();
    nc.addEventListener(NetStatusEvent.NET_STATUS, status_handler);
    nc.objectEncoding = ObjectEncoding.AMF0;
    nc.client = this;
    nc.connect(host_name);
}

private function closeConn():void {
    nc.close();
}

private function playNs():void {
    var channel_name:String = input.text;
    if (!channel_name) return;

    ns = new NetStream(nc);
    ns.addEventListener(NetStatusEvent.NET_STATUS, status_handler);

    var video:Video = new Video(320, 240);
    video.attachNetStream(ns);

    var ui:UIComponent = new UIComponent();
    ui.addChild(video);

    video_container.addChild(ui);

    ns.play(channel_name);
}

private function pauseNs():void {
    ns.pause();
}

private function resumeNs():void {
    ns.resume();
}

private function closeNs():void {
    ns.close();
}

private function seekNs():void {
    ns.seek(100);
}

public function onMessage(message:String):void {
    message_label.text =  message;
}
    ]]></mx:Script>
    <mx:Panel title="simple live play example" paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">
        <mx:HBox>
            <mx:Label text="Host:"/>
            <mx:TextInput id="host" width="120" text="rtmp://127.0.0.1/stream/live" />
            <mx:Button id="connectConn_button" label="connect" click="connectConn()" enabled="true"/>
            <mx:Button id="closeConn_button" label="close" click="closeConn()" enabled="true"/>
        </mx:HBox>
        <mx:HBox>
            <mx:TextInput id="input" width="120"/>
            <mx:Button id="play_button" label="play" click="playNs()" enabled="true"/>
            <mx:Button id="pause_button" label="pause" click="pauseNs()" enabled="true"/>
            <mx:Button id="resume_button" label="resume" click="resumeNs()" enabled="true"/>
            <mx:Button id="seek_button" label="seek" click="seekNs()" enabled="true"/>
            <mx:Button id="close_button" label="close" click="closeNs()" enabled="true"/>
        </mx:HBox>
        <mx:HBox id="video_container" width="320" height="240" />
        <mx:VBox>
        <mx:Label id="status" text="Connecting..."/>
        <mx:Label id="message_label" text=""/>
        </mx:VBox>
    </mx:Panel>
</mx:Application>