SDK 支持细粒度的播放进度回调。开发者可以在收到 SETtsPlaybackProgress
状态回调时,解析附加的 data
字段,获得播放进度(单位:百分比),格式如下:
{ "progress": 0.3, "reqid": "bb081d44-0671-4789-8df5-0050edae517b", }
SDK 返回的播放进度是当句已播放的音频长度除以该句音频的总长度:
综上,如果开发者想要获得准确的播放进度,建议根据标点(;!?。!?;…)对文本进行分割,分句提前合成进行缓存。该策略下,已完成合成的句子,后续的播放进度为准确值。
SDK 支持鉴权证书下载功能,因为没有必要每次初始化都重新下载证书,所以下载只可能在以下几种情况才会触发:
如果需要语音合成 SDK 在后台播报,就可能遇到其他应用使用播放器或录音机的场景。针对这几种情况,我们推荐开发者参考下面的示例代码进行处理,篇幅有限故仅展示核心代码,完整样例程序可参考 Demo 源码。以下示例代码实现了几种效果:
通过接收音频终端通知来完成相应的处理:
- (void)addNotification { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruptionHandler:) name:AVAudioSessionInterruptionNotification object:nil]; } - (void)audioInterruptionHandler:(NSNotification*)notification { AVAudioSessionInterruptionType interruptionType = (AVAudioSessionInterruptionType)[[notification.userInfo objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue]; AVAudioSessionInterruptionOptions intertuptionOptions = [[notification.userInfo objectForKey:AVAudioSessionInterruptionOptionKey] unsignedIntValue]; NSLog(@"Receive audio interruption notification, type: %lu, options: %lu.", (unsigned long)interruptionType, (unsigned long)intertuptionOptions); if (interruptionType == AVAudioSessionInterruptionTypeBegan) { NSLog(@"Audio session interruption began"); @synchronized (self) { int ret = [self.curEngine sendDirective:SEDirectivePausePlayer]; NSLog(@"Pause player return value: %d", ret); } } else if (interruptionType == AVAudioSessionInterruptionTypeEnded) { @synchronized (self) { NSLog(@"Audio session interruption ended"); if (intertuptionOptions == AVAudioSessionInterruptionOptionShouldResume) { AVAudioSession *session = [AVAudioSession sharedInstance]; AVAudioSessionCategoryOptions cur_options = session.categoryOptions; // AudioQueueStart() will return AVAudioSessionErrorCodeCannotInterruptOthers if options didn't contains AVAudioSessionCategoryOptionMixWithOthers if (!(cur_options & AVAudioSessionCategoryOptionMixWithOthers)) { AVAudioSessionCategoryOptions readyOptions = AVAudioSessionCategoryOptionMixWithOthers | cur_options; [session setCategory:AVAudioSessionCategoryPlayback withOptions:readyOptions error:nil]; } int ret = [self.curEngine sendDirective:SEDirectiveResumePlayer]; NSLog(@"Resume player return value: %d", ret); cur_options = session.categoryOptions; // Remove AVAudioSessionCategoryOptionMixWithOthers, or the playback will not be interrupted any more if (cur_options & AVAudioSessionCategoryOptionMixWithOthers) { [session setCategory:AVAudioSessionCategoryPlayback withOptions:((~AVAudioSessionCategoryOptionMixWithOthers) & cur_options) error:nil]; } } } } }