次の方法で共有


Quickstart: Audio and video for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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.

MediaElement object

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.

Note

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.

Note

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.

MediaElement property

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.

  • 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.

Controlling media playback

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();
} 
Private Sub StopMedia(sender As Object, e As RoutedEventArgs)
    media.[Stop]()
End Sub
Private Sub PauseMedia(sender As Object, e As RoutedEventArgs)
    media.Pause()
End Sub
Private Sub PlayMedia(sender As Object, e As RoutedEventArgs)
    media.Play()
End Sub

Note

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.

See Also

Other Resources

Supported media codecs for Windows Phone 8

Digital Rights Management (DRM) for Windows Phone 8