0
我基本上開始使用Flex和netstream進行視頻通話。所以我能夠閱讀一些關於Netstreams和streaming的內容,並且我編寫了這段代碼來獲取我的相機並在視圖中的視頻顯示中發佈我的流,但即使我沒有任何錯誤地通過所有方法,顯示也不是顯示所以我不知道發生了什麼。這是我做的。無法正確顯示來自NetStream的視頻
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160" creationComplete="start();">
<fx:Script>
import flash.media.Camera;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.events.AsyncErrorEvent;
import flash.events.NetStatusEvent;
import flash.net.NetStream;
import mx.graphics.ImageSnapshot;
import mx.graphics.codec.JPEGEncoder;
public var camera:Camera;
var video:Video;
public var myVideo:Video;
private var nc:NetConnection;
private var rtmpf:String="rtmfp://p2p.rtmfp.net/61c33c80be7022350a0dea3d-960194f988ba/";
private const DEVKEY:String = "61c33c80be7022350a0dea3d-960194f988ba";
public var in_ns:NetStream;
public var out_ns:NetStream;
public function start():void{
trace("Started the start function");
nc=new NetConnection();
nc.objectEncoding = ObjectEncoding.AMF0;
nc.client=this;
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.connect(rtmpf);
}
public function netStatusHandler(event:NetStatusEvent):void{
switch(event.info.code){
case "NetConnection.Connect.Success":
trace("Received the status");
initStart();
default:
trace(event.info.code);
}
}
public function initStart():void{
trace("Started the initstart function");
initNetStream();
initMyVideo();
publish();
playIt();
}
public function initNetStream():void{
trace("Started the initNetstream start function");
out_ns=new NetStream(nc);
out_ns.client=this;
in_ns=new NetStream(nc);
in_ns.client=this;
}
public function publish():void{
trace("Started the publish function");
camera=Camera.getCamera();
out_ns.attachCamera(camera);
out_ns.publish("Me", "live");
}
public function startCamera(muteCam:Boolean=false):void{
if(!video)
video = new Video();
trace("Started the startCamera function");
camera=Camera.getCamera();
if(muteCam){
video.attachCamera(camera);
//out_ns.attachCamera(camera);
//out_ns.publish("Me", "live");
vidHolder.addChild(video);
}else{
video.attachCamera(null);
if(contains(video))
vidHolder.removeChild(video);
//camera=null;
}
}
public function initMyVideo():void
{
trace("Started the initmyvideo function");
myVideo = new Video(230,160);
myVideo.x = 10;
myVideo.width = 230;
myVideo.height = 160;
myVideo.y = 30;
// myVid.addChild(myVideo);
}
public function playIt():void{
trace("Started the play it function");
myVideo.attachNetStream(in_ns);
in_ns.play("Me");
myVid.addChild(myVideo);
}
public function stopCamera():void{
vidHolder.removeChild(video);
}
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button x="116" y="28" label="Start" click="startCamera(true)"/>
<s:VideoDisplay id="vidHolder" x="31" y="87" width="200" height="100"
/>
<s:VideoDisplay id="myVid" x="31" y="250"/>
<s:Button id="stop" x="208" y="28" label="Stop" click="startCamera(false)"/>
</s:Application>
我按原樣運行你的代碼,它似乎工作。我看到來自我的攝像頭的視頻。你能澄清這部分不起作用嗎? –
哦,它看起來像你試圖使用兩個具有相同NetConnection的NetStream?所以也許你想弄清楚如何展示他人的視頻?如果是這樣,我建議爲另一個NetStream使用第二個NetConnection(我總是這樣做,也許它以你的方式工作)。遵循你的代碼有點混亂,所以我會停止猜測發生了什麼/出錯,並讓你澄清。 –
最後,我建議你在NetStreams上添加一個NetStatusEvent處理程序。你會得到狀態信息,表明發佈已經開始,等等。這些信息可以幫助你解決發生的問題,只需要確定'event.info.code'的所有值。 –