我正在将xamarin essentials
包用于文本到语音功能。演讲文本时,我需要突出显示相应的文本。另外,我需要一个选项来暂停/播放演讲。请看这个视频。
截屏:
如何实现高亮文本功能和暂停/播放音频作为视频?
我正在将xamarin essentials
包用于文本到语音功能。演讲文本时,我需要突出显示相应的文本。另外,我需要一个选项来暂停/播放演讲。请看这个视频。
截屏:
如何实现高亮文本功能和暂停/播放音频作为视频?
突出显示文本功能
您可以拆分 Label 的文本。并使用Span 设置高亮。
<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();
}
}