This topic provides a high-level overview about the capabilities—such as playing music and video and accessing pictures—of the Media API in XNA Game Studio. A game can query a media collection for songs, artists, albums, playlist, genres, pictures, and picture albums by using the Microsoft.Xna.Framework.Media namespace. Items in the media library also can be searched based on metadata such as artist name, album name, and musical genre.
Songs as Background Music
Access to the media library, combined with the ability to use playlists, allows games to create interesting background scores that can change with gameplay. Songs can be played directly from the media library, or can be imported by using the Content Pipeline. For more information, see Playing a Song.
Pictures as Textures
Games can obtain and use textures from pictures within a media library. For more information, see Accessing Pictures from a Picture Album.
Games now can add videos to their projects by using the Content Pipeline, and can display those videos on-screen. Videos can be displayed as cut scenes or loading graphics, or as part of an object in 3D space.
To add a video to a project, use the steps outlined in How to: Add Game Assets to a Content Project, and select a Windows Media Video (.wmv) file to import. The video content processor that comes with XNA Game Studio has some limitations:
- Video enabled with Digital Rights Management cannot be used.
- Video must be encoded to the Windows Media Video 9 "Main" profile by using the VC-1 standard.
- Video must be encoded with a constant bit rate (CBR).
- Video must include an audio track.
- Audio accompanying the video must be Windows Media Audio (.wma) encoded, with a single-pass CBR format.
XNA Game Studio supports the following maximum bit rates:
Profile Level Maximum bit rate Representative resolutions by frame rate Main Low 2 Mbps 320 × 240 @ 24 Hz (QVGA) Medium 10 Mbps
720 × 480 @ 30 Hz (480p)
720 × 576 @ 25 Hz (576p)
High 20 Mbps 1280 × 720 @ 30 Hz (720p)
Without an audio track, a video will fail to load, and will display this message: "Video file is invalid. Please make sure that the video is not DRM protected and is a valid single-pass CBR encoded video file."
Once a video is loaded, games can create a new VideoPlayer object to play, pause, and resume the video. Once the video starts, the game can call the GetTexture method to get a Texture2D containing the current frame of video. The texture can be used by SpriteBatch.Draw or rendered by an Effect as part of an object in 3D space.
Silverlight applications must ensure that FrameworkDispatcher.Update is called regularly in order for "fire and forget" sounds to work correctly. Media Overview throws an InvalidOperationException if FrameworkDispatcher.Update has not been called at least once before making this call. For more information, see Enable XNA Framework Events in Windows Phone Applications.
- Accessing Pictures from a Picture Album
- Demonstrates how to access pictures in a picture album using the Media API on Windows Phone.
- Playing a Video
- Demonstrates how to use the VideoPlayer to play a video.
- Playing a Video on a Surface
- Demonstrates how to use the VideoPlayer to play back videos on the surface of a quad.
- Playing a Song
- Demonstrates how to play a song from a user's media library.
- Playing a Song from a URI
- Demonstrates how to use the MediaPlayer to play a song from a Uniform Resource Identifier (URI).
- Picture Class
- Provides access to a picture in the media library.
- Song Class
- Provides access to a song in the song library.
- Video Class
- Represents a video.
- VideoPlayer Class
- Provides methods and properties to play back, pause, resume, and stop video. VideoPlayer also exposes repeat, volume, and play position information.