locked
Stop the SpeechSynthesizer when the back button pressed windows phone RRS feed

  • Question

  • I have a windows phone 8.1 Silverlight app which on one page I use SpeechSynthesizer texttospeech. This is fine but I want to stop the speech when the hardware back button is pressed, what I have done doesn't work, here is my code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using System.Windows.Media;
    using Microsoft.Phone.Tasks;
    using System.Threading.Tasks;
    using Windows.Phone.Speech.Synthesis;
    using Windows.Phone.UI.Input;
    
    
    namespace myApp
    {
        public partial class Inst : PhoneApplicationPage
        {
            SpeechSynthesizer synth = new SpeechSynthesizer();                             // The speech synthesizer (text-to-speech, TTS) object 
    
    
            public Inst()
            {
                InitializeComponent();
    
    
               HardwareButtons.BackPressed += HardwareButtons_OnBackKeyPress;
               mediaInst.AutoPlay = true;
    
                // Speak a string.
                synth.SpeakTextAsync("Welcome to the instructions.....");
                mediaInst.Loaded += MediaElement_OnLoaded;
    
             }
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
    
                base.OnNavigatedTo(e);
                HardwareButtons.BackPressed += HardwareButtons_OnBackKeyPress;
                 mediaInst.Stop();
                 synth.CancelAll();
                 
                
            }
    
            protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
            {
                HardwareButtons.BackPressed += HardwareButtons_OnBackKeyPress;
                mediaInst.AutoPlay = false;
                mediaInst.Stop();
                mediaInst.IsMuted = true;
                synth.CancelAll();
                synth.Dispose();
                
            }
    
            void  MainPage_Loaded(object sender, RoutedEventArgs e)
    
             {
    
             }
     
            private async void MediaElement_OnLoaded(object sender, RoutedEventArgs e)
    
            {
       
                MediaElement mediaInst = new MediaElement();
                mediaInst.Position = TimeSpan.Zero;
                mediaInst.Source = new Uri(@"ms-appx:///Animation/animate.mp4", UriKind.RelativeOrAbsolute);
                mediaInst.Stretch = Stretch.UniformToFill;
                mediaInst.MediaOpened += MediaElement_MediaOpened; 
               
    
            }
    
            private void MediaElement_MediaOpened(object sender, RoutedEventArgs e)
            {
               
                (sender as MediaElement).Play();
                mediaInst.MediaEnded += Media_Ended;
                
               
            }
            private void Media_Ended(object sender, EventArgs e)
            {
                mediaInst.Position = new TimeSpan(0, 0, 1);
                //mediaInst.Play();
                mediaInst.AutoPlay = false;  //true
                synth.CancelAll();
                synth.Dispose();
            }
    
           
            void HardwareButtons_OnBackKeyPress(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
            {
                synth.CancelAll();
                synth.Dispose();
                
                (sender as MediaElement).Stop();
                mediaInst.Stop();
                e.Handled = true;
                Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_OnBackKeyPress;
                (sender as MediaElement).Stop();
                mediaInst.MediaEnded += Media_Ended;
                   
                NavigationService.Navigate(new Uri("/Page3.xaml", UriKind.RelativeOrAbsolute));
               
            }
    
        }
    }
    How can I stop the speech from talking whenever the hardware back button is pressed

    Friday, September 25, 2015 8:16 AM