A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Actioncript 3 video player - troubleshoot :)

  1. #1
    Member
    Join Date
    Mar 2004
    Posts
    51

    Actioncript 3 video player - troubleshoot :)

    Hey all -

    I'm working on adding a small preview video player to a flash project I'm creating. I've successfully created an actionscript 3-based video player before, so my code is pretty much adapted from that. However, though the code executes successfully (seemingly), I can't see the video anywhere. Can someone take a look and see if you find anything egregious?

    Code:
    		private function buildVidPreview(page:Array):void {
    			vidPreviewa.x=stage.mouseX;
    			Tweener.removeTweens(vidPreviewa);
    			Tweener.addTween(vidPreviewa, {alpha:1, time:1});
    			setupPreviewVid(page);
    			}	
    
    		private function nsMetaDataCallback(mData:Object):void {
    			videoHeight=mData.height;
    			videoWidth=mData.width;
    			videoLength=mData.duration;
    			trace("metaData loaded, height="+videoHeight+", width="+videoWidth);
    			previewVideoStage=new Video(videoWidth,videoHeight);
    			previewVideoStage.name="Player";
    			trace("Creating a "+videoWidth+"x"+videoHeight+" player for "+pageClicked[4]);
    			var currentVideo:NetStream = new NetStream(previewFLVConnection);
    			previewVideoStage.attachNetStream(currentVideo);
    			//previewVideoStage.name=page[4];
    			vidPreviewa.addChild(previewVideoStage);
    			trace(vidPreviewa.getChildAt(1).name+" x: "+vidPreviewa.getChildAt(1).x+", y: "+vidPreviewa.getChildAt(1).y+", alpha: "+vidPreviewa.getChildAt(1).alpha)
    		}
    		private function setupPreviewVid(page:Array):void {
    			setupANetStream(previewFLVConnection,previewVideo,page);
    		}
    
    		private function setupANetStream(nc:NetConnection, ns:NetStream, page:Array):void {
    			var client:Object = new Object();
    			trace("Starting a Net Stream for "+page[4]);
    			nc.connect(null);
    			ns=new NetStream(nc);
    			ns.client=new Object;
    			ns.client=client;
    			client.onMetaData=nsMetaDataCallback;
    			trace("Playing "+page[3]);
    			ns.play(page[3]);
    		}

  2. #2
    Junior Member
    Join Date
    Jun 2009
    Location
    London
    Posts
    12
    It's a bit tricky as we can't see the whole class, and your code isn't the clearest... (sorry!)

    Problems I've found in the past:
    • Metadata width and height come through null (but it looks like you're covered that)
    • Your connection hasn't yet connected and you need to add an event handler (but normally this errors out)
    • You forgot to add an object somewhere in the chain to the stage
    • Your video is blank


    Can you hear the audio? I'd add some event listeners to the stream and see what comes up.

  3. #3
    Member
    Join Date
    Mar 2004
    Posts
    51
    Those are great ideas - I'll start checking them out now. Thanks for your help!

  4. #4
    Member
    Join Date
    Mar 2004
    Posts
    51
    OK, So i've started from scratch and written one just like the examples in flash help. unfortunately it's throwing an error that doesn't even reference a line of code (I friggin hate those). Here's my code - hopefully easier to parse this time

    Code:
    		private function nsMetaDataCallback(event:AsyncErrorEvent):void {
    			// do nothing
    		}
    		private function setupPreviewVid(page:Array):void {
    			setupANetStream(previewFLVConnection,previewVideo);
    			previewVideoBuilt=true;
    			previewVideo.play(page[3]);
    			previewVideoStage.attachNetStream(previewVideo);
    			vidPreviewa.addChild(previewVideoStage);
    		}
    		private function setupANetStream(nc:NetConnection, ns:NetStream):void {
    			var client:Object = new Object();
    			nc.connect(null);
    			ns=new NetStream(nc);
    			ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, nsMetaDataCallback);
    		}
    Basically setupPreviewVid is the function that's run, and it's given the location of the video file to play.

    I imagine it's giving me an error because it's not waiting for the SetupANetStream function to finish before it goes to the next line of code. Is there any way to force it to go line by line without running things concurrently?

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Location
    London
    Posts
    12
    Any reason you're passing in a netstream reference then creating a new one?

    And your client object needs a metadata handler. At the moment it's an empty object.
    You're forgetting to associate it with the netstream too.
    Code:
    ns.client = {onMetaData:function(info:Object){}}
    I'd get rid of all your page[x] variables as well, as well as your long variable names and just concentrate on getting it working.

    The code needed is not much in the end, it's just doing the right things in the right order!

    This is the basics (I bashed this out in 5 minutes, no looking at the manual)

    Code:
    // functions
    	function metaDataHandler(data:Object):void
    	{
    		for(var p:String in data)
    		{
    			trace(p + ' : ' + data[p]);
    		}
    	}
    
    // connection
    	var connection:NetConnection = new NetConnection();
    	connection.connect(null);
    	
    // stream
    	var stream:NetStream = new NetStream(connection);
    	stream.client = {onMetaData:metaDataHandler}
    	
    // video
    	var video:Video = new Video();
    	video.attachNetStream(stream);
    	addChild(video);
    	
    // play
    	var url:String = 'http://www.youtube.com/get_video?video_id=bNSIymy_Goc&t=vjVQa1PpcFOzRyjM4atj0FSJIFABVgqboCXfk0HUjKQ=&el=detailpage&ps=&fmt=5';
    	stream.play(url);
    Don't over-complicate things!


  6. #6
    Member
    Join Date
    Mar 2004
    Posts
    51
    Well, the problem is, that's all great if I've just got one video, but I'm going to want to switch the video in the same spot when a new rollover happens. I'll also have another larger video player that will only be on if a user clicks on something.

    Basically, I've been taught to create functions out of commonly used things so that my code doesn't get ungainly. Does flash not work well this way? Every time I ask for clarification on how to build a function that does X, I get told to forget it and just write the code out long-hand.

  7. #7
    Junior Member
    Join Date
    Jun 2009
    Location
    London
    Posts
    12
    Here's an example using functions, and an extra check for the connection (I'm unsure as to whether the connection needs to be already connected or not before setting up the stream - I suspect it does)

    Code:
    // variables
    	var url:String;
    	var video:Video;
    	var connection:NetConnection;
    	var stream:NetStream;
    
    // setup
    	function setup():void
    	{
    		// url
    			url = 'http://www.youtube.com/get_video?video_id=bNSIymy_Goc&t=vjVQa1PpcFOzRyjM4atj0FSJIFABVgqboCXfk0HUjKQ=&el=detailpage&ps=&fmt=5';
    
    		// video
    			video = new Video();
    			addChild(video);
    	}
    
    // connect
    	function connect():void
    	{
    		// connection
    			connection = new NetConnection();
    			connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
    			connection.connect(null);
    	}
    	
    	function netStatusHandler(event:NetStatusEvent):void
    	{
                switch (event.info.code) {
                    case "NetConnection.Connect.Success":
                        view();
                        break;
                    case "NetStream.Play.StreamNotFound":
                        trace("Unable to locate video: " + url);
                        break;
                }
    	}
    	
    // view
    	function metaDataHandler(data:Object):void
    	{
    		for(var p:String in data)
    		{
    			trace(p + ' : ' + data[p]);
    		}
    	}
    
    
    	function view():void
    	{
    		// stream
    			stream = new NetStream(connection);
    			stream.client = {onMetaData:metaDataHandler};
    			
    		// video
    			video.attachNetStream(stream);
    			
    		// play
    			stream.play(url);
    	}
    
    // code
    	setup();
    	connect();

  8. #8
    Member
    Join Date
    Mar 2004
    Posts
    51
    thanks for that example! I'll see if i can't get it working.

  9. #9
    Member
    Join Date
    Mar 2004
    Posts
    51
    Awesome! I was able to use your framework to make it work.

    You should write the flash documentation! None of those event handlers are in the "using flash" documentation.

  10. #10
    Junior Member
    Join Date
    Jun 2009
    Location
    London
    Posts
    12
    Cool

    You could probably abstract the view function into 2 functions, one that attaches the stream, and one that plays it. Then the connection success call would go call connectComplete() and then view(), and obviously you can then call view whenever you like after having updated the url variable. This way you don't connect dozens of streams, you just tell one stream to play a new URL.

    I'm sure you can work it out anyway. Welcome to Flash video (and all its foibles!)

  11. #11
    Junior Member
    Join Date
    Jun 2009
    Location
    London
    Posts
    12
    Oh, and your best bet for this sort of thing is to put the minimum amount of code you can into this class. Let it just be a self-contained video connecting / playing class.

    Really, it should just be a black box which you instantiate - it shouldn't have any knowledge of your "page" array, it only needs to know what URL it's currently playing (or attempting to play), and will update as and when it's told.

    As well, leave the commands for tweening and all that other jazz to a parent class. Just let this class be responsible for a single video without any distractions! If you extend it from Sprite, it will tween quite happily when told to...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center