Media

0
190

Windows Phone 7 physical devices can store and display photos, play video and audio files, and have a built-in FM radio. To display photos and images, you can use the standard Silverlight controls such as the Image control. You can include the image file in your project and set the Build Action property to Content and the Copy to Output Directory property to Copy if newer or Copy Always, so that it is included in the XAP file that uploads to the device. You then specify the image as the source for the control. Alternatively, you can specify a remote URL for the image, and it will be downloaded and displayed
just as in a web browser. Images must be in JPEG (.jpg) or PNG (.png) format.

To display a video or play an audio file, you can use the Silverlight MediaElement control. You can include the video or audio file in your project and set the Build Action property to Content and the Copy to Output Directory property to Copy if newer or Copy Always, and then specify it as the source of the control.

XML
<MediaElement Source=”MyVideo.wmv” Width=”300″ Height=”300″
AutoPlay=”True”/>

Alternatively, you can specify a remote URL for the video or audio file, and it will be streamed or downloaded and played just as in a Web browser. For a list of the supported video and audio formats, see “Supported Media Codecs for Windows Phone” on MSDN (http://msdn.microsoft.com/en-us/library/ff462087(VS.92).aspx). For information about using the MediaElement control, see “MediaElement” on MSDN (http://msdn.microsoft.com/en-us/library/bb980132(VS.95).aspx).

An alternative approach for video and audio files is to use the MediaPlayerLauncher task. You create an instance of this class, set the required properties, and the call the Show method. In this example, the Build Action property of the video file is set to Content and the Copy to Output Directory property is set to Copy if newer so that the video file is included in the XAP file uploaded to the device.

C#
MediaPlayerLauncher mediaLauncher = new MediaPlayerLauncher();
mediaLauncher.Controls = MediaPlaybackControls.All;
mediaLauncher.Location = MediaLocationType.Install;
mediaLauncher.Media = new Uri(“MyVideo.wmv”, UriKind.Relative);
mediaLauncher.Show();

Alternatively, you can copy the media item to isolated storage on the device and specify the MediaLocation property as Media LocationType.Data. For more information, see “MediaPlayerLauncher Class” on MSDN (http://msdn.microsoft.com/en-us/library/microsoft.phone.tasks.mediaplayerlauncher(VS.92).aspx).

Note: When using the MediaPlayerLauncher task in the emulator, you may find that video frames render only if you repeatedly click on the UI. Each click advances the video by one frame and then the display turns black.

Selecting a Photo on the Device

You can select an existing photo from the user’s media collection using the PhotoChooserTask class. You specify a callback handler to execute when the user selects a photo, and optionally the size for the picture that will be returned.

C#
PhotoChooserTask photoTask = new PhotoChooserTask();
photoTask.Completed += new EventHandler<PhotoResult>
(PhotoTask_Completed);
photoTask.PixelHeight = 500;
photoTask.PixelWidth = 500;
photoTask.Show();

When the user selects a picture, the callback can obtain the name of the picture and the Stream instance pointing to it. You must decode the byte stream into an appropriate image format for display, such as in an Image control on the page, as shown here. To use this code, you must add a reference to the namespaces System.Windows. Media.Imaging and Microsoft.Phone (for the PictureDecoder class).

C#
void PhotoTask_Completed(object sender, PhotoResult result)
{
if (result.Error == null && result.TaskResult == TaskResult.OK)
{
string fileName = result.OriginalFileName;
WriteableBitmap thePhoto = PictureDecoder.DecodeJpeg(
result.ChosenPhoto);
MyImageControl.Source = thePhoto;
}
}

For more information about using media such as photos, video, and audio on Windows Phone 7, see “Media for Windows Phone” (http://msdn.microsoft.com/en-us/library/ff402560(VS.92).aspx) and “Photos for Windows Phone” (http://msdn.microsoft.com/en-us/library/ff402553(VS.92).aspx) on MSDN.