簡體   English   中英

MediaElement無法播放mp3

[英]MediaElement won't play an 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>

在一種激發的方法中,我有:

this.myGamePlayerPage.footStep.Play();

沒有聲音播放,但沒有錯誤。 任何想法為什么會這樣? 謝謝。

編輯:我將源更改為此: Source="C:\\newnew\\TheseusAndTheMinotaur\\minotaurFoot.mp3"並且它可以工作。 但這不好。 它不適用於其他占用者。

您的設置似乎是正確的,但是我想MediaElement無法找到minotaurFoot.mp3 注冊MediaElementMediaFailed並檢查它是否被引發。 傳遞給該方法的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 newerCopy to Output directory

在項目中選擇mp3文件,右鍵單擊以打開ContextMenu。 然后選擇屬性並在上面進行指定的設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM