IT tutorials
 
Mobile
 

Using Media in XNA Game Studio : What Is Media?

1/17/2012 4:52:01 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

When “media” is discussed in the context of XNA Game Studio, it means video, songs, or enumeration of items in your media library, including pictures, album art, songs, and so on.

Although the media APIs exist on every platform, they were originally designed and intended to be used on small devices. The first implementation of these APIs was in Game Studio 3.0 for the Zune device, and the full set of features are now available on Windows Phone 7, while support for these features is more limited on Xbox 360 and Windows.

Playing a Song

Playing a song is just as easy as playing other sound effects, although it does have some special characteristics. Songs are considered music, and on some platforms can be overridden by the user. For example, on Xbox 360, if a user picks a playlist and plays it via the guide in your game, regardless of what music the game plays, the music the user chooses in the guide is what he hears.

To see how easy it is to play a song, create a new game project and add the samplemusic.mp3 file to your content project. Then, declare a new Song variable to your game:

Song song;

In your LoadContent method, load the song:

song = Content.Load<Song>("samplemusic");

You’re now ready to play the song. Notice that the song object doesn’t have a Play method on it like you have seen on the sound effect classes. This is because there can be only one song playing, where you can have multiple sound effects playing simultaneously. To play the music directly after it is created in your LoadContent method, add the following:

MediaPlayer.Stop();
MediaPlayer.Play(song);

Use the MediaPlayer class to control the music. Strictly speaking, the Stop call isn’t required, but just in case something else plays in this sample code, it is inserted directly before the Play method. Before you look at all the information and data you can get from the Song objects, let’s take a quick look at the MediaPlayer class.

MediaPlayer

There are a few properties on the class you can use to see the current state of what is playing and how. The first one GameHasControl returns true if the songs you play in your game are actually played, and false if the system is currently controlling the playback of music (because the player has requested it as in the previous example). You don’t have to do anything in response to this, and don’t even need to check; the system does the right thing, but if you needed to know, you have that data available.

You can also detect whether the system plays audio via the IsMuted property. Setting this property to true mutes audio currently playing, and setting it to false unmutes it. The IsShuffled property has an effect only if you’re playing a song list rather than a single song. When this property is true and it is time to go to the next song in the list, it randomly picks the next song from the full list of available songs in the collection rather than simply going to the next one in the list.

Use the IsRepeating property to detect or set whether or not the player should repeat playing from the beginning after it has finished playing its current set of music. If this is true and you play more than one song, it starts repeating only after every song in the collection has played.

If the IsVisualizationEnabled property returns true, calls to GetVisualizationData return data about the currently playing song. Visualization data can be retrieved during each frame and return a collection of frequencies and a collection of samples. A common usage of this data is to write visualizers, which are common in media players that have fancy graphics seemingly moving with the music.

Note

IsVisualizationEnabled always returns false on Windows Phone 7.


You can use the PlayPosition property to see how far into a given piece of music you are. It returns a TimeSpan of the duration you heard of the song so far. To find the total length of a song, use the Duration property of the song itself.

The Queue property is the current set of songs that is played by the player. It returns a MediaQueue object, which has three pertinent pieces of information on it: the currently active song returned from the ActiveSong property; the index of this song returned from the ActiveSongIndex property; and the total number of songs in this list, naturally returned in the Count property.

You can also get the State of the currently playing media, which returns a MediaState enumeration. The only values of this enumeration (and the only states available) are Playing, Paused, and Stopped.

The last property on the MediaPlayer is Volume, which is the volume of the music, relative to the current system volume for music. Volume is defined by decibels, where a value of 0.0f subtracts 96 decibels from the current volume, and a value of 1.0f does not modify the volume.

You’ve already seen the Play and Stop method, which are pretty self-explanatory. There are also Pause and Resume methods. Pause and Stop are remarkably similar, in that each of these methods stop the music. Stop, however, resets the current position of the music to the start, and Pause keeps the current position at the moment Pause happened. Resume restarts the music from the current position.

As mentioned, you can have more than one song in a queue. There are two methods to control which song you’re playing, namely MoveNext and MovePrevious. Depending on the state of the IsShuffled property, these methods move to the next or previous song in your list, where the concept of “next” and “previous” are random.

There are also two events you can hook to be notified of status changes of the media played. You can hook the ActiveSongChanged event to see when the current song has changed. This can be from the MoveNext or MovePrevious methods or by the current song ending, and the system automatically moves to a new song.

You can also use the MediaStateChanged event to be notified when the state has changed. As mentioned, these state changes are pause, play, and stop.

Songs and Metadata

At a high level, it is a description of a song, along with audio data necessary for the media player to play it. Most of the data is somewhat circular in nature because there are multiple ways to get to the same data.

The most direct properties for your song are the Name and Duration of the song. You can also get the TrackNumber and PlayCount if these properties exist (they are zero, otherwise). If the IsRated property is true, the Rating property includes the current rating of the song. You can also see whether the song is DRM (Digital Rights Management) protected by looking at the IsProtected property.

You can find out more information about the Artist of the song via the property of the same name, including the Name of the artist, a collection of Songs that the artist has created (including the one you got the artist object from), and a list of Albums the artist has created.

Of course, you can get the Album the song is on, and naturally that object has plenty of information, including the same Artist you got from the original song object (as mentioned, the references here can get quite circular). You can also get the collection of Songs that are on the album and the Name and Duration of the album. You can check whether the album has cover art by looking at the HasArt property. If this returns true, you can get the image from the GetAlbumArt method, which returns a Stream that you can use in the Texture2D.FromStream method to create a texture. You can also use the GetThumbnail method to get the art as a thumbnail.

Both the song and the album have a Genre property, which like the album and artist properties also includes a Songs property that is the collection of songs that belong to this genre. It also includes the collection of Albums that belong to the genre, and the Name of the genre itself.

For any given song, you can reference it four different ways: namely the song itself, through the album, through the artist, or through the genre. You can also get any piece of metadata that exists about the song from any of the types.

Note

Use the Song.FromUri to create a Song object from a Web address. This functionality doesn’t work on Xbox 360 and always throws a NotSupportedException.

 
Others
 
- Android Application Development : Layouts (part 2) - AbsoluteLayout & RelativeLayout
- Android Application Development : Layouts (part 1) - inearLayout
- Building an Advanced Java Game on Symbian OS (part 4) - Using the Bluetooth API
- Building an Advanced Java Game on Symbian OS (part 3) - Using the Mobile 3D Graphics API
- Building an Advanced Java Game on Symbian OS (part 2) - Using the Mobile Media API & Using the Scalable 2D Vector Graphics API
- Building an Advanced Java Game on Symbian OS (part 1)
- jQuery 1.3 : Simultaneous versus queued effects (part 2) - Working with multiple sets of elements & Callbacks
- jQuery 1.3 : Simultaneous versus queued effects (part 1) - Working with a single set of elements
- iPhone 3D Programming : Crisper Text with Distance Fields (part 3) - Implementing Outline, Glow, and Shadow Effects
- iPhone 3D Programming : Crisper Text with Distance Fields (part 2) - Smoothing and Derivatives
 
 
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
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us