0

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

屏幕:

在此处输入图像描述

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那么我能做什么?

4

1 回答 1

0

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

于 2012-12-04T21:11:45.010 回答