title: SoundEffect and SoundEffectInstance when in Windows Phone Silverlight app
body: <p>Windows Phone 7.1/7.5/Mango Silverlight app.</p>
  
  <p>I used the Sound Sample example <a href="http://msdn.microsoft.com/en-us/library/ff431744%28v=vs.92%29.aspx" rel="nofollow">here</a> to implement playing sounds in my app.
  Here's the snippet.</p>
  
  <pre><code>    //The sounds to play
      private SoundEffect coyoteSound;
      private SoundEffect birdSound;
  
      //Constructor
      public MainPage()
      {
         InitializeComponent();
         // Prime the pump or we'll get an exception.
         FrameworkDispatcher.Update();
  
         // Create and load SoundEffect objects.
         LoadSound("Resources/coyoteSound.wav", out coyoteSound);
         LoadSound("Resources/birdSound.wav", out birdSound);
      }
  
              /// &lt;summary&gt;
              /// Loads a wav file into an XNA Framework SoundEffect.
              /// &lt;/summary&gt;
              /// &lt;param name="SoundFilePath"&gt;Relative path to the wav file.&lt;/param&gt;
              /// &lt;param name="Sound"&gt;The SoundEffect to load the audio into.&lt;/param&gt;
              private void LoadSound(String SoundFilePath, out SoundEffect Sound)
              {
                  // For error checking, assume we'll fail to load the file.
                  Sound = null;
                  try
                  {
                      // Holds informations about a file stream.
                      StreamResourceInfo SoundFileInfo = App.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));
  
                      // Create the SoundEffect from the Stream
                      Sound = SoundEffect.FromStream(SoundFileInfo.Stream);
                  }
                  catch (NullReferenceException)
                  {
                      // Display an error message
                      MessageBox.Show("Couldn't load sound " + SoundFilePath);
                  }
              }
  
      //Play sound when page naivgateTo
      protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgse)    
      {
         birdSound.Play();
      }
      //Common mouseclick handler to play sound
      private void ElementClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
              {
                  FrameworkElement ele = sender as FrameworkElement;
  
                  if (ele != null)
                  {
                      switch (ele.Name)
                      {
                          case "imgcoyoteSound":
                              coyoteSound.Play();
                              break;
                          case "imgbirdSound":
                              birdSound.Play();
                              break;
                          default:
                              birdSound.Play();
                              break;
                      }
                  }
   }
  </code></pre>
  
  <p><strong>Questions:</strong></p>
  
  <p>1: I want to Stop the sound and I see I need to use SoundEffectInstance for that. What is the best way to integrate those in my above code.</p>
  
  <p>2: The sound in the OnNavigatedTo starts playing even before my page is loaded? I tried putting the play in OnLoaded but still the say. Any ideas on that?</p>
  
  <p>3: I read about being careful on Memory Usage when using SoundEffect/SoundEffectInstance. Will the above code cause any memory issue? </p>
  
  <p>4:Most of my pages (20-25) will have sound effects implementation like above. That shouldn't be an issue correct?</p>
  
  <p><strong>Update:</strong> Is is right place to call dispose? Will it affect the sound play when I go to this page using Back button?</p>
  
  <pre><code>//Play sound when page naivgateFrom
  protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgse)     
  { 
     birdSoundEffectInstance.Dispose(); 
     coyoteSoundEffectInstance.Dispose(); 
  } 
  </code></pre>