在Windows Phone 7中播放视频有两种方式,一种是使用MediaElement 控件来播放,一种是使用启动器MediaPlayerLanucher来实现视频的播放。用MediaElement 控件来播放视频比较灵活,你需要自己去实现播放暂停进度条等等的功能,播放屏幕的大小也可以由你来自定义,用启动器MediaPlayerLanucher来播放视频,是相当于调用了系统的默认播放器来打开你的视频,不过你可是改不了人家系统默认的播放器滴。
第一种方式:MediaElement 控件播放视频。
MediaElement 可以播放许多不同类型的音频和视频媒体。MediaElement 基本上是一个矩形区域,可以在其图面上显示视频,或播放音频(在这种情况下将不显示视频,但 MediaElement 仍然充当具有相应 API 的播放器对象)。因为它是一个 UIElement,所以,MediaElement 支持输入操作,并可以捕获焦点或鼠标。使用属性 Height 和 Width 可以指定视频显示图面的高度和宽度。但是,为了获得最佳性能,应避免显式设置 MediaElement 的宽度和高度。而是将这些值保留为未设置。指定源之后,媒体将以其实际大小显示,布局将重新计算该大小。如果需要更改媒体显示的大小,最好使用媒体编码工具将媒体重新编码为所需大小。默认情况下,加载 MediaElement 对象后,将立即播放由 Source 属性定义的媒体。
播放本地视频文件的XAML语法如下:
<MediaElement Source="test.wmv" AutoPlay="True"/> <MediaElement Source="test.wmv" AutoPlay="True"/>
播放远程视频文件的XAML语法如下:
<MediaElement Source="http://mschannel9.vo.msecnd.net/o9/mix/09/wmv/key01.wmv" AutoPlay="True"/>
MainPage.xaml
<phone:PhoneApplicationPage x:Class="MediaPlayer.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="PortraitOrLandscape" Orientation="Portrait" shell:SystemTray.IsVisible="True">
<Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="播放网络视频" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="media player" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="40" /> </Grid.RowDefinitions> <!--添加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>
</phone:PhoneApplicationPage>
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(); } }}
第二种方式:使用启动器MediaPlayerLanucher来实现视频的播放。
MediaPlayerLanucher 的功能是去启动和播放多媒体文件。前一章讲解过使用MediaElement元素来播放多媒体文件,那么使用MediaPlayerLanucher启动器是另外的一种播放多媒体文件的方式,这是利用了系统内部的多媒体播放器直接全屏显示播放多媒体文件。下面来看一下MediaPlayerLanucher类的一些重要的属性。
(1) Location属性,Location 是描述文件是放置在什么样的位置,有下面三种类型。
MediaLocationType.Install:指的就是跟着你的xap文件一起部署过去的相关文件,也就是位于程序安装的目录中。
MediaLocationType.Data:指的是位于隔离储存区当中的文件,也就是说如果你的文件是执行之后才会取得或是产生的(例如说从网络下载),而会将档案写入到隔离储存区当中,这个时候就要设定为这个属性。
MediaLocationType.None:这个属性目前来说是没有作用的,如果设定为None,那么呼叫Show 的方法之后,直接就会丢出异常FileNotFroundException。
(2) Meida 属性,Media 是文件的位置以及文件名称,是以Uri 的方式来表示。
(3) Controls 属性,Controls 是设定MediaPlayer 出现之后,在画面上会出现哪一些控制按钮,而各个项目也可以利用OR 的方式去设定。
MainPage.xaml
1<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 2 <Button Content="播放视频" Height="116" HorizontalAlignment="Left"Margin="100,81,0,0" Name="Start" VerticalAlignment="Top" Width="273" Click="Start_Click"/> 3 </Grid>
MainPage.xaml.cs
private void Start_Click(object sender, RoutedEventArgs e) { //创建一个多媒体的启动器 MediaPlayerLauncher mpl = new MediaPlayerLauncher(); //设置播放文件放置的位置属性 mpl.Location = MediaLocationType.Install; //设置所有控制纽都出现 mpl.Controls = MediaPlaybackControls.All; //设置出现停止按钮以及暂停按钮 mpl.Controls = MediaPlaybackControls.Pause | MediaPlaybackControls.Stop; //设置播放的文件 mpl.Media = new Uri(@"Media\123.wmv", UriKind.Relative); //启动播放 mpl.Show(); }
原文链接:http://www.cnblogs.com/linzheng/archive/2011/12/19/2293998.html
转载于:https://www.cnblogs.com/CharlesGrant/p/3639303.html