Video Playback

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Adding a Video to Your Project

XNA/MonoGame is expecting movies in the .wmv file format, you will therefore need to convert the video to this format if required. There are many online methods to do this, I have used KeepVid to download the MP4 file from a youtube video, there is also CloudConvert.

Our first step is to get ahold of a video that we want to play, add it to your projects content folder. You will need to add the video to the content pipeline and build the pipeline.

Loading the Video in MonoGame

Video will require an additional using statement to be added:

using Microsoft.Xna.Framework.Media;

To play the video, we'll need two variables. At the top of your main game class (Game1.cs) add the following:

Video video;
VideoPlayer player;

The Video class is a representation of your video that you want to see, while the VideoPlayer is the XNA/MonoGame class that will handle all of the playing for you.

To load the video, in the LoadContent method add the following two lines of code:

video = Content.Load<Video>("AVideoToPlayback");
player = new VideoPlayer();

Starting the Video

Now that we have a video to play, we'll need to start playing it in order to see anything. Doing this only requires a single line of code:


You could easily add this in the LoadContent method and be done with it, but many people prefer to put this in their Update method, in which case we'll need to wrap it with a check to see if the video player is already running our video, like below:

if (player.State == MediaState.Stopped)

You can also use the code to check to see when a video is done. Once the video is done, the player's State field will start returning MediaPlayer.Stopped, so, for instance, you can check for this, and if needed, go to a different scene, or load the next level or something.

Drawing the Video

To draw the video, we'll perform two steps. Go down to the Draw method and add the following code:

Texture2D videoTexture = null;
if (player.State != MediaState.Stopped)
    videoTexture = player.GetTexture();

This code gets a texture that represents the current frame in the video. Here, we've created a local variable to store it (videoTexture) and if the media player hasn't stopped, we get the current texture from it.

Now we can just draw the texture to the screen using a SpriteBatch, like we have done before. The following code will take the texture that we just got from the video, and draw it to a rectangle on the screen, so add it to your Draw method just after the code we just added.

if (videoTexture != null)
    spriteBatch.Draw(videoTexture, new Rectangle(0, 0, 400, 240), Color.White);

At this point, you should be able to run your game and see your video playing. Note that sometimes, when you compile your game content, XNA Game Studio gives an error message that looks like this:

Video file <filename> is invalid. Please make sure that the video is not DRM protected and is a valid single-pass CBR encoded video file.

If you see this message, then most likely, your video is in the wrong format. You will probably need to go back and play around with some of the settings in your video creation tool to get this to work.