Adobe FLA:自动播放

时间:2011-03-29 17:15:38

标签: flash actionscript-3

我对Flash Actionscript开发非常陌生,并且在用户点击播放时继承了一个开始播放网络流的脚本。

我去了动作并右键点击了动作。下面的代码出现了。我想要实现的是在加载对象后立即播放流(但是obv用户应该能够单击按钮来停止流(每分钟付费)

任何人都可以指出我正确的方向。代码todo可能是触发器或其他地方的东西,这是我能找到的全部。

编辑修复:

var nc:NetConnection = null;
var nsPlay:NetStream = null;                      


var myQueryStrings=this.loaderInfo.parameters;
var user_id:String = myQueryStrings.user_id;
var site_id:String = myQueryStrings.site_id;
var stream_id:String = myQueryStrings.stream_id;
var RTMP:String = "rtmp://" + myQueryStrings.rtmp + "?" + user_id + "-" + site_id + "-" + stream_id;
var stream_query:String = stream_id + "?" + user_id + "-" + site_id + "-" + stream_id;


function ncOnStatus(infoObject:NetStatusEvent) {
   switch (infoObject.info.code) {
       case "NetConnection.Connect.Success":
           _doPlay();
           break;
       case "NetStream.Play.StreamNotFound":
           trace("Unable to locate video");
        break;
   }
}


function doConnect() {
    trace("doConnect");

    // connect to the Wowza Media Server
    if (nc == null) {
        trace("nc is null");
        // create a connection to the wowza media server
        nc = new NetConnection();
        nc.connect(RTMP);

        // get status information from the NetConnection object
        nc.addEventListener(NetStatusEvent.NET_STATUS, ncOnStatus);
        doSubscribe.addEventListener(MouseEvent.CLICK, subscribe);
        doSubscribe.label = 'Stop';//Rename your btn to Stop
    } else {
        trace("nc not null");
        nsPlay = null;
        videoRemote.attachNetStream(null);
        videoRemote.clear();
        nc.close();
        nc = null;
        prompt.text = "";
    }
}



function nsPlayOnStatus(infoObject:NetStatusEvent) {
    trace("nsPlay: "+infoObject.info.code+" ("+infoObject.info.description+")");
    if (infoObject.info.code == "NetStream.Play.StreamNotFound" || infoObject.info.code == "NetStream.Play.Failed")
        prompt.text = infoObject.info.description;
}


function subscribe(event:MouseEvent) {
    trace("subscribe");

    if (doSubscribe.label == 'Play') {
        _doPlay();//moved code into a method so you can call it on load
    } else {       
        // here we are shutting down the connection to the server
        videoRemote.attachNetStream(null);
        nsPlay.play(null);
        nsPlay.close();

        doSubscribe.label = 'Play';
    }
}


function nsPublishOnStatus(infoObject:NetStatusEvent) {
    trace("nsPublish: "+infoObject.info.code+" ("+infoObject.info.description+")");
    if (infoObject.info.code == "NetStream.Play.StreamNotFound" || infoObject.info.code == "NetStream.Play.Failed")
        prompt.text = infoObject.info.description;
}


function _doPlay():void {
    // create a new NetStream object for video playback
        trace("do play");
        trace(nc);
        nsPlay = new NetStream(nc);

        // trace the NetStream status information
        nsPlay.addEventListener(NetStatusEvent.NET_STATUS, nsPlayOnStatus);

        var nsPlayClientObj:Object = new Object();
        nsPlay.client = nsPlayClientObj;
        nsPlayClientObj.onMetaData = function(infoObject:Object) 
        {
            trace("onMetaData");

            // print debug information about the metaData
            for (var propName:String in infoObject)
            {
                trace("  "+propName + " = " + infoObject[propName]);

                if (propName == "CameraWidth") { videoRemote.width = infoObject["CameraWidth"]; }
                if (propName == "CameraHeight") { videoRemote.height = infoObject["CameraHeight"]; }
            }
        };      

        // set the buffer time to zero since it is chat
        nsPlay.bufferTime = 0;

        // subscribe to the named stream
        nsPlay.play(stream_query);

        //prompt.text = stream_query;

        // attach to the stream
        videoRemote.attachNetStream(nsPlay);

        doSubscribe.label = 'Stop';
}


stage.align = "TL";
stage.scaleMode = "noScale";


doConnect();
stop();

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

var nc:NetConnection = null;
var nsPlay:NetStream = null;                      

//get var from flash page
var myQueryStrings=this.loaderInfo.parameters;
var user_id:String = myQueryStrings.user_id;
var site_id:String = myQueryStrings.site_id;
var stream_id:String = myQueryStrings.stream_id;
var RTMP:String = "rtmp://" + myQueryStrings.rtmp + "?" + user_id + "-" + site_id + "-" + stream_id;
var stream_query:String = stream_id + "?" + user_id + "-" + site_id + "-" + stream_id;

function ncOnStatus(infoObject:NetStatusEvent)
{
    trace("nc: "+infoObject.info.code+" ("+infoObject.info.description+")");
    if (infoObject.info.code == "NetConnection.Connect.Failed")
        prompt.text = "Connection failed: please refresh the page and try again";
    else if (infoObject.info.code == "NetConnection.Connect.Rejected")
        prompt.text = infoObject.info.description;
}

function doConnect()
{
    // connect to the Wowza Media Server
    if (nc == null)
    {
        // create a connection to the wowza media server
        nc = new NetConnection();
        nc.connect(RTMP);

        // get status information from the NetConnection object
        nc.addEventListener(NetStatusEvent.NET_STATUS, ncOnStatus);

        doSubscribe.addEventListener(MouseEvent.CLICK, subscribe);
        //doSubscribe.label = 'Play';
        doSubscribe.label = 'Stop';//Rename your btn to Stop
        _doPlay();//fire the video
        //prompt.text = RTMP;
    }
    else
    {
        nsPlay = null;

        videoRemote.attachNetStream(null);
        videoRemote.clear();

        nc.close();
        nc = null;

        prompt.text = "";
    }
}



function nsPlayOnStatus(infoObject:NetStatusEvent)
{
    trace("nsPlay: "+infoObject.info.code+" ("+infoObject.info.description+")");
    if (infoObject.info.code == "NetStream.Play.StreamNotFound" || infoObject.info.code == "NetStream.Play.Failed")
        prompt.text = infoObject.info.description;
}

function subscribe(event:MouseEvent)
{
    if (doSubscribe.label == 'Play')
    {
        _doPlay();//moved code into a method so you can call it on load
    }
    else
    {       
        // here we are shutting down the connection to the server
        videoRemote.attachNetStream(null);
        nsPlay.play(null);
        nsPlay.close();

        doSubscribe.label = 'Play';
    }
}

function nsPublishOnStatus(infoObject:NetStatusEvent)
{
    trace("nsPublish: "+infoObject.info.code+" ("+infoObject.info.description+")");
    if (infoObject.info.code == "NetStream.Play.StreamNotFound" || infoObject.info.code == "NetStream.Play.Failed")
        prompt.text = infoObject.info.description;
}

function _doPlay():void {
    // create a new NetStream object for video playback
        nsPlay = new NetStream(nc);

        // trace the NetStream status information
        nsPlay.addEventListener(NetStatusEvent.NET_STATUS, nsPlayOnStatus);

        var nsPlayClientObj:Object = new Object();
        nsPlay.client = nsPlayClientObj;
        nsPlayClientObj.onMetaData = function(infoObject:Object) 
        {
            trace("onMetaData");

            // print debug information about the metaData
            for (var propName:String in infoObject)
            {
                trace("  "+propName + " = " + infoObject[propName]);

                if (propName == "CameraWidth") { videoRemote.width = infoObject["CameraWidth"]; }
                if (propName == "CameraHeight") { videoRemote.height = infoObject["CameraHeight"]; }
            }
        };      

        // set the buffer time to zero since it is chat
        nsPlay.bufferTime = 0;

        // subscribe to the named stream
        nsPlay.play(stream_query);

        //prompt.text = stream_query;

        // attach to the stream
        videoRemote.attachNetStream(nsPlay);

        doSubscribe.label = 'Stop';
}

stage.align = "TL";
stage.scaleMode = "noScale";

doConnect();
stop();