【问题标题】:MediaElement won't play an mp3MediaElement 不会播放 mp3
【发布时间】:2013-11-28 07:20:21
【问题描述】:

在我的 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>

在一个触发我的方法中:

this.myGamePlayerPage.footStep.Play();

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

编辑:我将源更改为:Source="C:\newnew\TheseusAndTheMinotaur\minotaurFoot.mp3",它可以工作。但这不好。它不适用于其他计算机。

【问题讨论】:

    标签: c# wpf mp3 mediaelement


    【解决方案1】:

    您的设置似乎是正确的,但我猜MediaElement 找不到minotaurFoot.mp3。注册MediaElementMediaFailed-Event 并检查它是否被引发。传递给方法的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。然后选择属性并进行上面的指定设置。

    【讨论】:

    • 谢谢!它说它找不到媒体文件。我的文件直接在项目中。对于图像,此位置有效。这是 mp3 的问题吗?
    • @user2602079 不,mp3 文件没有问题。您只需要更改 MediaElement 的 Source 属性。尝试使用这个/TheseusAndTheMinotaur;component/minotaurFoot.mp3。或者使用Visual Studio的PropertyWindow设置Source
    • 没有这样的运气。我还将它放在图像文件夹中,并提供与我的图像相同的文件路径,但没有运气。不过还是谢谢。
    • 可能值得注意的是 mediaElement 在网格中,当我在代码中调用它时,我不指定网格,只指定页面。这是因为网格似乎没有智能感知,但页面有。
    • 谢谢。工作得很好。最后一件事 - mediaElement 似乎需要客户端机器上的 windows 媒体播放器。这似乎很糟糕,因为有些机器可能没有它。有没有更值得我信赖的替代方案,可以在其他人的电脑上玩?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多