Video Playback

From TRCCompSci - AQA Computer Science
Revision as of 11:06, 30 October 2017 by Admin (talk | contribs) (Created page with "=Adding a Video to Your Project= Our first step is to get ahold of a video that we want to play, add it to your project, and load it in the typical XNA manner. Like usual, ou...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Adding a Video to Your Project

Our first step is to get ahold of a video that we want to play, add it to your project, and load it in the typical XNA manner. Like usual, our first step is to add the content that we need to our project.

XNA is expecting movies in the .wmv file format, though there are video format converters out there that will let you convert to .wmv if you need to. Add the video to your project like we have done before. I have used KeepVid to download the MP4 file from a youtube video.

Loading the Video in XNA

Video will require an additional using statement to be added:

using Microsoft.Xna.Framework.Media;

To playback video, we'll need two variables play the video. At the top of your main game class (initially Game1), or wherever you think will be the most appropriate, add the following two variables to handle video playback:

Video video;
VideoPlayer player;

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

To load the video, in the LoadContent method (or wherever it is needed) 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:

player.Play(video);

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)
{
    player.Play(video);
}

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.

At this point, we have a normal Texture2D, and we can do anything we want with it, including using it as the texture for a 3D object in a game. We'll do that in another tutorial. For now, we'll stick with something simpler, and just draw it 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.Begin();
    spriteBatch.Draw(videoTexture, new Rectangle(0, 0, 400, 240), Color.White);
    spriteBatch.End();
}

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.