Question

I am creating an app using c# and xaml where i have 20 pages each page has some character images and when i tap on that image some dialogues should pop up.so i have taken a MediaElement and made it global like this:

App.xaml:

<MediaElement x:Key="StorySound" 
                  Volume="1"
                  AutoPlay="True"/>

App.xaml.cs:

public static MediaElement StorySound
    {
        get { return Current.Resources["StorySound"] as MediaElement; }
    }

And on every page on tap event i have written this code:

App.StorySound.Source = new Uri("/Sounds/Dialogues/" + textblock.Text + ".mp3", UriKind.Relative);
App.StorySound.MediaOpened+=StorySound_MediaOpened;

void StorySound_MediaOpened(object sender, RoutedEventArgs e)
    {         
            App.StorySound.Play();            
    } 

This plays the sound on not more than 2 pages when i navigate to the 3rd page using the next button which i have created no sound plays but when i close the app and directly open the 3rd page the sound plays.I have even tested this by using MessageBox to show the current state of the mediaelement i have found that on the first two pages the current state is "Opening" and the sound plays but on the third page the current state is "Closed" so the sound does not Play.is there any memory issue when playing sounds using MediaElement.I cannot use SoundEffect Because all my sound files are .mp3 and if i use soundeffect i will have to convert these sounds in .wav which will increase the size of my app because i have more than 50 sound files.

Was it helpful?

Solution

If I understand you correctly you are hooking the MediaOpened-event on the global StorySound-object on each page but you never seem to unhook it. I would suggest you to either:

  • use a local MediaElement on each page that you start (and perhaps stop), or
  • make sure you unhook all events on the StorySound when you navigate (so that each page is the only "user" of this global resource).

I am sure it will be fine to have may mp3's in your app; you should not need to convert them to wav.

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