Playing sounds one at a time

Im building a windows phone 8/8.1 app which consists in playing sounds, the way im doing is once i press a button, it fires up the event and runs this code:

StreamResourceInfo info = Application.GetResourceStream(new Uri("Assets/Sound/DO_converted.wav", UriKind.Relative));
SoundEffect sound = SoundEffect.FromStream(info.Stream);
SoundEffectInstance instance = sound.CreateInstance();
instance.Play();

But if i press more then one button i have more then one sound playing at the same time. So is there anyway to stop this from happening, play one sound at a time?

Greets, José Correia


Work out what the sound effect duration is. Use a queue to queue them and have a loop that play the next one after the current one's duration.

SoundEffect has the following property:

public TimeSpan Duration { get; }

So, ive also posted this question on Windows dev forum, and this is the best solution for my problem:

MSDN Forum

      SoundEffectInstance instance;
private void Button1_Click(object sender, RoutedEventArgs e)
    {
                StreamResourceInfo info = Application.GetResourceStream(new Uri("Assets/Sound/test1.wav", UriKind.Relative));
                SoundEffect sound = SoundEffect.FromStream(info.Stream);
                if (instance != null)
                    instance.Stop();
                instance = sound.CreateInstance();
                instance.Play();
     }

    private void Button2_Click(object sender, RoutedEventArgs e)
    {
                StreamResourceInfo info = Application.GetResourceStream(new Uri("Assets/Sound/test2.wav", UriKind.Relative));
                SoundEffect sound = SoundEffect.FromStream(info.Stream);
                if (instance != null)
                    instance.Stop();
                instance = sound.CreateInstance();
                instance.Play();
    }
链接地址: http://www.djcxy.com/p/25996.html

上一篇: SoundPlayer.Stop()挂起,嗡嗡声

下一篇: 一次播放一个声音