gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:23:27 24 4
gpt4 key购买 nike

我正在开发一个原型(prototype),我必须通过 RTMP 协议(protocol)播放视频。我的代码如下:

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

}

但是它没有播放,没有发生任何错误,也没有运行,如果有人有任何想法,请帮助我。

最佳答案

这样做对我有用。我只是以新闻 channel 的链接为例,因此请尝试将其替换为您自己的流 url。 (ps:忽略像素化,这是一个低分辨率的示例链接)。

另外..首先你有一个错字,你说vid = new video(); (意思是 = new Video ??)。这可能是 addChild(vid) 的问题吗?更进一步?其次,您需要像 这样的功能。异步错误处理程序 , onFC订阅 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 现在更加严格。在没有这条线之前它可以正常工作)。
  • 关于actionscript-3 - 如何通过 netConnection 和 netStream 播放 RTMP 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15967523/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com