Pergunta

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.

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top