我正在尝试制作像jwplayer这样的动态RTMP视频播放器。我的播放器从swfobect收集流可验证的流,然后在as3中通过netstream播放。我将netstream附加到我的舞台上。但我面临一些奇怪的问题。当我调整舞台大小或全屏播放我的播放器时,视频得到了拉伸。我试过stage.scaleMode,但它无法正常工作。请检查下面的主要as3,并给我一个解决方案,以便我的视频可以获得我的播放器/舞台的任何大小的纵横比。看截图
主要ActionScript代码
package com.WindchimeVedioPlayer {
import flash.media.*;
import flash.system.Security;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.display.*;
import flash.display.DisplayObject;
import com.WindchimeVedioPlayer.RTMPStream;
Security.allowDomain("*");
public class main extends RTMPStream {
/* the constructor. */
public function main():void {
trace("Downstream object has been created."); // debug trace..
//stage.scaleMode=StageScaleMode.NO_BORDER;
this.oVideo = new Video(640, 480);
this.oConnection = new NetConnection();
this.oConnection.addEventListener(NetStatusEvent.NET_STATUS, eNetStatus, false, 0, true);
this.oConnection.connect(this.sMediaServerURL);
addChild(logo);
//logo.x=440;
}
/* triggered when a net status event is received. */
private function eNetStatus(oEvent1:NetStatusEvent) {
trace("NetStatusEvent: " + oEvent1.info.code); // debug trace..
switch (oEvent1.info.code) {
case "NetConnection.Connect.Success":
// create a stream for the connection..
this.oNetStream = new NetStream(oConnection);
this.oNetStream.addEventListener(NetStatusEvent.NET_STATUS, eNetStatus, false, 0, true);
this.oNetStream.bufferTime = 5; // set this to whatever is comfortable..
// listen for meta data..
this.oMetaData.onMetaData = eMetaDataReceived;
this.oNetStream.client = this.oMetaData;
// attach the stream to the stage..
this.oVideo.attachNetStream(oNetStream);
this.oNetStream.play(sStreamName);
this.addChildAt(this.oVideo, 0);
trace("Connected to the RTMP server."); // debug trace..
break;
case "NetConnection.Connect.Closed":
trace("Disconnected from the RTMP server."); // debug trace..
break;
case "NetStream.Play.StreamNotFound":
trace("This stream is currently unavailable."); // debug trace..
break;
}
}
}
}
请参阅操作脚本代码,并建议我如何修复它。
答案 0 :(得分:3)
好this.oNetStream.client = this.oMetaData;
需要由元数据处理功能备份。在那里你想要阅读原始视频宽度/高度&用它来调整容器尺寸以适应(保持纵横比)。我还建议使用Sprite作为视频的容器,而不是仅仅在舞台上粘贴它,如果你想变得有趣,以后更容易使用它。)
public var oMetaData : Object; //will keep a database of meta info
// listen for meta data..
oMetaData = new Object();
oMetaData.onMetaData = received_Meta;
oNetStream.client = oMetaData;
然后包括此函数来处理
function received_Meta (data:Object):void
{
trace("Detected Video Width (pixels) : " + data.width);
trace("Detected Video Height (pixels) : " + data.height);
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; //or try: _videoW * Aspect_num;
_aspectH = (_stageH - _videoH) / 2;
oVideo.x = 0;
oVideo.y = _aspectH;
oVideo.width = _videoW;
oVideo.height = _videoH;
}
更新:说明您将如何阅读元数据......
此行function received_Meta (data:Object):void
使用data
作为元数据持有者的参考名称(类型:Object
)。
在我发布的代码中,我有(data:Object)
,因此这行符号Aspect_num = data.height
如果您将其更改为:
(oObject:Object)
现在该行的作用为Aspect_num = oObject.height