IT tutorials
 
Mobile
 

Using Media in XNA Game Studio : Media Enumeration

1/17/2012 4:52:53 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
Shipping music with your game is great, but what if you want to use the media library that already exists on the user’s machine? There are quite a few games out there that let the user pick music from his or her own library while playing. Naturally, this functionality is included in XNA Game Studio.

Media Library

The starting point for this functionality is the MediaLibrary class. Create a new Windows Phone Game project and add the following variable:

MediaLibrary media;

Initialize this object inside the Initialize method:

media = new MediaLibrary();

This initializes a new media library with the default source, although you can also use the overload that expects a MediaSource object. Using that overload forces you to create a new MediaSource object stating the sources type (either local or via Windows Media Connect) and the name. You can also get the media source from the just initialized media library. Alternatively, use the MediaSource.GetAvailableMediaSources method to return a collection of all available media sources on your current device.

Much like the multiple ways to get to songs, the media library class is an extension of that. Start with the entire library rather than a small portion of the library (one song). It has the Albums property, which is the collection of albums in the library and contains everything you learned earlier. It also has the Artists property, the Genres property, and the Songs properties, which are the collections of these things. Just like before, you can enumerate through all of your music through any of these properties (or any combination of them). You can also use the PlayLists in your library, which are a collection of Songs that can be played. Each playlist includes a Name and Duration much like the Albums collection, along with the Songs. Genres property does not exist because it is a user-specified collection that includes many different genres.

The media library has a few extra things, including pictures and music. The RootPictureAlbum property returns a PictureAlbum class. Each PictureAlbum includes a list of PictureAlbums (think folders). The Name property is also included as in all the other objects. The Parent property exists, too, which is the picture album that it is a member of (except for the RootPictureAlbum, which has no parent). There is a Pictures collection on each album, too.

Note

On Windows and Xbox 360, the picture collection returns an empty collection.


The Pictures collection on the media library returns the entire list of pictures, in all albums. Each picture in the collection has a few properties, such as Name, Width, and Height. It also includes Date, which is when the picture was taken or saved, and the Album the picture belongs to. Much like the music, there are multiple ways to get to each picture.

Of course, enumerating the pictures isn’t exciting by itself. Instead let’s find a picture, modify it slightly, and then save it out. You need a device to do this, and you have two choices of how to get the picture to modify.

First, add references to Microsoft.Phone.Tasks.dll and Microsoft.Phone.dll to your project, which is where the camera task comes from (and the camera is one of the ways you get a picture). Use #ifdef to use the camera or a saved picture as the starting point. Assuming you want to use the camera, add the following at the top of your game1.cs code file:

#define USE_CAMERA
#if USE_CAMERA
using Microsoft.Phone.Tasks;
#endif

You need to know when you have a picture to modify, along with ways to modify them, so add a few new variables to your class:

bool havePicture;
SpriteFont font;
Texture2D texture;
RenderTarget2D renderTarget;

To add a new sprite font to your content project, call it Photo. You can keep the default values (or modify them, if you like). Now, start the task to take a picture so you can modify it. In your LoadContent method, add the following code:

    #if USE_CAMERA
// Create the camera chooser, and save the data from it
new CameraCaptureTask().Show();
ChooserListener.ChooserCompleted += (s, e) =>
{
var args = (TaskEventArgs<PhotoResult>)e;
// Save the picture
Picture picture = media.SavePicture("Camera Picture" +
DateTime.Now.ToString(), args.Result.ChosenPhoto);
texture = Texture2D.FromStream(GraphicsDevice,
picture.GetImage());
havePicture = true;
};
#else
#endif

As long as the USE_CAMERA define is set, it shows the camera capture task (enabling you to take a picture), and when that has completed, you save the picture to your photo library, and then load that into the texture. Set the boolean to true to signify you’re ready to modify and save it.

The other way you can get your picture is directly from your photo library. This code assumes you have pictures in the library, but does not crash if you do not. Update the LoadContent method to include the following in between the #else and the #endif:

if (media.Pictures[0] != null)
{
texture = Texture2D.FromStream(GraphicsDevice,
media.Pictures[0].GetImage());
}
havePicture = true;

Assuming you have a picture in your photo library, it now sets to the first one in the list. If you want to use this instead of the camera, simply comment out the top line (the #define). With a picture now set (regardless of which method you choose), you can update the code to modify it slightly and save it out. First, declare a few new variables.

You need to update the LoadContent method again to instantiate the other variables (you can do so after the #endif):

font = Content.Load<SpriteFont>("Photo");
renderTarget = new RenderTarget2D(GraphicsDevice,
GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);

This loads the font used to write across the image, creates a texture out of the picture if it exists, and creates a render target to render the modified picture to. Now, replace your Draw method with the following:

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
if (texture != null)
{
// Switch to the render target so you can save it later
GraphicsDevice.SetRenderTarget(renderTarget);
spriteBatch.Begin();
spriteBatch.Draw(texture, GraphicsDevice.Viewport.Bounds,
Color.White);
spriteBatch.DrawString(font, "I just modified\r\nthis picture!",
Vector2.Zero, Color.White);
spriteBatch.End();
// Switch back to back buffer to commit the render target
GraphicsDevice.SetRenderTarget(null);
}
if (havePicture)
{
SavePicture("ModifiedPicture" + DateTime.Now.ToString(),
renderTarget);
// Exit the game now, you're done
Exit();
}
base.Draw(gameTime);
}


This code should be familiar to you by now. Switch to your created render target, draw your original picture full screen, and write some text over it. With that out of the way, you can exit the game. Now that you have modified the image, use the following code to save the picture:

private void SavePicture(string name, Texture2D texture)
{
System.IO.
MemoryStream stream = new System.IO.MemoryStream();
texture.SaveAsJpeg(stream, texture.Width, texture.Height);
media.SavePicture(name, stream);
}

Of course, you need to call this method. Right before your call to Exit in Draw, add the following:

SavePicture("ModifiedPicture" + DateTime.Now.ToString(),
renderTarget);

This is easy photo manipulation! There is also a special picture collection on the media library called SavedPictures, which is the collection of saved pictures. This is where the pictures from SavePicture end up.

 
Others
 
- Using Media in XNA Game Studio : What Is Media?
- 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
 
 
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