Dan-Levi Tømta
Dan-Levi Tømta

Reputation: 846

Problems figuring out playing wav file

The task should be simple. I have an Image with a "Tapped" even handler, and i want to play a wav file when it is clicked.

<Image x:Name="Snd_abort_1" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Source="Assets/Tiles/A/abort_1.png" Tapped="Snd_abort_1_Tapped"></Image>

private void Snd_abort_1_Tapped(object sender, TappedRoutedEventArgs e)
    {
        MediaElement myMediaElement = new MediaElement();
        myMediaElement.AutoPlay = false;
        myMediaElement.Source = new Uri("ms-appx:///Assets/Sounds/DukeNukem/abort.wav");
        myMediaElement.Play();
    }

enter image description here

For some particular reason this does not work. I tried to debug the click and that fires but no error, no sound.

I have tried both in emulator and on my Lumia 930 device.

What am i missing out?

Upvotes: 1

Views: 54

Answers (1)

Chubosaurus Software
Chubosaurus Software

Reputation: 8161

You don't need to prefix your files with ms-appx:, just do

myMediaElement.Source = new Uri("Assets/Sounds/DukeNukem/abort.wav", UriKind.Relative);
myMediaElement.Play();

Unfortunately, though it is correct. This code-behind approach will still not work. Because the myMediaElement is not part of the VisualTree (because you made it dynamically).

To fix this, just add it to the root Grid/Panel

myMediaElement.Source = new Uri("Assets/Sounds/DukeNukem/abort.wav", UriKind.Relative);
this.ContentPanel.Children.Add(myMediaElement);
myMediaElement.Play();

Now it should play nicely with everything.

Upvotes: 1

Related Questions