0

i have a page transition control in the MainWindow ( theres few buttons and a home button that leads to the homepage), so i click a button on the MainWindow that brings a user control page inside the page transition within the MainWindow , theres an audio to be played in that user control page , ok fine but when i click the home button to show the homepage , the audio from that user control page is still playing . How can i stop the user control page from running in the background?

this is how i call the user control page into the page transition control in the mainwindow :

    private void button2_Click(object sender, RoutedEventArgs e) // Story
    {
        Story page = new Story();
        pageTransition1.IsEnabled = true;
        pageTransition1.Visibility = System.Windows.Visibility.Visible;
        grid1.Visibility = System.Windows.Visibility.Hidden;
        pageTransition1.ShowPage(page);
    }


    private void button5_Click(object sender, RoutedEventArgs e) // Home Button
    {
        pageTransition1.Visibility = System.Windows.Visibility.Hidden;
        pageTransition1.IsEnabled = false;
        grid1.Visibility = System.Windows.Visibility.Visible;
    }

Basically i just want the user control page to stop running ( cos its running in the background) when i click the home button.

How i play my sound in my user control page ::

            mediaElement1.LoadedBehavior = MediaState.Manual;
        mediaElement1.Source = new Uri(audioNames[iCurrentImageCount], UriKind.RelativeOrAbsolute);
        mediaElement1.Play();

and

     MediaPlayer ap = new MediaPlayer();
        recordedaudio = System.IO.Directory.GetFiles(@"../../Audio/", "*.wav");
        if (recordedaudio == null)
        { MessageBox.Show("No Recorded Files!"); }
        else
        {
            ap.Open(new Uri(recordedaudio[iCurrentImageCount], UriKind.RelativeOrAbsolute));
            ap.Play();
        }
user2376998
  • 1,061
  • 7
  • 32
  • 65

1 Answers1

0

One solution is to handle the Unloaded event in the user control page and stop playing the media.

Vasanth Sriram
  • 1,285
  • 12
  • 19
  • private void Grid_Unloaded(object sender, RoutedEventArgs e) { ap.Stop(); ap.Close(); } This code will work only if you are loading and unloading the page. If you are just changing the visibility, use IsVisibleChanged event. – Vasanth Sriram Jul 19 '13 at 09:05
  • so its something like if UserControlPage_IsVisibleChanged( object sender , RoutedEventArgs e) ? – user2376998 Jul 21 '13 at 08:48