Problem to get rtmp from server under compile I get this 4 errors:
* NewClass.as, Line 30 1120: Access of undefined property nc.
* NewClass.as, Line 31 1120: Access of undefined property nc.
* NewClass.as, Line 32 1120: Access of undefined property nc.
* NewClass.as, Line 33 1120: Access of undefined property nc.
* NewClass.as, Line 41 1120: Access of undefined property nc
I am stuck Have no ideas what to do
package {
import flash.accessibility.Accessibility;
import flash.display.Sprite;
import flash.events.*;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
public class NewClass extends Sprite
{
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 = "some";
videoURL = "rtmp://someserver/application/";
vid = new Video();
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
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 _aspectH:int;
var _videoW:int;
var _videoH:int;
var relationship:Number;
relationship = data.height / data.width;
//Aspect ratio calculated here..
_videoW = _stageW;
_videoH = _videoW * relationship;
_aspectH = (_stageH - _videoH) / 2;
vid.x = 0;
vid.y = _aspectH;
vid.width = _videoW;
vid.height = _videoH;
}
}
}
You have the declaration for variable nc commented out on line 12.
Related
Good Morning,
i am working on a video class, using a CRTMP Server for streaming. This works fine, but for my solution i need to duplicate the video stream (for some effects).
I googled for duplicate MovieClips and tried to duplicate the video like this.
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.media.*;
import flash.system.*;
import flash.utils.ByteArray;
public class Main extends MovieClip
{
public var netStreamObj:NetStream;
public var nc:NetConnection;
public var vid:Video;
public var vid2:Video;
public var streamID:String;
public var videoURL:String;
public var metaListener:Object;
public function Main()
{
init_RTMP();
}
private function init_RTMP():void
{
streamID = "szene3.f4v";
videoURL = "rtmp://213.136.73.230/maya";
vid = new Video(); //typo! was "vid = new video();"
vid2 = 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);
//vid2.attachNetStream(netStreamObj); // wont work
addChild(vid);
// addChild(vid2); // wont work either
//intervalID = setInterval(playback, 1000);
}
}
private function asyncErrorHandler(event:AsyncErrorEvent):void
{
trace("asyncErrorHandler.." + "\r");
}
private 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;
vid2.x = 0;
vid2.y = _aspectH ;
}
}
It should be possible to duplicate the video stream. 2 Instance of the same videoStream. What am i doing wrong ?
Thanks for help.
"This means that i have to double the netstream. This is not what i want."
"I tried to duplicate the video per Bitmap.clone. But i got an sandbox violation."
You can try the workaround suggested here: Netstream Play(null) Bitmapdata Workaround
I'll show a quick demo of how it can be applied to your code.
This demo code assumes your canvas is width=550 & height=400. Keep that ratio if scaling up.
package
{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.media.*;
import flash.system.*;
import flash.utils.ByteArray;
import flash.geom.*;
import flash.filters.ColorMatrixFilter;
public class Main 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 var vid1_sprite : Sprite = new Sprite();
public var vid2_sprite : Sprite = new Sprite();
public var vid2_BMP : Bitmap;
public var vid2_BMD : BitmapData;
public var colMtx:Array = new Array(); //for ColorMatrix effects
public var CMfilter:ColorMatrixFilter;
public function Main()
{
init_RTMP();
}
private function init_RTMP():void
{
streamID = "szene3.f4v";
videoURL = "rtmp://213.136.73.230/maya";
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(null); //for file playback
nc.connect(videoURL); //for RTMP streams
}
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("vid.flv"); //if File
netStreamObj.play(streamID); //if RTMP
vid.attachNetStream(netStreamObj);
}
}
private function asyncErrorHandler(event:AsyncErrorEvent):void
{
trace("asyncErrorHandler.." + "\r");
}
private function received_Meta(data:Object):void
{
trace("Getting metadata");
var _stageW:int = stage.stageWidth;
var _stageH:int = stage.stageHeight;
var _videoW:int = data.width;
var _videoH:int = data.height;
var _aspectH:int = 0;
var Aspect_num:Number;
Aspect_num = data.width / data.height;
//Aspect ratio calculated here..
_videoW = _stageW;
_videoH = _videoW / Aspect_num;
_aspectH = (_stageH - _videoH) / 2;
trace("_videoW : " + _videoW);
trace("_videoW : " + _videoH);
trace("_aspectH : " + _aspectH);
vid.x = 0;
vid.y = 0;
vid.width = _videoW;
vid.height = _videoH;
setup_Copy(); //# Do this after video resize
}
public function setup_Copy () : void
{
vid2_BMD = new BitmapData(vid.width, vid.height, false, 0);
vid2_BMP = new Bitmap( vid2_BMD );
vid1_sprite.addChild(vid);
vid1_sprite.x = 0;
vid1_sprite.y = 0;
addChild( vid1_sprite );
vid2_sprite.addChild( vid2_BMP );
vid2_sprite.x = 0;
vid2_sprite.y = vid.height + 5;
addChild( vid2_sprite );
stage.addEventListener(Event.ENTER_FRAME, draw_Video);
}
public function draw_Video (evt:Event) : void
{
if ( netStreamObj.client.decodedFrames == netStreamObj.decodedFrames ) { return; } // Here we skip multiple readings
//# Get bitmapdata directly from container of video
if ( vid1_sprite.graphics.readGraphicsData().length > 0 )
{
vid2_BMD = GraphicsBitmapFill(vid1_sprite.graphics.readGraphicsData()[0]).bitmapData;
}
effect_BitmapData(); //# Do an effect to bitmapdata
}
public function effect_BitmapData ( ) : void
{
//# Matrix for Black & White effect
colMtx = colMtx.concat([1/3, 1/3, 1/3, 0, 0]); // red
colMtx = colMtx.concat([1/3, 1/3, 1/3, 0, 0]); // green
colMtx = colMtx.concat([1/3, 1/3, 1/3, 0, 0]); // blue
colMtx = colMtx.concat([0, 0, 0, 1, 0]); // alpha
CMfilter = new ColorMatrixFilter(colMtx);
vid2_BMP.bitmapData.applyFilter(vid2_BMD, new Rectangle(0, 0, vid.width, vid.height), new Point(0, 0), CMfilter);
}
}
}
I've created a zoom function but when I try to scale the bg_image nothing happends. It is like I cannot access it's properties. Does anyone know why? :)
Ty!
Main class
package
{
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.text.TextField;
import fl.controls.Button;
import fl.controls.List;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.display.Shape;
import fl.transitions.Fly;
import fl.motion.MatrixTransformer;
import flash.events.KeyboardEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.ui.Mouse;
public class Main extends Sprite
{
// Zoom
public static var scale:Number = 1;
public var spImage:Sprite;
public var mat:Matrix;
public var mcIn:MovieClip;
public var mcOut:MovieClip;
public var boardWidth:int = 980;
public var boardHeight:int = 661;
public var boardMask:Shape;
public var externalCenter:Point;
public var internalCenter:Point;
public const scaleFactor:Number = 0.8;
public var minScale:Number = 0.25;
public var maxScale:Number = 10.0;
// ------------------------
// Grafikk
public var bg_image:Sprite;
//-------------------------------
//-----------------------------------------------
public var routeArray:Array;
public var startList:List = new List();
public var sluttList:List = new List();
public var S_Norway:Dictionary = new Dictionary();
public var S_Australia:Dictionary = new Dictionary();
public var S_China:Dictionary = new Dictionary();
public var S_South_Africa:Dictionary = new Dictionary();
public var S_Brazil:Dictionary = new Dictionary();
public var S_USA:Dictionary = new Dictionary();
public var S_France:Dictionary = new Dictionary();
// ------------------------------------------------------
public static var airportDict:Dictionary = new Dictionary();
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, init);
// ---------------------------------
}
public function init(e:Event):void
{
bg_image = new Image(0, 0);
this.addChild(bg_image);
bg_image.addEventListener(MouseEvent.CLICK, mouseCoordinates);
removeEventListener(Event.ADDED_TO_STAGE, init);
// Zoom
this.graphics.beginFill(0xB6DCF4);
this.graphics.drawRect(0,0,boardWidth,boardHeight);
this.graphics.endFill();
spImage = new Sprite();
this.addChild(spImage);
boardMask = new Shape();
boardMask.graphics.beginFill(0xDDDDDD);
boardMask.graphics.drawRect(0,0,boardWidth,boardHeight);
boardMask.graphics.endFill();
boardMask.x = 0;
boardMask.y = 0;
this.addChild(boardMask);
spImage.mask = boardMask;
minScale = boardWidth / bg_image.width;
mcIn = new InCursorClip();
mcOut = new OutCursorClip();
bg_image.addChild(mcIn);
bg_image.addChild(mcOut);
bg_image.scaleX = minScale;
bg_image.scaleY = minScale;
spImage.addChild(bg_image);
spImage.addChild(mcIn);
spImage.addChild(mcOut);
spImage.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
spImage.addEventListener(MouseEvent.CLICK, zoom);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
S_USA["x"] = 180.7;
S_USA["y"] = 149.9;
S_USA["bynavn"] = "New York";
S_Norway["x"] = 423.7;
S_Norway["y"] = 76.4;
S_Norway["bynavn"] = "Oslo";
S_South_Africa["x"] = -26;
S_South_Africa["y"] = 146;
S_South_Africa["bynavn"] = "Cape Town";
S_Brazil["x"] = 226;
S_Brazil["y"] = 431.95;
S_Brazil["bynavn"] = "Rio de Janeiro";
S_France["x"] = 459.1;
S_France["y"] = 403.9;
S_France["bynavn"] = "Paris";
S_China["x"] = 716.2;
S_China["y"] = 143.3;
S_China["bynavn"] = "Beijing";
S_Australia["x"] = 809.35;
S_Australia["y"] = 414.95;
S_Australia["bynavn"] = "Sydney";
// ----------------------------------------------------
airportDict["USA"] = S_USA;
airportDict["Norway"] = S_Norway;
airportDict["South Africa"] = S_South_Africa;
airportDict["Brazil"] = S_Brazil;
airportDict["France"] = S_France;
airportDict["China"] = S_China;
airportDict["Australia"] = S_Australia;
for (var k:Object in airportDict)
{
var value = airportDict[k];
var key = k;
startList.addItem({label:key, data:key});
sluttList.addItem({label:key, data:key});
var airport:Airport = new Airport(key,airportDict[key]["bynavn"]);
airport.koordinater(airportDict[key]["x"], airportDict[key]["y"]);
bg_image.addChild(airport);
}
// --------------------------------------------
// --------------------------------------------
// -----------------------------------------------
}
private function startDragging(mev:MouseEvent):void
{
spImage.startDrag();
}
private function stopDragging(mev:MouseEvent):void
{
spImage.stopDrag();
}
private function zoom(mev:MouseEvent):void
{
if ((!mev.shiftKey)&&(!mev.ctrlKey))
{
return;
}
if ((mev.shiftKey)&&(mev.ctrlKey))
{
return;
}
externalCenter = new Point(spImage.mouseX,spImage.mouseY);
internalCenter = new Point(bg_image.mouseX,bg_image.mouseY);
if (mev.shiftKey)
{
bg_image.scaleX = Math.max(scaleFactor*bg_image.scaleX, minScale);
bg_image.scaleY = Math.max(scaleFactor*bg_image.scaleY, minScale);
}
if (mev.ctrlKey)
{
trace("Minscale: ", maxScale)
trace("Returned: ", 1/scaleFactor*bg_image.scaleY)
bg_image.scaleX = Math.min(1/scaleFactor*bg_image.scaleX, maxScale);
bg_image.scaleY = Math.min(1/scaleFactor*bg_image.scaleY, maxScale);
}
mat = this.transform.matrix.clone();
MatrixTransformer.matchInternalPointWithExternal(mat,internalCenter,externalCenter);
bg_image.transform.matrix = mat;
}
private function keyHandler(ke:KeyboardEvent):void
{
mcIn.x = spImage.mouseX;
mcIn.y = spImage.mouseY;
mcOut.x = spImage.mouseX;
mcOut.y = spImage.mouseY;
mcIn.visible = ke.ctrlKey;
mcOut.visible = ke.shiftKey;
if (ke.ctrlKey || ke.shiftKey)
{
Mouse.hide();
}
else
{
Mouse.show();
}
}
private function reise(evt:MouseEvent):void
{
var new_flight:Flight = new Flight(airportDict[startList.selectedItem.label]["x"],airportDict[startList.selectedItem.label]["y"],airportDict[sluttList.selectedItem.label]["x"],airportDict[sluttList.selectedItem.label]["y"]);
bg_image.addChild(new_flight);
}
private function mouseCoordinates(event: MouseEvent):void
{
// these are the x and y relative to the object
var localMouseX:Number = bg_image.mouseX;
var localMouseY:Number = bg_image.mouseY;
trace("Local coordinates: ", localMouseX, localMouseY);
// these are the x and y relative to the whole stage
var stageMouseX:Number = event.stageX;
var stageMouseY:Number = event.stageY;
trace("Global coordinates: ", stageMouseX, stageMouseY);
}
}
}
Image class:
package {
import flash.display.Sprite;
import flash.display.MovieClip;
public class Image extends Sprite
{
public function Image(y_:Number, x_:Number)
{
this.y = y_
this.x = x_
}
}
}
You don't have minScale declared, neither maxScale.
bg_image should be declared as Sprite, otherwise Compiler Error: Implicit coercion...
`
public var bg_image:Sprite;
As your image is very small, you may want to add your listener to the stage, and not the image, it would be very dificult to click in such a small image.
Also, here is your example working with MOUSE_WHEEL, instead of Click + Ctrl / Shift
stage.addEventListener(MouseEvent.MOUSE_WHEEL, zoom);
private function zoom(mev:MouseEvent):void
{
mev.delta > 0 ?
bg_image.scaleX = bg_image.scaleY = Math.max(scaleFactor * bg_image.scaleX, minScale) :
bg_image.scaleX = bg_image.scaleY = Math.min(1/scaleFactor * bg_image.scaleX, maxScale) ;
}
I'm in the process of creating an app, all the pages are loaded in using the Loader class and while my metro.as loads fine, when I try to load,
'chords.swf' via URLRequest (var chordReq:URLRequest) i get this
error: TypeError: Error #1009: Cannot access a property or method of a
null object reference. at Chords()
package {
import flash.display.MovieClip;
import com.greensock.TweenMax;
import com.greensock.easing.*;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Loader;
import flash.net.URLRequest;
public class Main extends MovieClip {
var chordsIcon:navBTN;
var clockIcon:navBTN;
var metroIcon:navBTN;
var logo:logoMC;
var _back:backBTN;
var metroLoader:Loader;
var metroReq:URLRequest;
var chordLoader:Loader;
var chordReq:URLRequest;
public function Main() {
// constructor code
if(stage){
initMain();
}else{
this.addEventListener(Event.ADDED_TO_STAGE, initMain);
}
function initMain(){
chordsIcon = new navBTN('Chord Charts','Chords');
metroIcon = new navBTN('Metronome','Metronome');
clockIcon = new navBTN('Session Timer','Clock');
logo = new logoMC;
_back = new backBTN;
chordLoader = new Loader();
chordReq = new URLRequest('chords.swf');
metroLoader = new Loader();
metroReq = new URLRequest('metro.swf');
addChild(chordLoader);
chordLoader.x = 0;
chordLoader.y = 0;
addChild(metroLoader);
metroLoader.x = 320;
metroLoader.y = 0;
metroLoader.load(metroReq);
initInterface();
}//end initMain
function initInterface(){
addChild(logo);
logo.x = 160;
logo.y = stage.stageHeight - (logo.height / 2);
addChild(chordsIcon);
chordsIcon.x = stage.stageWidth / 2;
chordsIcon.y = 234;
addChild(clockIcon);
clockIcon.x = 90;
clockIcon.y = 350;
addChild(metroIcon);
metroIcon.x = 230;
metroIcon.y = 350;
chordsIcon.addEventListener(MouseEvent.CLICK, onClick);
clockIcon.addEventListener(MouseEvent.CLICK, onClick);
metroIcon.addEventListener(MouseEvent.CLICK, onClick);
}
function removeInterface(){
removeChild(logo);
removeChild(clockIcon);
removeChild(chordsIcon);
removeChild(metroIcon);
}
function onClick(e:MouseEvent){
removeInterface();
addChild(_back);
_back.x = 30;
_back.y = 22;
_back.addEventListener(MouseEvent.CLICK, onBackClick);
if(e.target == chordsIcon.hitSpot){
trace('Chords Clicked');
chordLoader.load(chordReq);
}
else if(e.target == metroIcon.hitSpot){
trace('Metronome Clicked');
metroLoader.x = 0;
}
else if(e.target == clockIcon.hitSpot){
trace('Clock Clicked');
}
}
function onBackClick(e:MouseEvent){
metroLoader.x = 320;
initInterface();
}
}//end public function
}
}
not sure why this isn't working because metroLoader loads fine, while the other won't go.
chords.as looks like this...
package {
import flash.display.MovieClip;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.events.Event;
import com.greensock.TweenMax;
import com.greensock.easing.*;
public class Chords extends MovieClip {
//declare vars
var cMaj:MovieClip = new cMaj_MC;
var cMin:MovieClip = new cMin_MC;
var c7:MovieClip = new c7_MC;
var dMaj:MovieClip = new dMaj_MC;
var dMin:MovieClip = new dMin_MC;
var d7:MovieClip = new d7_MC;
var eMaj:MovieClip = new eMaj_MC;
var eMin:MovieClip = new eMin_MC;
var e7:MovieClip = new e7_MC;
var fMaj:MovieClip = new fMaj_MC;
var fMin:MovieClip = new fMin_MC;
var f7 :MovieClip = new f7_MC;
var slideRight:rightBTN;
var slideLeft:leftBTN;
var chordList:Array = [cMaj, cMin, c7, dMaj, dMin, d7, eMaj, eMin, e7, fMaj, fMin, f7];
var currentMC = chordList[i];
var i:int = 0;
var verticalCenter = (stage.stageHeight / 2) - (currentMC.height / 2);
var horizontalCenter = (stage.stageWidth / 2) - (currentMC.width / 2);
public function Chords() {
// constructor code
if(stage){
initChords();
}else{
this.addEventListener(Event.ADDED_TO_STAGE, initChords);
}
function initChords() {
slideRight = new rightBTN;
slideLeft = new leftBTN;
addChild(slideRight);
slideRight.x = stage.stageWidth - (slideRight.width);
addChild(slideLeft);
slideLeft.x = 0;
slideRight.addEventListener(MouseEvent.CLICK, onRight);
slideLeft.addEventListener(MouseEvent.CLICK, onLeft);
slideRight.alpha = .5;
slideLeft.alpha = .5;
addChild(currentMC);
currentMC.x = horizontalCenter;
currentMC.y = verticalCenter;
}//end initChords
function onRight(e:MouseEvent){
trace(i);
trace(currentMC);
if(i == 11) {
slideRight.removeEventListener(MouseEvent.CLICK, onRight);
}else{
slideRight.addEventListener(MouseEvent.CLICK, onRight);
TweenMax.to(slideRight, .05, {alpha:1, yoyo:true, repeat:1});
TweenMax.to(currentMC, .25, {x:-320, alpha:0, onComplete:nextChord});
}
}
function nextChord(){
removeChild(currentMC);
i++;
addChild(currentMC);
currentMC.x = 320;
currentMC.y = verticalCenter;
currentMC.alpha = 0;
TweenMax.to(currentMC, .25, {x:horizontalCenter, alpha:1});
}
function lastChord(){
removeChild(currentMC);
i--;
addChild(currentMC);
currentMC.x = -320;
currentMC.y = verticalCenter;
currentMC.alpha = 0;
TweenMax.to(currentMC, .25, {x:horizontalCenter, alpha:1});
}
function onLeft(e:MouseEvent){
if(i > 0) {
slideLeft.addEventListener(MouseEvent.CLICK, onLeft);
TweenMax.to(slideLeft, .05, {alpha:1, yoyo:true, repeat:1});
TweenMax.to(currentMC, .25, {x:320, alpha:0, onComplete:lastChord});
}
}
}//end public function
}//end public class
}//end package
Have the declaraion of the variables verticalCenter , horizontalCenter on top and move these lines into function initChords
verticalCenter = (stage.stageHeight / 2) - (currentMC.height / 2);
horizontalCenter = (stage.stageWidth / 2) - (currentMC.width / 2);
Hope it helps.
I am working on a prototype in which I have to play a video through RTMP protocol. My code is following :
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);
}
}
But it not playing, not occurring any error and not plying, If anyone have any idea, please help me.
doing it this way worked for me. I just used a link to a news channel as example so try replacing it with your own stream url. (ps: ignore the pixelation, it's a low-res example link).
Also.. first you had a typo whereby you said vid = new video(); (meant = new Video??). Could that be an issue for the addChild(vid) line further on? Second you need functions like the asyncErrorHandler, onFCSubscribe and onBWDone that I've included when working with RTMP to stop errors that some streams throw out (in my past experiences anyway). This example code goes in a document class called RTMP_test.as (rename as preferred)...
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
UPDATED CODE:
New demo link: Now QVC (UK shopping) instead of Russia Today (World News).
Added line: nc.client = { onBWDone: function():void{} }; (since Flash Player is now more strict. Before it worked fine without this line).
Perhaps a more complete version of the code is like this. it should play RT channel live.
package {
import flash.events.NetStatusEvent;
import flash.events.AsyncErrorEvent;
import flash.display.MovieClip;
import flash.net.NetStream;
import flash.net.NetConnection;
import flash.media.Video;
import flash.utils.setInterval;
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 var intervalID:uint;
public var counter:int;
public function RTMP_test ()
{ init_RTMP(); }
function init_RTMP():void
{
streamID = "RT_2";
videoURL = "rtmp://fms5.visionip.tv/live/RT_2";
vid = new Video(); //typo! was "vid = new video();"
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
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 _aspectH:int;
var _videoW:int;
var _videoH:int;
var relationship:Number;
relationship = data.height / data.width;
//Aspect ratio calculated here..
_videoW = _stageW;
_videoH = _videoW * relationship;
_aspectH = (_stageH - _videoH) / 2;
vid.x = 0;
vid.y = _aspectH;
vid.width = _videoW;
vid.height = _videoH;
}
}
}
I am having an Error on the loop function can anyone tell me what i am doing wrong?
I think my syntax is good?
Severity and Description Path Resource Location Creation Time Id
1046: Type was not found or was not a compile-time constant: Event. ar/src ar.as line 94 1318225764229 228
package {
import flash.display.Sprite;
import org.papervision3d.objects.primitives.Cube;
[SWF(width="640", height="480", framerate="30", backgroundColor="#ffffff")]
public class ar extends Sprite
{
// Embed the marker.pat file
[Embed(source="marker.pat", mimeType="application/octet-stream")]
private var marker:Class;
// Embed the camera.pat file
[Embed(source="camera_para.dat", mimeType="application/octet-stream")]
private var cam_params:Class;
// createFLAR Vars
private var ar_params:FLARParam;
private var ar_marker:FLARCode;
// createCAM Vars
private var ar_vid:Video;
private var ar_cam:Camera;
// createBMP Vars
private var ar_bmp:BitmapData;
private var ar_raster:FLARRgbRaster_BitmapData;
private var ar_detection:FLARSingleMarkerDetector;
// createPapervision Vars
private var ar_scene:Scene3D;
private var ar_3dcam:FLARCamera3D;
private var ar_basenode:FLARBaseNode;
private var ar_viewport:Viewport3D;
private var ar_renderengine:BasicRenderEngine;
private var ar_transmat:FLARTransMatResult;
private var ar_cube:Cube;
public function ARProj()
{
createFLAR();
createCAM();
createBMP();
createPapervision();
addEventListener(Event.ENTER_FRAME, loop);
}
public function createFLAR()
{
ar_params = new FLARParam();
ar_marker = new FLARCode();
ar_params.loadParam(new cam_params() as ByteArray);
ar_marker.loadARPatt(new marker());
}
public function createCAM()
{
ar_vid = new Video(640, 480);
ar_cam = Camera.getCamera();
ar_cam.setMode(640,480,30);
ar_vid.attachCamera(ar_cam);
addChild(ar_vid);
}
public function createBMP()
{
ar_bmp = new BitmapData(640,480);
ar_bmp.draw(ar_vid);
ar_raster = new FLARRgbRaster_BitmapData(ar_bmp);
ar_raster = new FLARSingleMarkerDetector(ar_params, ar_marker, 80);
}
public function createPapervision()
{
ar_scene = new Scene3D();
ar_3dcam = new FLARCamera3D(ar_params);
ar_basenode = new FLARBaseNode();
ar_renderengine = new BasicRenderEngine();
ar_transmat = new FLARTransMatResult();
ar_viewport = new Viewport3D();
var ar_light:PointLight3D = new PointLight3D();
ar_light.x = 1000;
ar_light.y = 1000;
ar_light.z = -1000;
var ar_bitmap:BitmapMaterial;
ar_bitmap = new BitmapFileMaterial("image.jpeg");
ar_bitmap.doubleSided = true;
ar_cube = new Cube(new MaterialsList({all:ar_bitmap}), 80, 80, 80);
ar_scene.addChild(ar_baseNnode);
ar_basenode.addChild(ar_cube);
addChild(ar_viewport);
}
public function loop(e:Event):void
{
ar_bmp.draw(ar_vid);
ar_cube.rotationX +=4;
ar_cube.rotationY +=6;
try
{
if (ar_detection.detectMarkerLite(ar_raster, 80) && ar_detection.getConfidence() > 0)
{
ar_detection.getTransformMatrix(ar_transmat);
ar_basenode.setTransformMatrix(ar_transmat);
ar_renderengine.renderScene(ar_scene, ar_3dcam, ar_viewport);
}
}
catch(e:Error){
}
}
}
}
import flash.events.Event;
You have not imported Event class. Add this import.
You're missing the import for the event Class (and probably more).