Xamarin Forms:如何突出显示文本和 pause/play 文本到语音的音频?
Xamarin Forms: How to highlight text and pause/play audio of text to speech?
我正在使用 xamarin essentials
包进行文本转语音功能。演讲文本时,我需要突出显示相应的文本。另外,我需要 pause/play 演讲的选项。请观看 this 视频。
截图:
如何实现高亮文本功能和pause/play音频作为视频?
highlight text feature
您可以拆分 Label 的文本。并使用 Span 设置高亮 .
在xaml
<StackLayout x:Name="firstPage" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
<Label x:Name="label" WidthRequest="250" HeightRequest="300" />
<Button Text="start" Clicked="Button_Clicked" />
</StackLayout>
在代码隐藏中
public partial class MainPage : ContentPage
{
string[] strList;
public MainPage()
{
InitializeComponent();
string content = "Each platform supports different locales,\n to speak back text in different languages and accents.\n Platforms have different codes and ways of specifying the locale, \n which is why Xamarin provides a cross-platform Locale class and a way to query them with GetLocalesAsync.\n ";
label.Text = content;
string str = ".";
char character = char.Parse(str);
string str2 = ",";
char character2 = char.Parse(str2);
strList = content.Split(new char[] { character,character2 });
}
private async void Button_Clicked(object sender, EventArgs e)
{
for (int i=0;i< strList.Length;i++)
{
string content = strList[i];
var formattedString = new FormattedString();
for (int j=0;j<strList.Length;j++)
{
if(i==j)
{
formattedString.Spans.Add(new Span { Text = strList[j], ForegroundColor = Color.Black, BackgroundColor = Color.Gray });
}
else
{
formattedString.Spans.Add(new Span { Text = strList[j], ForegroundColor = Color.Black, });
}
}
label.FormattedText = formattedString;
//Using a bool varibale we can pause the TTS fucntion, when press back button set the value of StopTTS to true.
//When loading this set the value back to false.
if (!Utility.StopTTS)
{
await TextToSpeech.SpeakAsync(content);
}
}
}
protected override bool OnBackButtonPressed()
{
Utility.StopTTS = true;
return base.OnBackButtonPressed();
}
}
我正在使用 xamarin essentials
包进行文本转语音功能。演讲文本时,我需要突出显示相应的文本。另外,我需要 pause/play 演讲的选项。请观看 this 视频。
截图:
如何实现高亮文本功能和pause/play音频作为视频?
highlight text feature
您可以拆分 Label 的文本。并使用 Span 设置高亮 .
在xaml
<StackLayout x:Name="firstPage" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
<Label x:Name="label" WidthRequest="250" HeightRequest="300" />
<Button Text="start" Clicked="Button_Clicked" />
</StackLayout>
在代码隐藏中
public partial class MainPage : ContentPage
{
string[] strList;
public MainPage()
{
InitializeComponent();
string content = "Each platform supports different locales,\n to speak back text in different languages and accents.\n Platforms have different codes and ways of specifying the locale, \n which is why Xamarin provides a cross-platform Locale class and a way to query them with GetLocalesAsync.\n ";
label.Text = content;
string str = ".";
char character = char.Parse(str);
string str2 = ",";
char character2 = char.Parse(str2);
strList = content.Split(new char[] { character,character2 });
}
private async void Button_Clicked(object sender, EventArgs e)
{
for (int i=0;i< strList.Length;i++)
{
string content = strList[i];
var formattedString = new FormattedString();
for (int j=0;j<strList.Length;j++)
{
if(i==j)
{
formattedString.Spans.Add(new Span { Text = strList[j], ForegroundColor = Color.Black, BackgroundColor = Color.Gray });
}
else
{
formattedString.Spans.Add(new Span { Text = strList[j], ForegroundColor = Color.Black, });
}
}
label.FormattedText = formattedString;
//Using a bool varibale we can pause the TTS fucntion, when press back button set the value of StopTTS to true.
//When loading this set the value back to false.
if (!Utility.StopTTS)
{
await TextToSpeech.SpeakAsync(content);
}
}
}
protected override bool OnBackButtonPressed()
{
Utility.StopTTS = true;
return base.OnBackButtonPressed();
}
}