天天看點

Windows Phone 7 播放視訊

在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.v​​o.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  

            {//停止視訊時各菜單的狀态  

                ((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)  

        //暫停視訊菜單事件  

        private void Pause_Click(object sender, EventArgs e)  

            myMediaElement.Pause();  

        //停止視訊菜單事件  

        private void Stop_Click(object sender, EventArgs e)  

    }  

第二種方式:使用啟動器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 的方式去設定。

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">            <Button Content="播放視訊" Height="116" HorizontalAlignment="Left" Margin="100,81,0,0" Name="Start" VerticalAlignment="Top" Width="273" Click="Start_Click" />        </Grid> 

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();   

        } 

本文轉自linzheng 51CTO部落格,原文連結:http://blog.51cto.com/linzheng/1081675

繼續閱讀