Xamarin Forms:如何突出显示文本和暂停/播放文本到语音的音频? [英] Xamarin Forms: How to highlight text and pause/play audio of text to speech?
本文介绍了Xamarin Forms:如何突出显示文本和暂停/播放文本到语音的音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 xamarin Essentials
包来实现文本转语音功能.当语音文本时,我需要突出显示相应的文本.另外,我需要一个选项来暂停/播放演讲.请参阅
如何实现高亮文本功能和暂停/播放音频作为视频?
解决方案
突出显示文本功能
您可以拆分 Label 的文本.并使用 Span 设置突出显示.
在 xaml 中
<标签x:名称=标签"WidthRequest=250"高度请求=300"/><按钮文本=开始"Clicked=Button_Clicked"/></StackLayout>
在隐藏代码中
公共部分类 MainPage : ContentPage{字符串[] strList;公共主页(){初始化组件();string content = "每个平台都支持不同的区域设置,\n 以不同的语言和口音回说文本.\n 平台具有不同的代码和指定区域设置的方式,\n 这就是 Xamarin 提供跨平台区域设置类和一种使用 GetLocalesAsync 查询它们的方法.\n ";label.Text = 内容;字符串 str = ".";char 字符 = char.Parse(str);字符串 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
I am using the xamarin essentials
package for text to speech feature. When speech the text I need to highlight the corresponding text. Also, I need an option to pause/play the speech. Please see this video.
Screenshot:
How can I achieve highlight text feature and pause/play audio as the video?
解决方案
highlight text feature
You could splite the Text of Label . And use Span to set the highlight .
in xaml
<StackLayout x:Name="firstPage" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
<Label x:Name="label" WidthRequest="250" HeightRequest="300" />
<Button Text="start" Clicked="Button_Clicked" />
</StackLayout>
in Code Behind
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 Forms:如何突出显示文本和暂停/播放文本到语音的音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文