C# MediaElement赢得';我不放mp3
在xaml中,我有:C# MediaElement赢得';我不放mp3,c#,wpf,mp3,mediaelement,C#,Wpf,Mp3,Mediaelement,在xaml中,我有: <Page.Background> <ImageBrush ImageSource="/TheseusAndTheMinotaur;component/Images/marbleBackground.jpg"/> </Page.Background> <Grid x:Name="mainGrid" Margin="0" IsHitTestVisible="True"> <Grid.Colu
<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>
没有声音播放,但没有错误。你知道为什么吗?谢谢
编辑:我将源代码更改为:
source=“C:\newnew\TheseusAndTheMinotaur\minotaurFoot.mp3”
,它可以工作。但这不好。它不适用于其他计算机。您的设置似乎是正确的,但我猜MediaElement
找不到minotausfoot.mp3
。注册MediaElement
的MediaFailed
-事件,并检查是否引发该事件。传递给方法的ExceptionRoutedEventArgs
应包含有关无法播放文件的原因的信息
XAML
<MediaElement x:Name="footStep"
MediaFailed="MediaFailedHandler"
Source="minotaurFoot.mp3"
Volume="1"
LoadedBehavior="Manual"/>
更新
您还需要将mp3复制到项目的输出文件夹中。这是通过在高级设置复制到输出目录
中设置始终复制
或复制(如果更新
)
选择项目中的mp3文件,右键单击打开上下文菜单。然后选择属性并进行上述指定设置。谢谢!它说找不到媒体文件。我的文件直接在项目中。对于图像,此位置起作用。这是mp3的问题吗?@user2602079否mp3文件没有问题。您只需要更改MediaElement的源属性。尝试使用这个
/TheseusAndTheMinotaur;component/minotaurFoot.mp3
。或者使用VisualStudio的PropertyWindow来设置SourceNon这样的运气。我也把它放在Images文件夹中,并给出了与我的图像相同的文件路径,但没有运气。谢谢。值得注意的是,mediaElement位于网格中,当我在代码中调用它时,我不指定网格,只指定页面。这是因为网格在intellisense中似乎没有,但页面有。谢谢。工作很好。最后一件事-mediaElement似乎需要在客户端计算机上安装windows media player。这看起来很糟糕,因为有些机器可能没有。有没有一种我可以更信任的选择,可以在其他人的电脑上玩?
<MediaElement x:Name="footStep"
MediaFailed="MediaFailedHandler"
Source="minotaurFoot.mp3"
Volume="1"
LoadedBehavior="Manual"/>
public void MediaFailedHandler(object sender, ExceptionRoutedEventArgs e){
// e.ErrorException contains information what went wrong when playing your mp3
}