0

In my windows phone 7 app I'm using the following code to navigate to a page

 private void button2_Click(object sender, RoutedEventArgs e)
        {
           NavigationService.Navigate(new Uri("/Views/Game.xaml, UriKind.RelativeOrAbsolute));
        }

In the OnNavigatedTo event of Game.xaml.cs I use the following code to play two wav files one after another. In order for me to prevent both audio play at the same time, I use while loop to wait until the first audio is finish before the second file is played. I believe this is what causing the first audio to play before the game page is displayed. Is there a better way of doing this?

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{            
    playAudio("intro.wav");
    while (MySoundEffect.State == SoundState.Playing)
    {
    }

    playAudio("firstQuestion.wav");

    base.OnNavigatedTo(e);                 
}

The following is the code for playAudio.

 protected void playAudio(string fileName)
        {
            Stream stream = TitleContainer.OpenStream("audio/" + fileName);
            SoundEffect effect = SoundEffect.FromStream(stream);
            MySoundEffect = effect.CreateInstance();
            FrameworkDispatcher.Update();
            MySoundEffect.Play();
        }

MySoundEffect is a property of the Game class.

private SoundEffectInstance _MySoundEffect;

        public SoundEffectInstance MySoundEffect
        {
            get { return _MySoundEffect; }
            set { _MySoundEffect = value; }
        }

Whenever user click the button, the sound

1 Answer 1

1

To answer your question in the title, you could try the same with the Loaded event handler instead of the OnNavigateTo. This way, your audio files will start playing only after the page is loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.