Question

On my windows phone app, I want to play a short .wav audio, similar to windows when it boots up. At first I tried using an event handler when one of my controls was loaded (this worked about 60% of the time, which is very interesting, maybe someone can clear that up as well, I'm thinking it has to do with the order things happen to load. That's why its different each time running it). I'm using visual studios 2012 ultimate, this is my xaml code for the .wav file:

<MediaElement x:Name="MySound"
                          Source="/quantum_drive.wav"
                          Volume="1"
                          AutoPlay="false"
                          />

I've also just tried to call this method when the main page loads:

private void MainPage1_Loaded(object sender, RoutedEventArgs e)
    {
        MySound.Play();
    }

For some reason this only works about 60% of the time(seemingly randomly), why is this?! This seems like a common thing to want to do in a phone app, yet I cannot find any information on this on stackoverflow or google searches.

Was it helpful?

Solution

Don't use the media element to do what you are trying to do. It is to buggy for sound effects. It is meant more for user interaction media.

Instead, do the following (you can the full article here)

using Microsoft.Xna.Framework.Audio;

using Microsoft.Xna.Framework.Media;

using Microsoft.Xna.Framework;

static Stream stream1 = TitleContainer.OpenStream("soundeffect.wav");

static SoundEffect sfx = SoundEffect.FromStream(stream1);

static SoundEffectInstance soundEffect = sfx.CreateInstance();

Now just invoke play sound from your loaded method

public void playSound(){

    FrameworkDispatcher.Update();

    soundEffect.Play();

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top