Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

play two sounds simultaneously in Windows 8 Metro App (C#/XAML)

I a Windows 8 Metro App (C#/XAML): How can I trigger the same sound effect twice so that it plays simultaneously.

The second play should start before the first has finished.

Related questions are:

Playing two sounds simultaneously c# and Play two sounds simultaneusly

I found this class for XNA which does what I want, but is not available under Metro: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.audio.soundeffectinstance.aspx

like image 433
Cilvic Avatar asked Jun 09 '12 12:06

Cilvic


1 Answers

Just create a separate media element for every sound effect.

(not sure I need to relocate the file every time)

  public async void PlayLaserSound()
    {
        var package = Windows.ApplicationModel.Package.Current;
        var installedLocation = package.InstalledLocation;
        var storageFile = await installedLocation.GetFileAsync("Assets\\Sounds\\lazer.mp3");
        if (storageFile != null)
        {
            var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
            MediaElement snd = new MediaElement();
            snd.SetSource(stream, storageFile.ContentType);
            snd.Play();
        }
    }
like image 119
Cilvic Avatar answered Oct 21 '22 21:10

Cilvic