×
Namespaces

Variants
Actions

使用MediaElement播放电影

From Nokia Developer Wiki
Jump to: navigation, search
WP Metro Icon Multimedia.png
SignpostIcon XAML 40.png
SignpostIcon WP7 70px.png
Article Metadata

兼容于
文章
翻译:
最后由 hamishwillee 在 17 Jul 2013 编辑

本文演示了如何使用MediaElement播放电影

Contents

简介

使用Silverlight播放电影很简单。本文介绍怎样从本地以及远程服务器使用MediaElement播放电影,同时也说明了怎样使用MediaPlayerLauncher播放远程电影。

StreamingVideo.png

基本思路

在XAML中添加一个MediaElement,在CS文件中设置它的Source属性。此外也可以通过Play,Pause,Stop按钮控制电影播放。使用不同的按钮播放本地以及远程的电影。最后一个按钮就用来打开MediaPlayerLauncher。所有的streaming都使用HTTP拓扑。

实现

首先创建一个Windows Phone程序。添加一个MediaElement和六个按钮:play, pause, stop, Local, HTTP, Launch Default Player。

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<MediaElement Height="299" HorizontalAlignment="Left" Margin="28,6,0,0" Name="mediaElement1" VerticalAlignment="Top" Width="400" Volume="100"/>
<Button Content="Local" Height="72" HorizontalAlignment="Left" Margin="28,425,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
<Button Content="HTTP" Height="72" HorizontalAlignment="Right" Margin="0,425,28,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />
<Button Content="Launch Default Player" Height="72" HorizontalAlignment="Left" Margin="28,513,0,0" Name="button3" VerticalAlignment="Top" Width="400" Click="button3_Click" />
<Button Content="pause" Height="72" HorizontalAlignment="Left" Margin="28,311,0,0" Name="button4" VerticalAlignment="Top" Width="118" Click="button4_Click" />
<Button Content="play" Height="72" HorizontalAlignment="Left" Margin="164,311,0,0" Name="button5" VerticalAlignment="Top" Width="133" Click="button5_Click" />
<Button Content="stop" Height="72" HorizontalAlignment="Left" Margin="303,311,0,0" Name="button6" VerticalAlignment="Top" Width="125" Click="button6_Click" />
</Grid>

在MainPage.xaml.cs中,针对每个按钮的事件,调用PlayVideo,并且开始播放电影。

public void PlayVideo(string aUrl)
{
mediaElement1.Stop();
mediaElement1.Source = new Uri(aUrl, UriKind.RelativeOrAbsolute);
mediaElement1.Play();
mediaElement1.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(MediaElement_MediaFailed);
 
}
 
void MediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
var errorException = e.ErrorException;
}

为了使用MediaPlayerLauncher,首先添加namespace:'Microsoft.Phone.Tasks。MediaPlayerLauncher会打开一个标准的电影播放程序并且播放设定的电影。我们要做的事就是创建一个 MediaPlayerLauncher实例,设置好各种属性,并且调用Show。

Note.pngNote: 在XAML使用MediaElement时, 需要给它命名,否则Media Library的安全策略可能阻止你的程序发布至Marketplace。

现在就可以用MediaPlayerLauncher播放电影了。

MediaPlayerLauncher mediaPlayerLauncher = new MediaPlayerLauncher();
mediaPlayerLauncher.Media = new Uri(@"http://video-js.zencoder.com/oceans-clip.mp4", UriKind.Absolute);
 
mediaPlayerLauncher.Show();

源代码

本文的源代码在这里:File:StreamingVideoWP7.zip

This page was last modified on 17 July 2013, at 09:49.
56 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×