无法在任何地方得到任何爱,这很难相信。NetStream 上的实时流和缓冲区不会混合。为什么?文档并没有说你不能这样做。
播放被冲洗掉了。缓冲区应该填充到您在播放之前设置的内容。相反,大约有一半的时间,播放器会立即开始播放,而忽略您设置的缓冲时间。然后,您会陷入短暂的缓冲和重新缓冲周期,从而降低播放体验。
自己试一试。这是最简单的直播视频播放器。没有多余的装饰。只需通过更改 ns.play() 语句中的 defaultURL 字符串和流名称将其连接到您的实时流。
在 Flex 调试模式下运行它。它将每秒检查并打印 ns.bufferLength 属性。缓冲区当前设置为 20 秒。上次我运行它时,我从来没有在缓冲区中超过 2 秒。播放器立即开始播放,而不是先缓冲完整的 20 秒。有时你会达到满 20 分,有时则不会。为什么?不知道。
您不能可靠地缓冲实时流还是代码有问题?
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
initialize="init()">
<mx:Script>
<![CDATA[
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import mx.core.UIComponent;
private var vid:Video;
private var videoHolder:UIComponent;
private var nc:NetConnection;
private var defaultURL:String="rtmp://your_streaming_server_url_here";
private var ns:NetStream;
private var msg:Boolean;
private var intervalMonitorBufferLengthEverySecond:uint;
private function init():void
{
vid=new Video();
vid.width=864;
vid.height=576;
vid.smoothing = true;
//Attach the video to the stage
videoHolder = new UIComponent();
videoHolder.addChild(vid);
addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
this.addChild(videoHolder);
connect();
}
public function onSecurityError(e:SecurityError):void
{
trace("Security error: ");
}
public function connect():void
{
nc = new NetConnection();
nc.client = this;
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.connect(defaultURL);
}
public function netStatusHandler(e:NetStatusEvent):void
{
switch (e.info.code) {
case "NetConnection.Connect.Success":
trace("Connected successfully");
createNS();
break;
case "NetConnection.Connect.Closed":
trace("Connection closed");
connect();
break;
case "NetConnection.Connect.Failed":
trace("Connection failed");
break;
case "NetConnection.Connect.Rejected":
trace("Connection rejected");
break;
case "NetConnection.Connect.AppShutdown":
trace("App shutdown");
break;
case "NetConnection.Connect.InvalidApp":
trace("Connection invalid app");
break;
}
}
public function createNS():void
{
trace("Creating NetStream");
ns=new NetStream(nc);
//nc.call("FCSubscribe", null, "live_production"); // Only use this if your CDN requires it
ns.addEventListener(NetStatusEvent.NET_STATUS, netStreamStatusHandler);
vid.attachNetStream(ns);
//Handle onMetaData and onCuePoint event callbacks: solution at http://tinyurl.com/mkadas
//See another solution at http://www.adobe.com/devnet/flash/quickstart/metadata_cue_points/
var infoClient:Object = new Object();
infoClient.onMetaData = function oMD():void {};
infoClient.onCuePoint = function oCP():void {};
ns.client = infoClient;
ns.bufferTime = 20;
ns.play("your_stream_name");
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function asyncErrorHandler(event:AsyncErrorEvent):void {
trace(event.text);
}
intervalMonitorBufferLengthEverySecond = setInterval(monPlayback, 1000);
}
public function netStreamStatusHandler(e:NetStatusEvent):void
{
switch (e.info.code) {
case "NetStream.Buffer.Empty":
trace("Buffer empty: ");
break;
case "NetStream.Buffer.Full":
trace("Buffer full:");
break;
case "NetStream.Play.Start":
trace("Play start:");
break;
}
}
public function monPlayback():void {
// Print current buffer length
trace("Buffer length: " + ns.bufferLength);
}
public function onBWDone():void {
//Do nothing
}
public function onFCSubscribe(info:Object):void {
// Do nothing. Prevents error if connecting to CDN.
}
public function onFCUnsubscribe(info:Object):void {
// Do nothing. Prevents error if connecting to CDN.
}
]]>
</mx:Script>
</mx:Application>
我最后一次跑步:
Connected successfully
Creating NetStream
Play start:
Buffer length: 0.001
Buffer full: //Obviously the buffer is not full here. Bogus.
Buffer length: 2.202
Buffer length: 2.369
Buffer length: 2.102
Buffer length: 2.402
Buffer length: 2.302
Buffer length: 2.369
Buffer length: 2.269
Buffer length: 2.269
Buffer length: 2.302
Buffer length: 2.369
Buffer length: 1.926
Buffer length: 2.336
Buffer length: 2.286
Buffer length: 2.336
Buffer length: 2.336
Buffer length: 2.403
Buffer length: 2.388
Buffer length: 2.402
Buffer length: 2.335
Buffer length: 2.369
Buffer length: 2.336
Buffer length: 2.339
Buffer length: 2.369
Buffer length: 2.402
Buffer length: 2.369
Buffer length: 2.396
Buffer length: 2.436
Buffer length: 2.336
Buffer length: 2.269