Home > Blog > Video Support in XNA Game Studio 3.1

Video Support in XNA Game Studio 3.1

April 17th, 2009 Michael Klucher

VideoIconWith XNA Game Studio 3.1 ever approaching I wanted to talk about one of my favorite features in the new release, video support! While we haven’t gone into too much detail on exactly how things will work here’s what you need to know.

What will I be able to do with video in XNA Game Studio 3.1?

  • Take video processed through XNA Game Studio and play it back full screen, this can be used for things like cut scenes, animated logos and intros.
  • Take video processed through XNA Game Studio and apply it as an animated texture, even on 3D geometry.

What won’t I be able to do with video in XNA Game Studio 3.1?
Even with those great features, we may not support all the scenarios you can think of yet. In XNA Game Studio 3.1 you will not be able to:

  • Initiate playback of Video files already on the Xbox 360 (All contest must be in your game’s container, as you cannot enumerate the video on a user’s console).
  • Stream video from a computer, or Media Center.
  • There is no option to seek through video for playing back small sections in a larger video. You can pause and resume however.
  • Video playback support only works on the Xbox 360 and Windows platforms.

Importing Video
To import video, your video must first meet a couple of requirements, which I’ve listed below.

  • The format must be Windows Media Video, Series 9 using the “Main” profile. This profile defines several sub formats. You can grab the encoder for Windows Media Video at this link.
  • The maximum video resolution supported is 1280×720
  • The video and audio streams must be encoded at a constant bit rate aka (CBR)
  • Only unprotected, non-DRM content is supported.

If you’ve got video that meets that criteria your ready to go, if not use the encoder link and re-encode the video so it’s in a compatible format. Once you have your video file you add it just like you would any other XNA Game Studio Content.
image

Rendering Video
Now that video has been imported into your project, we’re ready to play it back! First thing we need to do is create the types to hold the video data and the class used for playing back the video:

We have two objects, Video (used to hold the video data) and the VideoPlayer used to control the playback of the video in our game. You can see that we’ve created the VideoPlayer inside the constructor for the game.

GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// Create the variables needed to hold and play back my video.
Video myVideoFile;
VideoPlayer videoPlayer;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Create the VideoPlayer
videoPlayer = new VideoPlayer();
}


Next we need to load the video for playback, this is simple enough in that it’s loaded just like any other XNA Game Studio content from the XNA Framework Content Pipeline:

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
// Load in my video file (just like any other content!)
myVideoFile = Content.Load<Video>(@"Bear");
}

Now that we have the video loaded we’ll want to start playing back the video, in this particular case I’ve wired up playing the video to pressing the “A” button on the Xbox 360 controller pressing “B” stops the video. You’ll see that the playback is controlled through the VideoPlayer object in the code below. Note that I don’t need to worry about calling Play/Stop if the video is already in that state as it won’t harm anything.

protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
// Start the video playback when the user presses A
if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
{
videoPlayer.Play(myVideoFile);
}
// Start the video playback if the user presses B
if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
{
videoPlayer.Stop();
}
base.Update(gameTime);
}

Finally let’s draw our video to the screen. In this case I’m simply using SpriteBatch to draw the image. As you can see we’re just getting the texture data using VideoPlayer.GetTexture() and displaying it to the screen. In the example below I’m just playing back the video in its native resolution, I could stretch the video and/or play it back full screen by using the GraphicsDeviceManager.GraphicsDevice.Viewport.Width and GraphicsDeviceManager.GraphicsDevice.Viewport.Height properties.

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
// Render the video in it's orginal resolution to the screen using SpriteBatch
spriteBatch.Begin();
if (videoPlayer.State == MediaState.Playing)
{
spriteBatch.Draw(videoPlayer.GetTexture(), new Rectangle(0, 0, myVideoFile.Width, myVideoFile.Height), Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}

The audio playback from the video is handled automatically, and can be muted by setting the IsMuted property on the VideoPlayer object. You can also turn on things like looping (through VideoPlayer.IsLooping property) so the video plays back continuously, but keep in mind that there is a slight hitch when the video restarts.

That’s it! If you’ve added the code to a new XNA Game Studio for Windows or Xbox 360 project you should see something like this when you start the video:

image 

Bonus Round: tips & tricks to keep in mind

As you implement video in your game here are a couple of things to keep in mind, think of this like a “best practices” for video support.

  • Calling VideoPlayer.GetTexture() will return the latest decoded frame in time. This means that every time it’s called you have the potential of getting a new image. If you have a game where you want to display more than one copy of the video on the screen, make sure that your only calling VideoPlayer.GetTexture() once, then holding onto that data when using it multiple times. This will ensure that your video stays perfectly synchronized while drawing your frame.
  • Because the video data is treated like Texture2D data, you have the ability to use effects on it when you render it on screen!
  • If you’re mixing music playback and video playback, you’ll want to have a look at the VideoSoundtrackType class. This will allow you to easily play back music while muting the music from your video if needed.
  • Higher resolution or bit rate video means you’re incurring a higher CPU cost to decode it. Don’t use ultra high resolution video for something that’s not playing back full screen. Make the appropriate decision of how it will be seen by the player.

Wrapping Up!

I hope this gives you a good idea how to use the upcoming video functionality in XNA Game Studio 3.1, again this is one of my favorite new features and am looking forward to seeing some great video functionality in your games soon!

  • Share/Bookmark
Categories: Blog Tags:

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

  1. April 18th, 2009 at 03:30 | #1

    Awesome! What size (roughly) are we looking at for the movie files once they’ve went through the content pipeline?

  2. April 18th, 2009 at 13:51 | #2

    Hi Harald, This is a great question, sorry I didn’t cover this in my blog post! Video is a pretty unique case in that the video will stay as a WMV file (which should stay the same size) when it’s moved through the XNA Framework Content Pipeline. Additionally to keeping the WMV file we generate a very small XNB file (on the order of a few hundred bytes) that goes with the video content.

    Hope this answers your question!

    Thanks

    Michael

  3. Nate
    April 19th, 2009 at 21:16 | #3

    Awesome, this could really be useful for things like tutorial explainations, cut scenes, and since it can be applied to 3D textures I guess it could be used for level decoration (that made me think of Duke Nukem 3D, the level with the movie theater…not to mention all the security camera montors showing random stuff in the background).

  4. April 22nd, 2009 at 13:24 | #4

    RE: Video Support in XNA Game Studio 3.1

    My colleague Michael Klucher posted a great introduction to the new video playback feature that is coming

  5. April 23rd, 2009 at 19:21 | #5

    RE: Video Support in XNA Game Studio 3.1

    As mentioned on the XNA team blog and this XNA Creators Club forum post , the XNA team made a few announcements

  6. Sonik
    April 24th, 2009 at 12:23 | #6

    Any plan on implementing a seek method in it?

    It will decode even if we dont call GetTexture() ?

    And it will decode "faster" if we call GetTexture() more than once per frame?

    Thanks in advance.

  7. mike
    June 3rd, 2009 at 20:13 | #7

    will xna 3.1 have mjpeg streaming support? I’m guessing not…but that would open a lot of new type of apps. Try and put the support in! Put some support for h.264 video as well.

  8. June 11th, 2009 at 12:21 | #8

    RE: Video Support in XNA Game Studio 3.1

    As announced today in this news item on the Creators Club site , XNA Game Studio 3.1 is now available

  9. Charles
    June 19th, 2009 at 07:23 | #9

    Hi! Thanks a lot for the tutorial as it is quite easy to follow, I have a question, when I import the file and I click on proprieties in the content importer and processor it won’t appear the options you ahve put. I ‘m using xna game studio 3.1 but still I cannot to get it working.

    thank you for your time

    charles

  10. Charles
    June 19th, 2009 at 08:59 | #10

    Hi its me again I sorted out but this error "Please make sure that the video is not DRM protected and is a valid single-pass CBR encoded video file2 I went to the XNa community an apparentelly I’m not the only one having this kind of problem :P Thanks for the tutorial tough.

  11. June 20th, 2009 at 11:26 | #11

    Sonik, Yes the video continues to move even if you aren’t calling GetTexture(). This way to you don’t have to worry about frame rate differences between your Draw()/Update() and Video.

    Charles, Did you post about this on our forums? Shoot me a link and I will try and help you over there.

    Thanks

    Michael

  12. July 2nd, 2009 at 02:32 | #12

    RE: Video Support in XNA Game Studio 3.1

    Недавно была выпущена новая версия XNA Game Studio 3.1. Среди остальных прикольных нововведений (таких

  13. December 28th, 2009 at 20:31 | #13

    Looks great man! I think video will be a major step in XNA overall :D

    I’ll get the newset version of XNA ;)

Comments are closed.