2012-12-04 87 views
0

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

XAML

enter image description here

<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所以我能做些什么在MessageBox?

回答

0

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

+0

查看编辑.. – a1204773

+0

您是否将wmv文件设置为“内容” ? –

+0

是的..我认为问题是与我的电脑...也许我没有安装所需的东西? – a1204773