MediaElement无法播放mp3

MediaElement无法播放mp3

本文介绍了MediaElement无法播放mp3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在xaml中,我有:

<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>

在一种可以触发的方法中,我有:

In a method that fires I have:

this.myGamePlayerPage.footStep.Play();

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

No sound plays but there is no error. Any ideas why this is? Thanks.

编辑:我将源更改为此:Source="C:\newnew\TheseusAndTheMinotaur\minotaurFoot.mp3",并且可以使用.但这不好.它不适用于其他占用者.

I changed the source to this: Source="C:\newnew\TheseusAndTheMinotaur\minotaurFoot.mp3" and it works. But this is no good. It won't work for other ocmputers.

推荐答案

您的设置似乎正确,但是我想MediaElement无法找到minotaurFoot.mp3.注册MediaElementMediaFailed -Event并检查它是否被引发.传递给该方法的ExceptionRoutedEventArgs应包含有关无法播放文件的原因的信息.

Your setup seems to be correct, but i guess that the MediaElement cannot find the minotaurFoot.mp3. Register the MediaFailed-Event of the MediaElement and check if it gets raised. The ExceptionRoutedEventArgs passed to the method, should contain information about, why the file cannot be played.

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来完成.

You also need to copy the mp3 to the output folder of you project. This is done by setting Copy always or Copy if newer in the Advanced Setting Copy to Output directory.

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

Select the mp3 file in your project, right click to open ContextMenu. Then select Properties and make the specified setting above.

这篇关于MediaElement无法播放mp3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 08:58