Playing mp4 video in AS3. NetStream was unable to invoke callback onMetaData - actionscript-3

I'm trying to play a video in flash player using following code.
package {
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.text.TextField;
import flash.media.Video;
public class Main extends Sprite {
public var MyNC:NetConnection = new NetConnection();
public var MyNS:NetStream;
public var MyVideo:Video = new Video();
public function Main() {
MyVideo = new Video();
addChild(MyVideo);
MyNC = new NetConnection();
MyNC.connect(null);
MyNS = new NetStream(MyNC);
MyVideo.attachNetStream(MyNS);
MyNS.play("video.mp4");
}
}
}
It works but shows folowing error:
Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.NetStream was unable to invoke callback onMetaData.
error=ReferenceError: Error #1069: Property onMetaData not found on flash.net.NetStream and there is no default value.
at Main()
Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.NetStream was unable to invoke callback onXMPData.
error=ReferenceError: Error #1069: Property onXMPData not found on flash.net.NetStream and there is no default value.
at Main()

I got this error removed by setting value for MyNS.client
listener.onMetaData = function(md:Object):void {};
MyNS.client = listener;

Related

Error #1009: Cannot access a property or method of a null object ref AS3 project

I have AS3 project and Im trying to create a way to display GIF or SWF (converted online from the gif) in a similar way that I display a single image.
A single image is displayed with class and then referenced in Main.as. Similarly for gif or swf I extend MovieClip:
(yes I read other threads did not help)
package com.mee.mytest
{
import flash.display.Bitmap;
import flash.events.Event;
import flash.display.MovieClip;
/**
* ...
* #author Mee
*/
public class MyTest extends MovieClip
{
[Embed(source="../../../../assets/spfx_MyClip.swf", mimeType="application/octet-stream")]
private static const cMyTest : Class;
private var swfMyClip : MovieClip;
public function MyTest()
{
swfMyClip = new cMyTest() as MovieClip;
swfMyClip.scaleX = 600;
swfMyClip.scaleY = 400;
addChild(swfMyClip);
}
}
}
And now my main:
import com.mee.mytest.MyTest
import flash.desktop.NativeApplication;
import flash.display.Bitmap;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public var vMyTest : MyTest; //this is var = the class
public function Main()
{
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.DEACTIVATE, deactivate);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.setAspectRatio(StageAspectRatio.LANDSCAPE);
// touch or gesture? BLAH BLAH
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
testMyGif();
}
function testmyGif():void
{
vMyTest = new MyTest();
addChild(vMyTest);
}
ERROR ERROR ERROR
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.mee.mytest::MyTest()
at Main/testmyGif()
at Main()
dem ... just for any1's sake. It mostly comes from 'Your displayed SWF is supposed to be in the same folder as the built swf, then the url has new URLRequest("myfile.swf") forget other folders or ..exiting folders, this is the only way to avoid any other URL errors
using
public var loader : Loader = new Loader();
public var urlRequ : URLRequest = new URLRequest("MyFile.swf")
loader.load(urlRequ);
addChild(loader);

AddEventListener from Package in AIR Error #1067?

I'm publishing a Flash game I'm making in AIR 2.6 rather than Flash Player, since that's the only way I can get it to work with TCP. For some reason though, doing this will not allow me to import packages, seemingly...
If I have it published through Flash Player, this code works fine:
import TCP;
addEventListener(Event.ENTER_FRAME, TCP);
When I do the same exact thing but publish with AIR 2.6, I get this error:
1067: Implicit coercion of a value of type Class to an unrelated type Function.
This is the entire package:
package
{
import flash.display.Sprite;
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.events.*;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.ServerSocketConnectEvent;
import flash.net.ServerSocket;
import flash.net.Socket;
public class TCP extends Sprite
{
private var serverSocket:ServerSocket;
private var clientSockets:Array = new Array();
public function TCP()
{
try
{
// Create the server socket
serverSocket = new ServerSocket();
// Add the event listener
serverSocket.addEventListener( Event.CONNECT, connectHandler );
serverSocket.addEventListener( Event.CLOSE, onClose );
// Bind to local port 8087
serverSocket.bind( 8087, "127.0.0.1" );
// Listen for connections
serverSocket.listen();
trace( "Listening on " + serverSocket.localPort );
}
catch(e:SecurityError)
{
trace(e);
}
}
public function connectHandler(event:ServerSocketConnectEvent):void
{
//Thesocket is provided by the event object
var socket:Socket = event.socket as Socket;
clientSockets.push( socket );
socket.addEventListener( ProgressEvent.SOCKET_DATA, socketDataHandler);
socket.addEventListener( Event.CLOSE, onClientClose );
socket.addEventListener( IOErrorEvent.IO_ERROR, onIOError );
//Send a connect message
socket.writeUTFBytes("Connected.");
socket.flush();
trace( "Sending connect message" );
}
public function socketDataHandler(event:ProgressEvent):void
{
var socket:Socket = event.target as Socket
//Read the message from the socket
var message:String = socket.readUTFBytes( socket.bytesAvailable );
trace( "Received: " + message);
// Echo the received message back to the sender
message = "Echo -- " + message;
socket.writeUTFBytes( message );
socket.flush();
trace( "Sending: " + message );
}
private function onClientClose( event:Event ):void
{
trace( "Connection to client closed." );
//Should also remove from clientSockets array...
}
private function onIOError( errorEvent:IOErrorEvent ):void
{
trace( "IOError: " + errorEvent.text );
}
private function onClose( event:Event ):void
{
trace( "Server socket closed by OS." );
}
}}
So what do I have to do here? I believe the package is "importing", but when I use addEventListener I get that error thrown at me. Again, this only happens when I publish with AIR. What do I do?
There should be no reason to add an enterframe listener. It looks like the TCP class should function is is...
var tcp:TCP = new TCP();
Should instantiate the class. Its constructor will then try to make a socket connection.
Error is correct, you should add event listener like:
addEventListener(Event.ENTER_FRAME, onEnterFrameFunction);
Then in onEnterFrameFunction, you can use the TCP class:
var tcp:TCP = new TCP();
I am surprised that works in flash. Maybe you are not describing things fully.
In the code below you are trying to add an eventlistener with a Class as the function to be called when the event occurs.
import TCP;
addEventListener(Event.ENTER_FRAME, TCP);
So the error is correct:
1067: Implicit coercion of a value of type Class to an unrelated type Function.
It is not clear from your question what you are trying to do with the event listener.

Error #2044: Unhandled NetStatusEvent

I'm not sure why i'm getting the following error. Can you please suggest what change in my code needed to successfully play the rtmp stream on my stage?
error:
NetConnection.Connect.Success Error #2044: Unhandled NetStatusEvent:.
level=error, code=NetStream.Play.StreamNotFound at
main/attachnetstream()[/Users/user/Desktop/ojotha/main.as:24] Debug
session terminated.
package {
import flash.display.MovieClip;
import flash.net.*;
import flash.events.NetStatusEvent;
import flash.media.Video;
public class main extends MovieClip {
public var streamserver:String="rtmp://216.245.200.114/live";
public var streamname:String="shomoy";
public var netconnection:NetConnection=new NetConnection();
public function main() {
netconnection.connect(streamserver);
netconnection.addEventListener(NetStatusEvent.NET_STATUS, attachnetstream);
}
public function attachnetstream(event:NetStatusEvent):void{
trace(event.info.code);
switch (event.info.code) {
case "NetConnection.Connect.Success":
var netstream:NetStream=new NetStream(netconnection);
var video:Video=new Video();
video.attachNetStream(netstream);
netstream.play(streamname);
video.height=480;
video.width=640;
addChild(video);
break;
case "NetStream.Play.StreamNotFound":
trace("stream not found");
break;
}
}
}
}
You should add NetStatusEvent event listener to your stream as well.
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
Look at the docs
You may also need to set another event listener, even if it is empty to prevent some other runtime errors.
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

as3 how to use a URLLoader in a class

Is it possible to use a URLLoader in a class?
I am trying to use it in a class. Code is taken from: http://www.republicofcode.com/tutorials/flash/as3externaltext/
And here is my class I am trying to use it in but I get this error:
C:\Users\com\defaultVars.as, Line 36 1046: Type was not found or was not a compile-time constant: Event.**
package com {
import flash.display.Stage;
import flash.*;
public class defaultVars
{
// loader
public var myTextLoader:URLLoader = new URLLoader();
public function defaultVars()
{
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
myTextLoader.load(new URLRequest("myText.txt"));
}
public function sampleFunction()
{
// trace("not used");
}
/// ERROR LINE right below. But even if I fix this I get more errors.
function onLoaded(e:Event):void {
trace(e.target.data);
}
////////////////////////
}
}
try adding this to the top of your file.
import flash.events.*
import flash.net.*;

What is the null-object error with this AS3 code for loading external swf?

I am getting a null object error when I add the mouse event listener for the log in button. (Look at the comments in the constructor)
I am using Flash CS6, and objects such as logInbutton and screen_log_in are instance names from the .fla file. This here is the .as file.
Error I get is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at actions::indexPage()
My AS3 code:
package actions
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IEventDispatcher;
import flash.net.URLRequest;
import flash.display.Loader;
import fl.motion.MotionEvent;
import flash.events.MouseEvent;
public class indexPage extends MovieClip
{
public function indexPage():void
{
loadSWF("http://mathlympics.cu.cc/loginsystem.swf");
//THIS IS THE LINE WHICH IS CAUSING THE ERROR
//WHEN I COMMENT IT OUT THE ERROR IS GONE
logInButton.addEventListener(MouseEvent.CLICK, goToLogIn);
}
var _swfLoader:Loader;
var _swfContent:MovieClip;
public function loadSWF(path:String):void
{
var _req:URLRequest = new URLRequest();
_req.url = path;
_swfLoader = new Loader();
setupListeners(_swfLoader.contentLoaderInfo);
_swfLoader.load(_req);
}
function setupListeners(dispatcher:IEventDispatcher):void
{
dispatcher.addEventListener(Event.COMPLETE, addSWF);
}
function addSWF(event:Event):void
{
event.target.removeEventListener(Event.COMPLETE, addSWF);
event.target.removeEventListener(ProgressEvent.PROGRESS, preloadSWF);
_swfContent = event.target.content;
screen_log_in.addChild(_swfContent);
}
function unloadSwf():void
{
_swfLoader.unloadAndStop();
screen_log_in.removeChild(_swfContent);
_swfContent = null;
}
function goToLogIn(e:MouseEvent):void
{
unloadSwf();
screen_log_in.loadSWF("http://mathlympics.cu.cc/loginsystem.swf");
}
function goToRegister(e:MouseEvent):void
{
unloadSwf();
screen_log_in.loadSWF("http://mathlympics.cu.cc/register.swf");
}
}
}
You can not access stage until stage is available.
public function indexPage():void
{
addEventListener(Event.ADDED_TO_STAGE,init)
}
public function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init)
loadSWF("http://mathlympics.cu.cc/loginsystem.swf");
//THIS IS THE LINE WHICH IS CAUSING THE ERROR
//WHEN I COMMENT IT OUT THE ERROR IS GONE
logInButton.addEventListener(MouseEvent.CLICK, goToLogIn);
}
I am just going to answer my question for the future visitors. The cause of problem I have note been able to figure out but what I have figured out is how to get around it. I simply copy pasted my code from document class to the frames and it works absolutely fine there.