提交 83ea0d07 作者: 王苏进

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

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