Thursday 2 May 2013

Windows Store App - Audio Playback across all pages


Add the following in MainPage.xaml

<Frame x:Name="frame1" />
        <MediaElement Name="media"
              AudioCategory="BackgroundCapableMedia"
              Source="/Sounds/bgmusic_v9.mp3" Loaded="OnMediaLoaded"/>

Add the following in MainPage.xaml.cs

private void OnMediaLoaded(object sender, RoutedEventArgs e)
    {
            if (App.appVariables.GlobalAudioElement == null)
              App.appVariables.GlobalAudioElement = sender as MediaElement;
    }



Add a static variable GlobalAudioElement.
public  MediaElement GlobalAudioElement = null;



Add the code in the required pages in ur app

        /// <summary>
        /// Mute the sound and displays the unmute button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnMuteClick(object sender, RoutedEventArgs e)
        {
            App.appVariables.Mute = true;
            App.appVariables.GlobalAudioElement.Pause();
            btnMute.Visibility = Visibility.Collapsed;
            btnUnMute.Visibility = Visibility.Visible;
        }
        /// <summary>
        /// Unmute the sound and displays the mute button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnUnMuteClick(object sender, RoutedEventArgs e)
        {
            App.appVariables.Mute = false;
            App.appVariables.GlobalAudioElement.Play();
            btnMute.Visibility = Visibility.Visible;
            btnUnMute.Visibility = Visibility.Collapsed;
        }


No comments:

Post a Comment