提交 83ea0d07 作者: 王苏进

feat: 增加 同步语音文字显示

上级 f9defa05
......@@ -35,8 +35,10 @@ class _MyAppState extends State<MyApp> {
_aivoicePlugin.onAsrResultReceived.listen((event) {
final a = event["finish"];
final b = event["playWords"];
setState(() {});
_text = "$a";
_text = b ?? "$a";
// print(event["text"]);
});
_aivoicePlugin.prepareEnvironment({"appId": "2301072440"});
......
......@@ -692,12 +692,8 @@ static int TTS_MAX_RETRY_COUNT = 3;
// 根据 SDK 返回的播放进度高亮正在播放的文本,用红色表示
// 根据 SDK 返回的合成开始和合成结束回调高亮正在合成的文本,用蓝色表示
-(void)updateTtsResultText:(NSString*) playingId {
if (self.engineErrorOccurred) {
NSLog(@"When a fatal error occurs, prevent the playback text from being displayed.");
return;
}
-(void)updatePlayingWords:(NSString*) playingId {
int synthesisIndex = self.ttsSynthesisIndex;
NSNumber* val = [self.ttsSynthesisMap objectForKey:playingId];
if (val != nil) {
self.ttsPlayingIndex = [val intValue];
......@@ -705,7 +701,8 @@ static int TTS_MAX_RETRY_COUNT = 3;
int beginIndex = MAX(self.ttsPlayingIndex, 0);
int maxSentencesDisplayed = MIN((int)[self.ttsSynthesisText count], 16);
NSMutableAttributedString *resultStr = [[NSMutableAttributedString alloc] initWithString:@""];
NSString *resultStr = @"";
for (int cnt = 0; cnt < maxSentencesDisplayed; ++cnt) {
int index = (beginIndex + cnt) % [self.ttsSynthesisText count];
NSString* current_sentence = self.ttsSynthesisText[index];
......@@ -713,13 +710,11 @@ static int TTS_MAX_RETRY_COUNT = 3;
if (index == self.ttsPlayingIndex) {
playedPosition = MIN(ceil((double)(self.ttsPlayingProgress) * (double)([current_sentence length])), [current_sentence length]);
NSLog(@"played position: %ld", (long)playedPosition);
NSString* playedString = [current_sentence substringToIndex:playedPosition];
NSAttributedString* playedSpan = [[NSAttributedString alloc] initWithString:playedString attributes:[NSDictionary dictionaryWithObject:[UIColor redColor] forKey:NSForegroundColorAttributeName]];
[resultStr appendAttributedString:playedSpan];
resultStr = [resultStr stringByAppendingString:[current_sentence substringToIndex:playedPosition]];
}
if([ self.delegate respondsToSelector:@selector(onRecieve:)]) {
[self.delegate onRecieve:@{@"playWords": resultStr}];
}
NSString* remainString = [current_sentence substringFromIndex:playedPosition];
NSAttributedString* span = [[NSAttributedString alloc] initWithString:remainString attributes:[NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName]];
[resultStr appendAttributedString:span];
}
// [self.resultTextView setAttributedText:resultStr];
}
......@@ -749,7 +744,7 @@ static int TTS_MAX_RETRY_COUNT = 3;
dispatch_async(dispatch_get_main_queue(), ^{
// self.pauseResumeButton.enabled = TRUE;
self.ttsPlayingProgress = 0.0;
[self updateTtsResultText:playingId];
[self updatePlayingWords:playingId];
});
}
......@@ -771,10 +766,8 @@ static int TTS_MAX_RETRY_COUNT = 3;
NSLog(@"playing id: %@, progress in percent: %.2f", reqid, percentage);
dispatch_async(dispatch_get_main_queue(), ^{
self.ttsPlayingProgress = percentage;
if([ self.delegate respondsToSelector:@selector(onRecieve:)]) {
[self.delegate onRecieve:@{@"play_progress": @(percentage)}];
}
// [self updateTtsResultText:reqid];
[self updatePlayingWords:reqid];
});
}
}
......@@ -786,26 +779,18 @@ static int TTS_MAX_RETRY_COUNT = 3;
// if (![self.settings getBool:SETTING_TTS_ENABLE_DUMP_NOVEL_TTS_DETAIL]) {
// [self.ttsSynthesisMap removeObjectForKey:playingId];
// }
[self.ttsSynthesisMap removeObjectForKey:playingId];
dispatch_async(dispatch_get_main_queue(), ^{
self.ttsPlayingProgress = 1.0;
[self updateTtsResultText:playingId];
[self updatePlayingWords:playingId];
if([[self.ttsSynthesisMap allKeys] count] == 0) {
NSLog(@"这里告诉flutter 已经全部读完了");
[self.delegate onRecieve:@{@"finish": @(YES)}];
}
});
if (self.ttsSynthesisFromPlayer) {
if(self.ttsSynthesisIndex == 0) {
// 播放完第一遍后自动停止
[self stopEngineBtnClicked];
if([self.delegate respondsToSelector:@selector(onRecieve:)]) {
[self.delegate onRecieve:@{@"finish" : @(true)}];
}
} else {
[self triggerSynthesis];
self.ttsSynthesisFromPlayer = FALSE;
}
[self triggerSynthesis];
self.ttsSynthesisFromPlayer = FALSE;
}
}
......@@ -827,7 +812,9 @@ static int TTS_MAX_RETRY_COUNT = 3;
- (void)synthesisNextSentence {
self.ttsSynthesisIndex = (self.ttsSynthesisIndex + 1) % [self.ttsSynthesisText count];
if (!self.ttsSynthesisFromPlayer) {
// 合成最后一条后停止
if (!self.ttsSynthesisFromPlayer && (self.ttsSynthesisIndex +1) < self.ttsSynthesisText.count) {
[self triggerSynthesis];
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论