Sound C# Metro RRS feed

  • Question

  • When my main page opens up for my app, I need it to play a sound (no problem there), the sound needs to keep playing when frames switch(again, no problem with that)

    My problem arises when I switch from another frame to my mainpage, when I do that, it will start playing the same sound on top of the old one

            private async void PlaySound()
                if (soundElement.AudioStreamCount == 0)
                    var file =
                        (await Package.Current.InstalledLocation.GetFolderAsync("Common")).GetFileAsync("Opening Theme.mp3");
                    var stream = await file.OpenAsync(FileAccessMode.Read);
                    this.soundElement.SetSource(stream, file.ContentType);
                    this.soundElement.IsLooping = true;
    I call this method in the MainPage initializer, I believe the problem is the check that I am doing at the beginning of the method, but I can't find a static MediaElement method that will tell me if any sound is playing when the method is called

    Wednesday, August 7, 2013 3:37 PM


  • Do you have multiple MediaElements playing sounds? There is no static method to say if anybody is playing a sound, but you can tell if a specific MediaElement is playing by looking at the CurrentState property.

    Typically to have continuous sound you would have a MediaElement on a constant main page that the app doesn't move away from. The content pages would be hosted in a sub-Frame on that page.

    The sample code you show should cause duplicate sounds on its own. If anything I would expect it not to play anything, since the Play method is called before the SetSource has finished loading the sound. Instead, call play after the CurrentStateChanged event fired that the source was ready or set the AutoPlay property to true.


    • Proposed as answer by Anne Jing Saturday, August 10, 2013 12:57 PM
    • Marked as answer by Anne Jing Wednesday, August 14, 2013 1:24 AM
    Thursday, August 8, 2013 2:38 AM