在Windows Phone应用程序中播放视频依然还是使用MediaElement元素去实现,与音频播放不同的是视频的播放还产生一个显示效果,也就是说MediaElement元素就必须要有一定的宽度和高度才能够把视频展现出来。
下面给出播放本地视频的示例: 播放程序本地的视频,并将播放的进度用进度条显示出来。
代码清单8-3:播放本地视频(源代码:第8章\Examples_8_3)
MainPage.xaml文件主要代码
------------------------------------------------------------------------------------------------------------------
<Grid x:Name="LayoutRoot" Background="Transparent">
……
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
……
<!--添加MediaElement多媒体播放控件-->
<MediaElement Name="myMediaElement" AutoPlay="True" Grid.Row="0" />
<ProgressBar Name="pbVideo" Grid.Row="1" />
</Grid>
</Grid>
<!--3个菜单栏:播放、暂停和停止-->
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" >
<shell:ApplicationBarIconButton IconUri="/icons/play.png" Click="Play_Click" Text="播放"/>
<shell:ApplicationBarIconButton IconUri="/icons/pause.png" Click="Pause_Click" Text="暂停"/>
<shell:ApplicationBarIconButton IconUri="/icons/stop.png" Click="Stop_Click" Text="停止"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
MainPage.xaml.cs文件代码
------------------------------------------------------------------------------------------------------------------
using System;
using System.Windows;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using Microsoft.Phone.Shell;
namespace MediaPlayer
{
public partial class MainPage : PhoneApplicationPage
{
// 使用定时器来处理视频播放的进度条
DispatcherTimer currentPosition = new DispatcherTimer();
// 页面的初始化
public MainPage()
{
InitializeComponent();
//定义多媒体流可用并被打开时触发的事件
myMediaElement.MediaOpened += new RoutedEventHandler(myMediaElement_MediaOpened);
//定义多媒体停止时触发的事件
myMediaElement.MediaEnded += new RoutedEventHandler(myMediaElement_MediaEnded);
//定义多媒体播放状态改变时触发的事件
myMediaElement.CurrentStateChanged += new RoutedEventHandler(myMediaElement_CurrentStateChanged);
//定义定时器触发的事件
currentPosition.Tick += new EventHandler(currentPosition_Tick);
//设置多媒体控件的网络视频资源
myMediaElement.Source = new Uri("123.wmv", UriKind.Relative);
}
//视频状态改变时的处理事件
void myMediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
{
if (myMediaElement.CurrentState == MediaElementState.Playing)
{//播放视频时各菜单的状态
currentPosition.Start();
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = false; // 播放
((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = true; // 暂停
((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = true; // 停止
}
else if (myMediaElement.CurrentState == MediaElementState.Paused)
{ //暂停视频时各菜单的状态
currentPosition.Stop();
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true;
((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = false;
((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = true;
}
else
{//停止视频时各菜单的状态
currentPosition.Stop();
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true;
((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = false;
((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = false;
}
}
//多媒体停止时触发的事件
void myMediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
//停止播放
myMediaElement.Stop();
}
//多媒体流可用并被打开时触发的事件
void myMediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
//获取多媒体视频的总时长来设置进度条的最大值
pbVideo.Maximum = (int)myMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;
//播放视频
myMediaElement.Play();
}
//定时器触发的事件
void currentPosition_Tick(object sender, EventArgs e)
{
//获取当前视频播放了的时长来设置进度条的值
pbVideo.Value = (int)myMediaElement.Position.TotalMilliseconds;
}
//播放视频菜单事件
private void Play_Click(object sender, EventArgs e)
{
myMediaElement.Play();
}
//暂停视频菜单事件
private void Pause_Click(object sender, EventArgs e)
{
myMediaElement.Pause();
}
//停止视频菜单事件
private void Stop_Click(object sender, EventArgs e)
{
myMediaElement.Stop();
}
}
}
程序运行的效果如图8.3所示。