简单的MediaElement类示例

时间:2012-12-04 19:06:15

标签: windows-phone-7

我试图学习MediaElement Class ..所以我找到了一个例子,但是当我运行它时不起作用(视频没有播放)...有人可以告诉我为什么吗?

屏幕:

enter image description here

XAML:

<phone:PhoneApplicationPage 
x:Class="Player.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="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="339"/>
        <RowDefinition Height="429*"/>
    </Grid.RowDefinitions>

    <MediaElement Height="238" HorizontalAlignment="Left" Margin="12,12,0,0" 
                  Name="mediaElement1" VerticalAlignment="Top" Width="424" 
                  Source="/video/Wildlife.wmv" AutoPlay="True" />
    <Button Content="Play" Height="81" HorizontalAlignment="Left" Margin="12,258,0,0" 
            Name="PlayButton" VerticalAlignment="Top" Width="134" Click="PlayButton_Click" />
    <Button Content="Pause" Height="81" HorizontalAlignment="Left" Margin="152,258,0,0"
            Name="PauseButton" VerticalAlignment="Top" Width="124" Click="PauseButton_Click" />
    <Button Content="Stop" Height="81" HorizontalAlignment="Right" Margin="0,258,65,0" 
            Name="StopButton" VerticalAlignment="Top" Width="133" Click="StopButton_Click" />
</Grid>

CS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Player
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void PlayButton_Click(object sender, RoutedEventArgs e)
        {
            mediaElement1.Play();
        }

        private void PauseButton_Click(object sender, RoutedEventArgs e)
        {
            mediaElement1.Pause();
        }

        private void StopButton_Click(object sender, RoutedEventArgs e)
        {
            mediaElement1.Stop();
        }
    }
}

编辑:在@Igor Kulman建议之后添加了两个事件......

private void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Opened");
    }

    private void mediaElement1_MediaFailed(object sender, ExceptionRoutedEventArgs e)
    {
        MessageBox.Show("Failed");
    }

延迟3-5秒后消息框返回Failed所以我可以做什么?

1 个答案:

答案 0 :(得分:0)

您是否将wmv文件的构建操作设置为Content而不是Resource?如果是,请尝试连接到MediaElement的MediaOpened和MediaFailed事件,以查看文件是否已加载或失败(并查看预期以确定原因)