actionscript-3 – 如何通过netConnection和netStream播放RTMP视频

我正在制作一个原型,我必须通过RTMP协议播放视频.我的代码如下:

private function init():void
    {
        streamID:String = "mp4:myVideo";
        videoURL = "rtmp://fms.xstream.dk/*********.mp4";
        vid = new video();
        vid.width = 480;
        vid.height = 320;

        nc = new NetConnection();
        nc.client = {onBWDone: function():void
            {
            }};
        nc.addEventListener(NetStatusEvent.NET_STATUS,onConnectionStatus);
        nc.connect(videoURL);           
    }

    private function onConnectionStatus(e:NetStatusEvent):void
    {
        if (e.info.code == "NetConnection.Connect.Success")
        {
            trace("Creating NetStream");
            netStreamObj = new NetStream(nc);
            netStreamObj.client = new CustomClient();
            netStreamObj.play(streamID);
            vid.attachNetStream(netStreamObj);
            addChild(vid);
            intervalID = setInterval(playback,1000);
        }
    }

    private function playback():void
    {
        trace((++counter) + " Buffer length: " + netStreamObj.bufferLength);
    }





class CustomClient 
{
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);
}

}

但它没有播放,没有发生任何错误而且没有合作,如果有人有任何想法,请帮助我.

解决方法

这样做对我有用.我只是使用新闻频道的链接作为示例,因此请尝试使用您自己的流网址替换它. (ps:忽略像素化,这是一个低分辨率的示例链接).

另外..首先你有一个错字,你说vid = new video(); (意思是=新视频??).这可能是addChild(vid)行的问题吗?其次你需要asyncErrorHandler,onFCSubscribe和onBWDone等函数,我在使用RTMP时已经包含这些函数来阻止某些流丢失的错误(无论如何我在过去的经历中).此示例代码位于名为RTMP_test.as的文档类中(重命名为首选)…

package  {

import flash.display.*; 
import flash.events.*;
import flash.net.*;
import flash.media.*;
import flash.system.*;
import flash.utils.ByteArray;

public class RTMP_test extends MovieClip 
{
    public var netStreamObj:NetStream;
    public var nc:NetConnection;
    public var vid:Video;

    public var streamID:String;
    public var videoURL:String;
    public var MetaListener:Object;

public function RTMP_test () 
{ init_RTMP(); }

function init_RTMP():void
{
        /*
        streamID  = "mp4:myVideo";
        videoURL = "rtmp://fms.xstream.dk/*********.mp4";
        */
        streamID  = "QVCLive1@14308";
        videoURL = "rtmp://cp79650.live.edgefcs.net/live/";

        vid = new Video(); //typo! was "vid = new video();"

        nc = new NetConnection();
        nc.addEventListener(NetStatusEvent.NET_STATUS,onConnectionStatus);
        nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncErrorHandler);
        nc.client = { onBWDone: function():void{} };
        nc.connect(videoURL);           
}

private function onConnectionStatus(e:NetStatusEvent):void
{
        if (e.info.code == "NetConnection.Connect.Success")
        {
            trace("Creating NetStream");
            netStreamObj = new NetStream(nc);

            MetaListener = new Object();
            MetaListener.onMetaData = received_Meta;
            netStreamObj.client = MetaListener;

            netStreamObj.play(streamID);
            vid.attachNetStream(netStreamObj);
            addChild(vid);
            //intervalID = setInterval(playback,1000);
        }
}

private function playback():void
{ 
  //trace((++counter) + " Buffer length: " + netStreamObj.bufferLength); 
}

public function asyncErrorHandler(event:AsyncErrorEvent):void 
{ trace("asyncErrorHandler.." + "\r"); }

public function onFCSubscribe(info:Object):void
{ trace("onFCSubscribe - succesful"); }

public function onBWDone(...rest):void
{ 
    var p_bw:Number; 
    if (rest.length > 0)
      { p_bw = rest[0]; }
    trace("bandwidth = " + p_bw + " Kbps."); 
}

function received_Meta (data:Object):void
{
    var _stageW:int = stage.stageWidth;
    var _stageH:int = stage.stageHeight;

    var _videoW:int;
    var _videoH:int;
    var _aspectH:int; 

    var Aspect_num:Number; //should be an "int" but that gives blank picture with sound
    Aspect_num = data.width / data.height;

    //Aspect ratio calculated here..
    _videoW = _stageW;
    _videoH = _videoW / Aspect_num;
    _aspectH = (_stageH - _videoH) / 2;

    vid.x = 0;
    vid.y = _aspectH;
    vid.width = _videoW;
    vid.height = _videoH;
}

    } //end class

} //end package

更新的代码

>新的演示链接:现在QVC(英国购物)而不是今日俄罗斯(世界新闻).>添加行:nc.client = {onBWDone:function():void {}}; (因为Flash Player现在更严格了.没有这条线之前它工作正常).

相关文章

Chrome 无法显示使用插件的内容 Chrome 不再支持很多插件。不过网站创建者已经通过更安全的方式,将多数...
Flash 开发环境搭建和文字滚动效果实例 一、Flash 开发环境搭建 Flash发布的时候可以将资源(即将库中的...
方法一: 1、选择合适的yum源http://get.adobe.com/cn/flashplayer/进入此网址选择 “YUM,适用于Linux...
在做web开发中可能会遇到flash遮挡页面中元素的情况,无论怎么设置flash容器和层的深度(z-index)也无济...
chrome 45以上不自动播放"非必要"flash的一种可能解决方案chrome 45以上flash被拦截...
透过现象看本质 首先是个转场特效问题。 其实我早想到做法了,只是当时还没实践。增加混合(BlendMode....