With 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.

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:
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!