2013-11-28 134 views
1

在XAML我:的MediaElement不会播放MP3

<Page.Background> 
    <ImageBrush ImageSource="/TheseusAndTheMinotaur;component/Images/marbleBackground.jpg"/> 
</Page.Background> 
    <Grid x:Name="mainGrid" Margin="0" IsHitTestVisible="True"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition Width="500" x:Name="gameArea"/> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 

    <MediaElement x:Name="footStep" Source="minotaurFoot.mp3" Volume="1" LoadedBehavior="Manual"/> 
    <Button x:Name="btnExit" Content="Exit" HorizontalAlignment="Right" Margin="0,0,220,20" VerticalAlignment="Bottom" Width="75" Click="btnExit_Click" Grid.Column="2" IsHitTestVisible="True"/> 
     <Canvas x:Name="pbxMap" Margin="10" Grid.Column="1" Background="#7F000000" IsHitTestVisible="True" Focusable="True"/> 
    </Grid> 

在触发一个方法,我有:

this.myGamePlayerPage.footStep.Play(); 

没有播放声音,但没有错误。任何想法,为什么这是?谢谢。

编辑:我改变了源代码:Source="C:\newnew\TheseusAndTheMinotaur\minotaurFoot.mp3"它的工作原理。但这不好。它不适用于其他ocmputers。

回答

2

您的设置似乎是正确的,但我猜MediaElement找不到minotaurFoot.mp3。注册MediaFailed - MediaElement的事件,并检查它是否升起。传递给方法的ExceptionRoutedEventArgs应包含有关为什么文件无法播放的信息。

XAML

<MediaElement x:Name="footStep" 
       MediaFailed="MediaFailedHandler" 
       Source="minotaurFoot.mp3" 
       Volume="1" 
       LoadedBehavior="Manual"/> 

C#

public void MediaFailedHandler(object sender, ExceptionRoutedEventArgs e){ 
    // e.ErrorException contains information what went wrong when playing your mp3 
} 

更新

您还需要你的项目到MP3复制到输出文件夹。这是通过在高级设置Copy to Output directory中设置Copy alwaysCopy if newer完成的。

选择您的项目中的mp3文件,右键单击打开ContextMenu。然后选择属性并在上面进行指定的设置。

+0

谢谢!它说它无法找到媒体文件。我的文件直接在项目中。有了这个位置的图像。这是MP3的问题吗? – user2602079

+0

@ user2602079不,没有MP3文件的问题。您只需更改MediaElement的Source属性。尝试使用这个'/ TheseusAndTheMinotaur; component/minotaurFoot.mp3'。或者使用Visual Studio的PropertyWindow来设置Source – Jehof

+0

没有这样的运气。我也把它放在Images文件夹中,并提供与我的图像相同的文件路径,但没有运气。不过谢谢。 – user2602079