MediaElement won't play an mp3

天大地大妈咪最大 提交于 2019-12-13 01:26:58

问题


In the xaml I have:

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

EDIT: 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.


回答1:


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
}

Update

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.

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



来源:https://stackoverflow.com/questions/20260001/mediaelement-wont-play-an-mp3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!