Export (0) Print
Expand All
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Quickstart: Audio and video for Windows Phone 8

August 19, 2014

Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 | Windows Phone OS 7.1

Windows Phone includes support for playing audio and video files. This Quickstart describes how to integrate media into your apps.

This topic contains the following sections.

Adding media to a Windows Phone app is as simple as adding a MediaElement to your markup and providing a Uniform Resource Identifier (URI) to the media to be played. Make sure the extension on the media file matches the file format; otherwise, the media won't play.

NoteNote:

When you add a MediaElement in XAML, always name the MediaElement. Otherwise, the Media Library security capability isn't granted during submission to the Windows Phone Store. For more information, see Security for Windows Phone 8.

The following example creates a MediaElement and sets its Source property to the URI of a video file. The MediaElement object can play Windows Media Video (WMV), Windows Media Audio (WMA), and MP3 files. For a detailed list of the supported formats and protocols, see Supported media codecs for Windows Phone 8.

<MediaElement Name="media" Source="MyMovie.wmv" />

To add a video file to your app, perform the following actions in Visual Studio:

  1. On the Project menu, select Add Existing Item.

  2. Select the desired video file, then click Add.

  3. In Solution Explorer, select the image file that was added.

  4. In the Properties window, change the Build Action property to Resource.

NoteNote:

If you add the video file to Visual Studio project, be sure to set the property of the video item as a Resource. The MediaElement begins playing when the page loads.

The MediaElement object provides several media-specific properties. The following list describes the commonly used properties.

  • AutoPlay: Specifies whether the MediaElement should begin playing automatically. The default value is True.

  • IsMuted: Specifies whether the MediaElement is silenced. A value of True mutes the MediaElement. The default value is False.

  • Stretch: Specifies how video is stretched to fill the MediaElement object. Possible values are None, Uniform, UniformToFill, and Fill. The default is Fill. The following illustration shows Stretch value examples.

    Media - Stretch Enum
  • Volume: Specifies the volume of the MediaElement object's audio as a value from 0 to 1, with 1 being the loudest. The default value is 0.5.

In addition to its media-specific properties, MediaElement also has all the properties of a UIElement, such as Opacity and Clip.

You can control media playback by using the Play, Pause, and Stop methods of a MediaElement object. When a MediaElement object is playing, all other media playback on the Windows Phone device is stopped.

The following example defines a MediaElement object and buttons to stop, pause, and start media playback.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <MediaElement x:Name="media" Source="MyMovie.wmv" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" />
    <!-- Stops media playback.-->
    <Button Click="StopMedia" Grid.Column="0" Grid.Row="1" Content="Stop" />
    <!-- Pauses media playback. -->
    <Button Click="PauseMedia" Grid.Column="1" Grid.Row="1" Content="Pause" />
    <!-- Begins media playback. -->
    <Button Click="PlayMedia" Grid.Column="2" Grid.Row="1" Content="Play" />
</Grid> 

private void StopMedia(object sender, RoutedEventArgs e)
{
    media.Stop();
}
private void PauseMedia(object sender, RoutedEventArgs e) 
{
    media.Pause();
}
private void PlayMedia(object sender, RoutedEventArgs e) 
{
    media.Play();
} 

NoteNote:

In addition to stopping, pausing, or playing media, you can also seek to a specific position by setting the Position property of a MediaElement object.

Show:
© 2014 Microsoft