title: Sound won't play in user control but will in my main page - Windows Phone
body: <p>I have a .wav file sound effect in my app. For some reason it plays correctly in one place (a normal phone page), but when used elsewhere (within a usercontrol) it doesn't play (although I am using the same code and the code <em>is</em> executing).</p>
  
  <p>And oddly (at least to me :) ) if I put a breakpoint before the sound is to play, then click Continue the sound will actually play in the emulator, which makes me think I need to do something extra to get my usercontrol to play sound correctly (since the same code works elsewhere).</p>
  
  <p>I set up my sound in XAML like this:</p>
  
  <pre><code>&lt;MediaElement x:Name="StampSound"
               Source="/Sounds/stamp.wav"
               Volume="1"                          
               AutoPlay="False" /&gt;
  </code></pre>
  
  <p>I use the same code in the XAML page and my user control to set up the sound. Then when I want my sound to play I use the following:</p>
  
  <pre><code>  StampSound.Play()
  </code></pre>
  
  <p>This works correctly in my main page, but in my user control it does not make a sound (but the code is executing, and if I put a breakpoint in and then continue the sound WILL play).</p>
  
  <p>So I think my code is OK, but perhaps I'm missing something in my user control. I try to play the sound in the UserControl_Loaded sub, where I also (successfully) implemented an animation to go with the audio (incidentally an identical animation to the one the coincides with the sound in my main page).</p>
  
  <p>Is there some timing type issue I'm ignorant of here? I'd appreciate any help or ideas.</p>
  
  <p>This is for Windows Phone 8 XAML + VB.net, but I can manage with C# code.</p>
  
  <p>EDIT: FOUND THE ANSWER</p>
  
  <p>OK, I found the answer as I was about to post this, but the answer was for a slightly different question, so I thought I'd answer this in case someone else comes across it.</p>
  
  <p>(answer from <a href="https://stackoverflow.com/questions/14335987/wp7-playing-mediaelement-after-page-is-loaded">enter link description here</a></p>
  
  <p>I was apparently trying to play the sound before it was loaded, so I just added a handler for when the sound finished loading and played the sound in there:</p>
  
  <pre><code>Private Sub StampSound2_MediaOpened(sender As Object, e As RoutedEventArgs) Handles StampSound2.MediaOpened
      StampSound2.Play()
  End Sub
  </code></pre>
  
  <p>This works for me as I need to play the sound ASAP (i.e. it should ALWAYS play as soon as the control is loaded).</p>