Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

Using Media in XNA Game Studio : Video

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
8/2/2012 5:18:32 PM
There are times when you might want to play a video in your game. Perhaps you have a “cut scene” in your game, or the introduction video. This functionality is easy to use and can be done in just a few lines of code.

Rendering a Video

Create a new Windows Game project and add the samplevideo.wmv file from the downloadable examples to the content project. Add a couple variables to your project to hold and control the video:

Video video;
VideoPlayer player;

Of course, you have to instantiate them in your LoadContent method:

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

Before getting into the details of these classes, let’s get your video rendering. Add the following lines directly after creating the player in the LoadContent method:

player.IsLooped = true;
player.Play(video);

Finally, replace the Draw method with the following:

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.Black);
    spriteBatch.Begin();
    spriteBatch.Draw(player.GetTexture(), GraphicsDevice.Viewport.Bounds, Color.White);
    spriteBatch.End();
    base.Draw(gameTime);
}

					  

That’s it. Running the project now renders your video to the full window and repeats the video until the project is closed as in Figure 1.

Figure 1. Rendered video

The Video object is a repository for the data about a video. It includes the Duration of the video and the FramesPerSecond (the number of frames of video in each second, if the name wasn’t obvious). The native Width and Height of the video are stored here, too, along with the VideoSoundtrackType. The sound track type can be Dialog, Music, or MusicAndDialog, and these are used to determine how the system should interact with the audio from the video. If the sound track type is Music, and the system controls the music, the audio from your video does not play. If the setting is Dialog, and the system controls the music (and music is playing), the audio from the video mixes with this music. If the setting is MusicAndDialog, system sounds stop and the video’s audio plays.

Note

The VideoSoundTrackType is valid only for Xbox 360.


The VideoPlayer object is similar to the MediaPlayer object from earlier. It has IsMuted and IsLooped properties (where looped is similar to repeating, except it loops only the current video). It has the Play, Pause, Stop, and Resume methods, along with the State property, which mirrors what you saw earlier. It also has the PlayPosition property you saw previously. The last method, which you used in the Draw method previously, is GetTexture. This method returns the current frame of video as a texture. So long as the video is playing (via the Play method), you don’t need to do anything special to make it continue to update.

Because this is a Texture2D, it can be used in any place a texture is used. You can use it to render in a sprite batch, but you could just as easily use it to render onto a 3D model. Add a new model variable to your content project:

Model model;

To get the content, add the box.fbx model to your content project and then update LoadContent:

model = Content.Load<Model>("Box");

Finally, replace your Draw method with the following and notice your video rendering onto each face of the cube spinning around as in Figure 2:

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    BasicEffect effect = model.Meshes[0].Effects[0] as BasicEffect;
    effect.Texture = player.GetTexture();
    effect.TextureEnabled = true;
    model.Draw(Matrix.CreateRotationX((float)gameTime.TotalGameTime.TotalSeconds) *
        Matrix.CreateRotationY((float)gameTime.TotalGameTime.TotalSeconds /2) *
        Matrix.CreateScale(3.0f),
        Matrix.CreateLookAt(new Vector3(5,0,-5),
        Vector3.Zero, Vector3.Up),
        Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
        GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f));
    base.Draw(gameTime);
}

					  

Figure 2. Rendered video onto a 3d cube

There shouldn’t be anything surprising in this section of code. All you do is render a model with a slight rotation and scale, and update its texture every frame. Even though the most common use of videos is for cut scenes where they are rendered over the entire viewable area, this snippet shows it is possible to render them onto 3D objects, too.

Note

The Video and VideoPlayer classes are not available on Windows Phone 7. To render video here, you must use the MediaPlayerLauncher task. By using that task though, you cannot get the texture from the video to use in your game as you’ve done here.

Other -----------------
- XNA Game Studio 4.0 : Dynamic Sound Effects - Recording Audio with a Microphone, Generating Dynamic Sound Effects
- XNA Game Studio 4.0 : Playing Sound Effects (part 2) - Microsoft Cross-Platform Audio Creations Tool
- XNA Game Studio 4.0 : Playing Sound Effects (part 1) - Using SoundEffect for Audio Playback
- Windows Phone 7 : Using MVVM and Performing Unit Testing
- Windows Phone 7 : Implementing MVVM on Windows Phone by Using MVVMLight
- Windows Phone 7 : In the Cloud - Creating a Feed Reader
- Windows Phone 7 : In the Cloud - Interacting with WCF
- Windows Phone 7 : Isolated Storage - Saving a Photo in Isolated Storage (part 2)
- Windows Phone 7 : Isolated Storage - Saving a Photo in Isolated Storage (part 1)
- Windows Phone 7 : Isolated Storage - Modifying Settings of Your Application
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server