Vladimir Shevyakov
Vladimir Shevyakov

Reputation: 2821

Playing a video with flex, AS3

I am currently trying to make a game on flex and one of the problems I ran in to is how to play a short animation at the beginning. This is what I have so far:

Game.mxml

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    name="Game"
    backgroundColor="#000000"
    horizontalAlign="center"
    creationComplete="Init();"
    enterFrame="UpdateFrame();"
    paddingLeft="0"
    paddingTop="0"
    paddingBottom="0"
    paddingRight="0"
    width="800" height="600">

  <mx:Script>
  <![CDATA[
    include "Game.as";
  ]]>
  </mx:Script>

  <mx:Canvas id="gamePanel" x="0" y="0" width="100%" height="100%" mouseDown="MouseDown(event)" mouseUp="MouseUp(event)" mouseMove="MouseMoved(event)"/>
</mx:Application>

and Game.as

import flash.display.*; 
import flash.events.*;
import flash.external.ExternalInterface; 
import mx.events.*;
import mx.controls.*;

[Embed(source="MyVideoClip.flv")] private var MyVideoClip:Class;

public function Init():void
{   
    var MyVideo:Video = new Video(800, 600);
    addChild(MyVideo);
    var qNetConnection:NetConnection = new NetConnection();
    qNetConnection.connect(null);
    var qNetStream:NetStream = new NetStream(qNetConnection);
    MyVideo.attachNetStream(qNetStream);
    qNetStream.client = new Object();
    qNetStream.play(MyVideoClip);
}

private function UpdateFrame():void
{

}

private function MouseDown(event:MouseEvent):void   
{   

}   

private function MouseUp(event:MouseEvent):void     
{

}

private function MouseMoved(event:MouseEvent):void  
{

}

I am rather new to Flex and AS3 so most of this code was ripped off web tutorials. Whenever I try to compile it I get: 'Error: 'MyVideoClip.flv' does no have a recongnized extention, and a mimeType was not provided. Error: unable to transcode 'MyVideoClip.flv''

If I remove the 'embed' line and replace MyVideoClip with "MyVideoClip.flv" in the play() function, the code compiles with no errors, but when I open the SWF all I get is a black screen. What am I doing terribly wrong?

Thanks in advance!!

Upvotes: 2

Views: 111

Answers (2)

VC.One
VC.One

Reputation: 15871

You embedded the video file (its bytes) into the output SWF. So now NetStream must play from a bytes source. Just set a byteArray as equal to new MyVideoClip(); and append to NetStream.

Try this...

[Embed(source="MyVideoClip.flv", mimeType="application/octet-stream")] private var MyVideoClip:Class; //# embed the FLV's bytes

public var VideoClipBytes : ByteArray;
public var MyVideo:Video;

public var qNetConnection:NetConnection;
public var qNetStream:NetStream;

public function Init():void
{   
    VideoClipBytes = new MyVideoClip() as ByteArray; //# fill a byte Array with embedded bytes 

    qNetConnection = new NetConnection(); qNetConnection.connect(null);
    qNetStream = new NetStream(qNetConnection);
    qNetStream.client = new Object();

    MyVideo = new Video(800, 600);
    MyVideo.attachNetStream(qNetStream);
    addChild(MyVideo);

    qNetStream.play(null); //# play mode is now bytes
    qNetStream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN); //# ready for new audio/video data        
    qNetStream.appendBytes( VideoClipBytes ); //# add bytes to decoder queue (playback)

}

PS : I made some of your variables as public so later you can access & control them from any other functions. Remember if you make var inside a public function it stays valid only in that one function and doesn't exist to other functions. Best make such vars as publicly available to all functions.

Upvotes: 2

Robert Lombardo
Robert Lombardo

Reputation: 152

Try setting the mime-type, e.g.:

[Embed(source = "MyVideoClip.flv", mimeType = "application/octet-stream")]

Upvotes: 2

Related Questions