[C#]尝试在页面加载循环时停止连续循环并停止文本到语音 [英] [C#]Trying to stop the continuous loop and stop text to speech while looping on page load

查看:79
本文介绍了[C#]尝试在页面加载循环时停止连续循环并停止文本到语音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试停止连续循环,并在页面加载时循环停止文本到语音。



详细问题



在页面加载时,字母表自动从a到z开始。



问题需要解决 



1.如果我们点击"音量"图标文本到语音应停止。

2.如果我们点击"暂停"图标,它应该更改为播放图标并停止连续循环,如果我们点击"播放"图标它应该开始连续播放直至"z"字母表或按照您的意愿连续重复。



XAML代码



               

< Page

  &NBSP; x:Class =" ABCD_kids.ABCD"

  &NBSP; xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  &NBSP; xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"

  &NBSP; xmlns:local =" using:ABCD_kids"

  &NBSP; xmlns:d =" http://schemas.microsoft.com/expression/blend/2008"

  &NBSP; xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"

  &NBSP; mc:Ignorable =" d">



  &NBSP; < Grid Name =" ABCDHome">

  &NBSP; &NBSP; &NBSP; < Grid.Background>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < ImageBrush Stretch =" Fill" ImageSource =" ms-appx:///ABCD/AbcdMain.png" />

  &NBSP; &NBSP; &NBSP; < /Grid.Background>

  &NBSP; &NBSP; &NBSP; < RelativePanel x:Name =" HomePanel" Grid.Row = QUOT; 0" Canvas.ZIndex =" 1">

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < Image x:Name =" Home"来源=" ABCD / Home.png"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.AlignLeftWithPanel =" True"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;保证金= QUOT; 10,10,0,0" Tapped =" HomePage"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;宽度=" 40"高度="40"/>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < Image x:Name =" Prev"来源=" ABCD / Previous.png"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.AlignVerticalCenterWithPanel =" True"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   Tapped =" Prevevent"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;宽度=" 40"高度="40"/>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < Image x:Name =" Fwd"来源=" ABCD / Forward.png"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.AlignVerticalCenterWithPanel =" True"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.RightOf =" Prev"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  保证金=" 280,0,0,0"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   Tapped =" FwdEvent"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;宽度=" 40"高度="40"/>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < Image x:Name =" Music"来源=" ABCD / Music_active.png"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; RelativePanel.AlignBottomWithPanel = QUOT;真" &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;余量= QUOT; 69,0,0,0" Tapped =" MusicTap"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;宽度=" 40"高度="40"/>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < Image x:Name =" Pause"来源=" ABCD / Pause.png"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.AlignBottomWithPanel =" True"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.RightOf =" Music"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;保证金= QUOT; 17,0,0,0" Tapped =" PauseTap"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;宽度=" 40"高度="40"/>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < Image x:Name =" Sound"来源=" ABCD / sound_active.png"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.AlignBottomWithPanel =" True"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; RelativePanel.RightOf = QUOT;暂停" Tapped =" SoundTap"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  保证金=" 17,0,0,0"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;宽度=" 40"高度="40"/>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < Image x:Name =" Reload"来源=" ABCD / Reload.png"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.AlignBottomWithPanel =" True"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.RightOf =" Sound"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;保证金= QUOT; 17,0,0,0" Tapped =" ReloadTap"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;宽度=" 40"高度="40"/>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < Image x:Name =" Alph_cap"来源=" ABCD / Cap_alpha / Cap_0.png"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.AlignLeftWithPanel =" True" 

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.Below =" Home"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  保证金=" 10,0,0,0"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;宽度=" 100"高度="100"/>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < TextBlock x:Name =" CapAlphaName" &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Text =" Ant"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; FontFamily =" Font / VAG Round.TTF#VAG Round"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.AlignTopWithPanel =" True"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.RightOf =" Alph_cap"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   Foreground =" BlueViolet"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;保证金= QUOT; 10,90,0,0"字号= QUOT; 39" FontStretch =" UltraCondensed">

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < / TextBlock>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < Image x:Name =" Alph_small"来源=" ABCD / Cap_alpha / Sml_0.png"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.AlignRightWithPanel =" True" 

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; RelativePanel.Above = QUOT;正向" &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;宽度=" 80 QUOT;高度="80"/>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < TextBlock x:Name =" SmallAlphaName" &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Text =" ant"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; FontFamily =" Font / VAG Round.TTF#VAG Round"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.AlignRightWithPanel =" True"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   RelativePanel.LeftOf =" Alph_cap"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   Foreground =" BlueViolet"

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;保证金= QUOT; 0,242,75,0"字号= QUOT; 39" FontStretch =" UltraCondensed">

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < / TextBlock>



  &NBSP; &NBSP; &NBSP; < / RelativePanel>

  &NBSP; &NBSP; &NBSP; < MediaElement x:Name =" mediaABCD"源= QUOT; /Video/abcd.mp3"体积= QUOT; 0.4"   />

  &NBSP; &NBSP; &NBSP; < MediaElement x:Name =" textToSpeech"体积= QUOT; 0.9" />

  &NBSP; < / Grid>



< / Page>



c#code

$
使用System;

使用System.Collections.Generic;

使用System.IO;

使用System.Linq;

使用System.Runtime.InteropServices.WindowsRuntime;

使用System.Threading.Tasks;

使用Windows.Foundation;

使用Windows.Foundation.Collections;

使用Windows.Media.SpeechSynthesis;

使用Windows.UI.Xaml;

使用Windows.UI.Xaml.Controls;

使用Windows.UI.Xaml.Controls.Primitives;

使用Windows.UI.Xaml.Data;

使用Windows.UI.Xaml.Input;

使用Windows.UI.Xaml.Media;

使用Windows.UI.Xaml.Media.Imaging;

使用Windows.UI.Xaml.Navigation;



//空白页面项目模板记录在http://go.microsoft .com / fwlink /?LinkId = 234238



命名空间ABCD_kids

{

  &NBSP; ///< summary>

  &NBSP; ///可以单独使用或导航到框架内的空白页面。

  &NBSP; ///< / summary>

  &NBSP;公开密封的分类ABCD:页面
  &NBSP; {

  &NBSP; &NBSP; &NBSP; private bool _isPaused = false;

  &NBSP; &NBSP; &NBSP; int currentIndex = 0; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP;公开ABCD()

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.InitializeComponent();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; DoLooping();

  &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; private void PauseTap(object sender,TappedRoutedEventArgs e)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _isPaused =!_isPaused; //这将允许暂停按钮充当切换

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(_isPaused)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Pause.Source = new BitmapImage(new Uri(" ms-appx:///ABCD/Play.png")); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;否则

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Pause.Source = new BitmapImage(new Uri(" ms-appx:///ABCD/Pause.png")); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; private async void DoLooping()

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //这将永远循环

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // while(true)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(!_isPaused)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if((Pause.Source as BitmapImage).UriSource == new Uri(" ms-appx:///ABCD/Pause.png"" ;,UriKind.Absolute))

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; for(int currentIndex = 0; currentIndex< 26; currentIndex ++)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(currentIndex> 25)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; currentIndex = 25;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;返回;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Alph_cap.Source = new BitmapImage(new Uri(" ms-appx:/// ABCD / Cap_alpha / Cap_" + currentIndex +" .png"));

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Alph_small.Source = new BitmapImage(new Uri(" ms-appx:/// ABCD / Cap_alpha / Sml_" + currentIndex +" .png"));

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; CapAlphaName.Text = CapsAlpha [currentIndex];

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; SmallAlphaName.Text = SmallAlpha [currentIndex];

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var speechText = this.CapAlphaName.Text;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(speechText!="")

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var synth = new SpeechSynthesizer();



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var speechStream = await synth.SynthesizeTextToStreamAsync(speechText);



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.textToSpeech.AutoPlay = true;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.textToSpeech.SetSource(speechStream,speechStream.ContentType);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.textToSpeech.Play();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Task.Delay(1000).Wait();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; currentIndex = 25;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;否则

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

$
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; currentIndex = 25;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }  &NBSP; &NBSP; &NBSP;  

  &NBSP; &NBSP; &NBSP; &NBSP;  



  &NBSP; &NBSP; &NBSP; string [] CapsAlpha = new string [] {" Ant"," Bear"," Cat"," Dog"," Elephant"," Frog"," Giraffe"," Hippo", "冰淇淋",​​"水母","b b","风筝","狮子","鼠标","巢","猫头鹰","鹦鹉","女王"等。 ,"兔","羊","玩具","伞和QUOT;,"雕","观看","透视","牦牛","斑马"
};

  &NBSP; &NBSP; &NBSP; string [] SmallAlpha = new string [] {" ant","bear","cat","dog","elephant","frog","giraffe","hippo", "ice-cream","jellyfish",
"kite","lion","mouse","nest","owl","parrot","queen"等。 ,"兔","羊","玩具","伞状QUOT;,"鹫","钟表",英寸×射线","牦牛","斑马"};

  &NBSP; &NBSP;   

  &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; private async void Prevevent(object sender,TappedRoutedEventArgs e)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; currentIndex--;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(currentIndex< 0)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; currentIndex = 0;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;返回;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Alph_cap.Source = new BitmapImage(new Uri(" ms-appx:/// ABCD / Cap_alpha / Cap_" + currentIndex +" .png"));

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Alph_small.Source = new BitmapImage(new Uri(" ms-appx:/// ABCD / Cap_alpha / Sml_" + currentIndex +" .png")); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; CapAlphaName.Text = CapsAlpha [currentIndex];

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; SmallAlphaName.Text = SmallAlpha [currentIndex];

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var speechText = this.CapAlphaName.Text;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(speechText!="")

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var synth = new SpeechSynthesizer();



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var speechStream = await synth.SynthesizeTextToStreamAsync(speechText);



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.textToSpeech.AutoPlay = true;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.textToSpeech.SetSource(speechStream,speechStream.ContentType);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.textToSpeech.Play();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP;  

  &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; private async void  FwdEvent(object sender,TappedRoutedEventArgs e)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; currentIndex ++;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(currentIndex> 25)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; currentIndex = 25;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;返回;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Alph_cap.Source = new BitmapImage(new Uri(" ms-appx:/// ABCD / Cap_alpha / Cap_" + currentIndex +" .png"));

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Alph_small.Source = new BitmapImage(new Uri(" ms-appx:/// ABCD / Cap_alpha / Sml_" + currentIndex +" .png"));

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; CapAlphaName.Text = CapsAlpha [currentIndex];

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; SmallAlphaName.Text = SmallAlpha [currentIndex];

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var speechText = this.CapAlphaName.Text;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(speechText!="")

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var synth = new SpeechSynthesizer();



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var speechStream = await synth.SynthesizeTextToStreamAsync(speechText);



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.textToSpeech.AutoPlay = true;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.textToSpeech.SetSource(speechStream,speechStream.ContentType);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.textToSpeech.Play();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; private void MusicTap(object sender,TappedRoutedEventArgs e)

  &NBSP; &NBSP; &NBSP; {  &NBSP; &NBSP; &NBSP; &NBSP;  

  &NBSP; &NBSP; &NBSP; &NBSP;   if((Music.Source as BitmapImage).UriSource == new Uri(" ms-appx:///ABCD/Music_active.png"" ;,UriKind.Absolute))

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Music.Source = new BitmapImage(new Uri(" ms-appx:///ABCD/Music_mute.png"));

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; mediaABCD.Stop();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;否则

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Music.Source = new BitmapImage(new Uri(" ms-appx:///ABCD/Music_active.png"));

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; mediaABCD.Play();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; private bool IsTTSEnabled = false;

  &NBSP; &NBSP; &NBSP; private void SoundTap(object sender,TappedRoutedEventArgs e)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; IsTTSEnabled =!IsTTSEnabled;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(IsTTSEnabled)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Sound.Source = new BitmapImage(new Uri(" ms-appx:///ABCD/Sound_mute.png")); &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; textToSpeech.Stop();
$


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;否则

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Sound.Source = new BitmapImage(new Uri(" ms-appx:///ABCD/Sound_active.png"));

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; textToSpeech.Play();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; private async void ReloadTap(object sender,TappedRoutedEventArgs e)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var speechText = this.CapAlphaName.Text;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(speechText!="")

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var synth = new SpeechSynthesizer();



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var speechStream = await synth.SynthesizeTextToStreamAsync(speechText);



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.textToSpeech.AutoPlay = true;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.textToSpeech.SetSource(speechStream,speechStream.ContentType);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.textToSpeech.Play();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; private void HomePage(object sender,TappedRoutedEventArgs e)

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.Frame.Navigate(typeof(MainPage));

  &NBSP; &NBSP; &NBSP; }


  &NBSP;  

  &NBSP; }
}

参考图片

i'm trying to stop the continuous loop and stop text to speech while looping on page load.

Issue in details

On page load the alphabet automatically started from a to z .

Issue need to be resolved 

1.If we click on "volume" icon the text to speech should be stopped.
2.if we click on 'pause ' icon it should change to play icon and stop the continuous loop and again if we click on 'play' icon it should start playing continuously and goes till "z" alphabet or repeat continuously as your wish.

XAML code

               
<Page
    x:Class="ABCD_kids.ABCD"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ABCD_kids"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Name="ABCDHome">
        <Grid.Background>
            <ImageBrush Stretch="Fill" ImageSource="ms-appx:///ABCD/AbcdMain.png"/>
        </Grid.Background>
        <RelativePanel x:Name="HomePanel" Grid.Row="0" Canvas.ZIndex="1">
            <Image x:Name="Home" Source="ABCD/Home.png"
               RelativePanel.AlignLeftWithPanel="True"
                   Margin="10,10,0,0" Tapped="HomePage"
               Width="40" Height="40"/>
            <Image x:Name="Prev" Source="ABCD/Previous.png"
               RelativePanel.AlignVerticalCenterWithPanel="True"
               Tapped="Prevevent"
               Width="40" Height="40"/>
            <Image x:Name="Fwd" Source="ABCD/Forward.png"
               RelativePanel.AlignVerticalCenterWithPanel="True"
                   RelativePanel.RightOf="Prev"
                   Margin="280,0,0,0"
                   Tapped="FwdEvent"
               Width="40" Height="40"/>
            <Image x:Name="Music" Source="ABCD/Music_active.png"
               RelativePanel.AlignBottomWithPanel="True"                   
                Margin="69,0,0,0" Tapped="MusicTap"
               Width="40" Height="40"/>
            <Image x:Name="Pause" Source="ABCD/Pause.png"
               RelativePanel.AlignBottomWithPanel="True"
                   RelativePanel.RightOf="Music"
                   Margin="17,0,0,0" Tapped="PauseTap"
               Width="40" Height="40"/>
            <Image x:Name="Sound" Source="ABCD/sound_active.png"
               RelativePanel.AlignBottomWithPanel="True"
                   RelativePanel.RightOf="Pause" Tapped="SoundTap"
                   Margin="17,0,0,0"
               Width="40" Height="40"/>
            <Image x:Name="Reload" Source="ABCD/Reload.png"
               RelativePanel.AlignBottomWithPanel="True"
                   RelativePanel.RightOf="Sound"
                   Margin="17,0,0,0" Tapped="ReloadTap"
               Width="40" Height="40"/>
            <Image x:Name="Alph_cap" Source="ABCD/Cap_alpha/Cap_0.png"
               RelativePanel.AlignLeftWithPanel="True" 
                   RelativePanel.Below="Home"
                   Margin="10,0,0,0"
               Width="100" Height="100"/>
            <TextBlock x:Name="CapAlphaName"                
                Text="Ant"
                FontFamily="Font/VAG Round.TTF#VAG Round"
               RelativePanel.AlignTopWithPanel="True"
               RelativePanel.RightOf="Alph_cap"
               Foreground="BlueViolet"
               Margin="10,90,0,0" FontSize="39" FontStretch="UltraCondensed">
            </TextBlock>
            <Image x:Name="Alph_small" Source="ABCD/Cap_alpha/Sml_0.png"
               RelativePanel.AlignRightWithPanel="True" 
                   RelativePanel.Above="Fwd"                   
               Width="80" Height="80"/>
            <TextBlock x:Name="SmallAlphaName"                
                Text="ant"
                FontFamily="Font/VAG Round.TTF#VAG Round"
               RelativePanel.AlignRightWithPanel="True"
               RelativePanel.LeftOf="Alph_cap"
               Foreground="BlueViolet"
               Margin="0,242,75,0" FontSize="39" FontStretch="UltraCondensed">
            </TextBlock>

        </RelativePanel>
        <MediaElement x:Name="mediaABCD" Source="/Video/abcd.mp3" Volume="0.4"  />
        <MediaElement x:Name="textToSpeech" Volume="0.9" />
    </Grid>

</Page>

c# code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.SpeechSynthesis;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace ABCD_kids
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class ABCD : Page
    {
        private bool _isPaused = false;
        int currentIndex = 0;       
        public ABCD()
        {
            this.InitializeComponent();
            DoLooping();
        }

        private void PauseTap(object sender, TappedRoutedEventArgs e)
        {
            _isPaused = !_isPaused; // This will allow the pause button to act as a toggle
            if (_isPaused)
            {
                Pause.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Play.png"));            

            }
            else
            {
                Pause.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Pause.png"));                
            }
        }
        private async void DoLooping()
        {
            // This will keep looping forever
            //while (true)
            //{
                if (!_isPaused)
                {
                    if ((Pause.Source as BitmapImage).UriSource == new Uri("ms-appx:///ABCD/Pause.png", UriKind.Absolute))
                    {
                        for (int currentIndex = 0; currentIndex < 26; currentIndex++)
                        {
                            if (currentIndex > 25)
                            {
                                currentIndex = 25;
                                return;
                            }
                            Alph_cap.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Cap_alpha/Cap_" + currentIndex + ".png"));
                            Alph_small.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Cap_alpha/Sml_" + currentIndex + ".png"));
                            CapAlphaName.Text = CapsAlpha[currentIndex];
                            SmallAlphaName.Text = SmallAlpha[currentIndex];
                            var speechText = this.CapAlphaName.Text;
                            if (speechText != "")
                            {
                                var synth = new SpeechSynthesizer();

                                var speechStream = await synth.SynthesizeTextToStreamAsync(speechText);

                                this.textToSpeech.AutoPlay = true;
                                this.textToSpeech.SetSource(speechStream, speechStream.ContentType);
                                this.textToSpeech.Play();
                            }
                            Task.Delay(1000).Wait();
                        }
                    currentIndex = 25;
                }
                    else
                    {

                    }
                currentIndex = 25;
                }
            }          
           

        string[] CapsAlpha = new string[] { "Ant", "Bear", "Cat", "Dog", "Elephant","Frog", "Giraffe", "Hippo", "Ice-Cream", "Jellyfish", "Kite", "Lion","Mouse","Nest","Owl","Parrot","Queen","Rabbit","Sheep","Toy","Umbrella","Vulture","Watch","X-ray","Yak","Zebra" };
        string[] SmallAlpha = new string[] { "ant", "bear", "cat", "dog", "elephant","frog", "giraffe", "hippo", "ice-cream", "jellyfish", "kite", "lion","mouse","nest", "owl","parrot","queen","rabbit","sheep","toy","umbrella","vulture","watch","x-ray","yak","zebra"};
        
        
        private async void Prevevent(object sender, TappedRoutedEventArgs e)
        {
            currentIndex--;
            if (currentIndex < 0)
            {
                currentIndex = 0;
                return;
            }
            Alph_cap.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Cap_alpha/Cap_" + currentIndex + ".png"));
            Alph_small.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Cap_alpha/Sml_" + currentIndex + ".png"));            
            CapAlphaName.Text = CapsAlpha[currentIndex];
            SmallAlphaName.Text = SmallAlpha[currentIndex];
            var speechText = this.CapAlphaName.Text;
            if (speechText != "")
            {
                var synth = new SpeechSynthesizer();

                var speechStream =await synth.SynthesizeTextToStreamAsync(speechText);

                this.textToSpeech.AutoPlay = true;
                this.textToSpeech.SetSource(speechStream, speechStream.ContentType);
                this.textToSpeech.Play();
            }
           
        }
        private async void  FwdEvent(object sender, TappedRoutedEventArgs e)
        {
            currentIndex++;
            if (currentIndex > 25)
            {
                currentIndex = 25;
                return;
            }
            Alph_cap.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Cap_alpha/Cap_" + currentIndex + ".png"));
            Alph_small.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Cap_alpha/Sml_" + currentIndex + ".png"));
            CapAlphaName.Text = CapsAlpha[currentIndex];
            SmallAlphaName.Text = SmallAlpha[currentIndex];
            var speechText = this.CapAlphaName.Text;
            if (speechText != "")
            {
                var synth = new SpeechSynthesizer();

                var speechStream =await synth.SynthesizeTextToStreamAsync(speechText);

                this.textToSpeech.AutoPlay = true;
                this.textToSpeech.SetSource(speechStream, speechStream.ContentType);
                this.textToSpeech.Play();
            }
        }

        private void MusicTap(object sender, TappedRoutedEventArgs e)
        {            
           if((Music.Source as BitmapImage).UriSource == new Uri("ms-appx:///ABCD/Music_active.png", UriKind.Absolute))
            {
                Music.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Music_mute.png"));
                mediaABCD.Stop();
            }
            else
            {
                Music.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Music_active.png"));
                mediaABCD.Play();
            }
        }

        private bool IsTTSEnabled = false;
        private void SoundTap(object sender, TappedRoutedEventArgs e)
        {
            IsTTSEnabled = !IsTTSEnabled;
            if (IsTTSEnabled)
            {
                Sound.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Sound_mute.png"));                
                textToSpeech.Stop();

            }
            else
            {
                Sound.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Sound_active.png"));
                textToSpeech.Play();
            }
        }

        private async void ReloadTap(object sender, TappedRoutedEventArgs e)
        {
            var speechText = this.CapAlphaName.Text;
            if (speechText != "")
            {
                var synth = new SpeechSynthesizer();

                var speechStream = await synth.SynthesizeTextToStreamAsync(speechText);

                this.textToSpeech.AutoPlay = true;
                this.textToSpeech.SetSource(speechStream, speechStream.ContentType);
                this.textToSpeech.Play();
            }

        }

        private void HomePage(object sender, TappedRoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainPage));
        }

     
    }
}
refer image

推荐答案

嗨pariwesh,



您可以使用在中定义的currentIndex DoLooping方法的页面,而不是每次都创建一个新页面。然后使用while循环而不是for循环,并在PauseTap事件处理程序中将_isPause设置为false时调用DoLooping方法。对于
语音,您可以在语音部分的while循环中检查IsTTSEnabled变量。请参阅以下代码段。

Hi pariwesh,

You could use the currentIndex which is defined in the page for the DoLooping method instead of creating a new one every time. Then use while loop instead of for loop and call the DoLooping method when _isPause is set to false in PauseTap event handler. For speech, you could check the IsTTSEnabled variable in the while loop at the speech part. Please refer to the following code snippet.

private void PauseTap(object sender, TappedRoutedEventArgs e)
{
	_isPaused = !_isPaused; // This will allow the pause button to act as a toggle
	if (_isPaused)
	{
		Pause.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Play.png"));            
	}
	else
	{
		Pause.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Pause.png")); 
		DoLooping();
	}
}
private async Task DoLooping()
{
	while (true)
	{	
		if (_isPaused)
			return;
		else
		{
			Alph_cap.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Cap_alpha/Cap_" + currentIndex + ".png"));
			Alph_small.Source = new BitmapImage(new Uri("ms-appx:///ABCD/Cap_alpha/Sml_" + currentIndex + ".png"));
			CapAlphaName.Text = CapsAlpha[currentIndex];
			SmallAlphaName.Text = SmallAlpha[currentIndex];
			if (currentIndex++ == 25)
			{
				currentIndex = 0;
			}
			if(!IsTTSEnabled)
			{
				var speechText = this.CapAlphaName.Text;
				if (speechText != "")
				{
					var synth = new SpeechSynthesizer();

					var speechStream = await synth.SynthesizeTextToStreamAsync(speechText);

					this.textToSpeech.AutoPlay = true;
					this.textToSpeech.SetSource(speechStream, speechStream.ContentType);
					this.textToSpeech.Play();
				}
			}			
			await Task.Delay(1000);
		}
	}
}      



最好的问候,

David


Best Regards,
David


这篇关于[C#]尝试在页面加载循环时停止连续循环并停止文本到语音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆