James Jeffery
James Jeffery

Reputation: 557

Playing Soundbite in WP8 App

I'm trying to write a WP8 app that plays a short sound when a button is pressed, but I cannot seem to figure out how to play the sound. Here's a quick example of my code:

XAML

<Rectangle x:Name="Rect1" Grid.Row="0" Grid.Column="0" Tap="RectTapped" Fill="White" />

App.cs

private void RectTapped(object sender, System.Windows.Input.GestureEventArgs e)
{
    MediaElement sound = new MediaElement();
    sound.AutoPlay = false;
    sound.Source = new Uri("Assets\\Sounds\\bark-1.wav", UriKind.Relative);
    sound.Play();
}

When testing on my Nokie 820 device no sound plays. I can't understand why.

Is there something I'm doing wrong? The .wav is in my resources.

I've read that MediaElement shouldn't be used for this task. I've tried using the SoundEffect class in Xna.Framework.Audio; following the example from MSDN but that also fails because I couldn't use Content.Load as Load was not an available method of the Content class.

I've also looked at XAudio2, but as I do not know C++ I can't get my head around the examples.

Upvotes: 2

Views: 1193

Answers (5)

Shweta Goyal
Shweta Goyal

Reputation: 94

Remove the line

sound.AutoPlay=false;

or change it to:

sound.AutoPlay = true; 

it will work then.

Upvotes: 0

waghekapil
waghekapil

Reputation: 321

For Windows Phone 8.1 Silverlight, take the first two answers (from @DenDelimarsky and @onmyway133) to play the sound.

Without adding MediaElement to XAML sound not played in my case.

Use below code to play sound:

private void YourClickHandler(object sender, RoutedEventArgs e)
        {
            MediaElement myMediaElement = new MediaElement();
            myMediaElement.Source = new Uri("/MP3/AirHornTwoBlows.mp3", UriKind.Relative);
            LayoutRoot.Children.Add(myMediaElement);
            myMediaElement.Play();
        }

Upvotes: 0

onmyway133
onmyway133

Reputation: 48185

You need to add MediaElement to your XAML tree

this.LayoutRoot.Children.Add(sound);

Upvotes: 1

rikkit
rikkit

Reputation: 1137

You should be using the XNA SoundEffect class instead of the MediaElement because, well, you're playing sound effects.

The documentation is bad for this area, but this is how you do it:

effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();

so to play a soundeffect from your app package:

var stream = Application.GetResourceStream(filepath);
effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();

The advantage over MediaElement is that the SoundEffect does not need to be in the visual tree (and also it doesn't screw up the background audio player). The disadvantage is that you have none of the (sometimes useful) events that you have on MediaElement.

Upvotes: 0

Den
Den

Reputation: 16826

Instead of a Rectangle, use a Button. Also, MediaElement is good for playing short sounds in Silverlight applications. Make sure that the control is a part of your visual tree (add it in XAML). Then bind to the button Click event handler:

private void YourClickHandler(object sender, RoutedEventArgs e)
{
   myMediaElement.Source = new Uri("/Assets/Sounds/bark-1.wav", UriKind.Relative);
   myMediaElement.Play();
}

Upvotes: 0

Related Questions