当前位置:编程学习 > wap >>

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

            //定义多媒体播放状态改变时触发的事件

    

补充:移动开发 , Windows Phone ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,