网络视频的播放处理方式是和本地视频播放相类似的,只不过是MediaElement的源换成了网络的视频文件。因为播放网络视频会受到网络的好坏影响,不过Windows Phone 7系统内部已经做了缓冲的处理,可以通过MediaElement类的BufferingProgress属性来获取视频缓冲的信息。
下面给出播放网络视频的示例: 播放网络上的视频文件,并监控视频播放缓冲的情况和播放的进度。
代码清单8-4:播放网络视频(源代码:第8章\Examples_8_4)
MainPage.xaml文件主要代码
------------------------------------------------------------------------------------------------------------------
<Grid x:Name="ContentGrid" Grid.Row="1">
<MediaElement Height="289" HorizontalAlignment="Left"
Margin="26,148,0,0" x:Name="mediaPlayer"
VerticalAlignment="Top" Width="417"
AutoPlay="False"/>
<Button Content=">" Height="72"
HorizontalAlignment="Left" Margin="13,527,0,0"
x:Name="btnPlay" VerticalAlignment="Top" Width="87"
Click="btnPlay_Click" />
<Button Content="O" Height="72"
HorizontalAlignment="Right" Margin="0,527,243,0"
x:Name="btnStop" VerticalAlignment="Top" Width="87"
Click="btnStop_Click" />
<Button Content="||" Height="72" Margin="0,527,313,0"
x:Name="btnPause" VerticalAlignment="Top"
Click="btnPause_Click" HorizontalAlignment="Right" Width="87" />
<!--播放的进度条-->
<Slider Height="84" HorizontalAlignment="Left"
Margin="13,423,0,0" Name="mediaTimeline"
VerticalAlignment="Top" Width="443"
ValueChanged="mediaTimeline_ValueChanged"
Maximum="1" LargeChange="0.1" />
<TextBlock Height="30" HorizontalAlignment="Left"
Margin="26,472,0,0" Name="lblStatus"
Text="00:00" VerticalAlignment="Top" Width="88" FontSize="16" />
<TextBlock Height="30"
Margin="118,472,222,0" x:Name="lblBuffering"
Text="缓冲" VerticalAlignment="Top" FontSize="16" />
<TextBlock Height="30"
Margin="0,472,82,0" x:Name="lblDownload"
Text="下载" VerticalAlignment="Top" FontSize="16" HorizontalAlignment="Right" Width="140" />
<Button Content="声音开关" Height="72"
HorizontalAlignment="Left" Margin="217,527,0,0"
Name="btnMute" VerticalAlignment="Top" Width="115"
FontSize="16" Click="btnMute_Click" />
<TextBlock Height="30" HorizontalAlignment="Left"
Margin="324,551,0,0" Name="lblSoundStatus"
Text="声音开" VerticalAlignment="Top" Width="128" />
<TextBox x:Name="txtUrl" Height="57" Margin="91,33,8,0"
TextWrapping="Wrap" VerticalAlignment="Top" FontSize="16"
Text="http://localhost/123.wmv"/>
<TextBlock x:Name="lblUrl" HorizontalAlignment="Left" Height="25"
Margin="8,48,0,0" TextWrapping="Wrap" Text="视频地址:"
VerticalAlignment="Top" Width="83" FontSize="16"/>
<Button Content="加载视频" Height="72" HorizontalAlignment="Left" Margin="91,78,0,0" Name="load" VerticalAlignment="Top" Width="339" Click="load_Click" />
</Grid>
MainPage.xaml.cs文件代码
------------------------------------------------------------------------------------------------------------------
using System;
using System.Windows;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
namespace MediaPlayerDemo
{
public partial class MainPage : PhoneApplicationPage
{
//是否更新播放的进度显示
private bool _updatingMediaTimeline;
public MainPage()
{
InitializeComponent();
}
//暂停播放
private void btnPause_Click(object sender, RoutedEventArgs e)
{
if (mediaPlayer.CanPause)
{
mediaPlayer.Pause();
lblStatus.Text = "暂停";
}
else
{
lblStatus.Text = "不能暂停,请重试!";
}
}
//停止播放
private void btnStop_Click(object sender, RoutedEventArgs e)
{
mediaPlayer.Stop();
mediaPlayer.Position = System.TimeSpan.FromSeconds(0);
lblStatus.Text = "停止";
}
//开始播放
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
mediaPlayer.Play();
}
//声音开关
private void btnMute_Click(object sender, RoutedEventArgs e)
{
if (lblSoundStatus.Text.Equals("声音开", StringComparison.CurrentCultureIgnoreCase))
{
lblSoundStatus.Text = "声音关";
mediaPlayer.IsMuted = true;
}
else
{
lblSoundStatus.Text = "声音开";
mediaPlayer.IsMuted = false;
}
}
//播放进度
private void mediaTimeline_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!_updatingMediaTimeline && mediaPlayer.CanSeek)
{
TimeSpan duration = mediaPlayer.NaturalDuration.TimeSpan;
int newPosition = (int)(duration.TotalSeconds * mediaTimeline.Value);
mediaPlayer.Position = new TimeSpan(0, 0, newPosition);
}
}
//加载视频
private void load_Click(object sender, RoutedEventArgs e)
{
_updatingMediaTimeline = false;
mediaPlayer.Source = new Uri(txtUrl.Text);
// 视频播放的位置从第0秒开始
mediaPlayer.Position = System.TimeSpan.FromSeconds(0);
//更新播放的进度百分比
mediaPlayer.DownloadProgressChanged += (s, ee) =>
{
lblDownload.Text = string.Format("下载 {0:0.0%}", mediaPlayer.DownloadProgress);
};
// 预设一分钟缓冲视频的时间
mediaPlayer.BufferingTime = TimeSpan.FromSeconds(60);
//更新缓冲的进度百分比
mediaPlayer.BufferingProgressChanged += (s, ee) =>
{
lblBuffering.Text = string.Format("缓冲 {0:0.0%}", mediaPlayer.BufferingProgress);
};
// 更新视频播放的进度条
CompositionTarget.Rendering += (s, ee) =>
{
_updatingMediaTimeline = true;
TimeSpan duration = mediaPlayer.NaturalDuration.TimeSpan;
if (duration.TotalSeconds != 0)
{
double percentComplete = mediaPlayer.Position.TotalSeconds / duration.TotalSeconds;
mediaTimeline.Value = percentComplete;
TimeSpan mediaTime = mediaPlayer.Position;
string text = string.Format("{0:00}:{1:00}",
(mediaTime.Hours * 60) + mediaTime.Minutes, mediaTime.Seconds);
if (lblStatus.Text != text)
lblStatus.Text = text;
_updatingMediaTimeline = false;
}
};
mediaPlayer.Play();
}
}
}
程序运行的效果如图8.4所示。