Flash NetStream接收数据但不总是显示它

时间:2016-02-29 17:08:51

标签: flash video rtmp

我有一个连接到EvoStream服务器的Flash应用程序,用于通过RTMP接收视频。我从SNC Camera Emulator和物理相机中获取视频。当我第一次启动时,物理摄像头显示视频,但模拟摄像头不显示。保持播放器运行,我关闭了SNC程序,然后出现一帧视频。当我重新启动SNC程序时,视频现在可以在客户端中正常播放。

当我打印出NetStream.info.currentBytesPerSecond时,无论视频是否正在播放,我都会得到类似的值。

以下是负责显示视频的客户端代码;它与Adobe提供的示例代码基本相同。

package {
import flash.display.Sprite;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.Event;

public class testRTMP extends Sprite {
    private var connection:NetConnection;
    private var stream:NetStream;
    private var video:Video = new Video();   
    private var sID: String;

    public function testRTMP(serverURL: String, streamId : String) {
        sID = streamId;
        connection = new NetConnection();
        connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
        connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        connection.connect(serverURL);
    }

    private function netStatusHandler(event:NetStatusEvent):void {
        switch (event.info.code) {
            case "NetConnection.Connect.Success":
                connectStream();
                break;
            case "NetStream.Play.StreamNotFound":
                trace("Stream not found: " + sID);
                break;
        }
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void {
        trace("securityErrorHandler: " + event);
    }

    private function connectStream():void {
        addChild(video);
        var stream:NetStream = new NetStream(connection);
        stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
        stream.client = new CustomClient();
        video.attachNetStream(stream);
        stream.play(sID);
    }
}
}

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);
}
}

1 个答案:

答案 0 :(得分:0)

尝试添加:

connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
connection.client = { onBWDone: function():void{} };

然后确保onFCSubscribeonBWDone存在,因为Flash Player可能需要调用它们...

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."); 
}

同时尝试删除第var stream:NetStream = etc...行。

private function connectStream() : void 
{
    addChild(video);
    //var stream:NetStream = new NetStream(connection); //# Is already a (private) var...
    stream = new NetStream(connection);
    stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
    stream.client = new CustomClient();
    video.attachNetStream(stream);
    stream.play(sID);
}