Ios avspeechsynthesis writeutrance。如何找到完整的话语是否被写入文件?
我指的是下面的链接:- 想知道我问题的答案。但是上面的链接工作得很好w.r.t.只从AVSpeechSynthesizer编写一个音频文件。如果我需要将多个原始pcm音频文件写入数据库,该怎么办 我面临的问题是缓冲区回调被多次调用。因此,我无法确定文件写入是否为第一次发言完成,是否可以开始写入另一个文件。 以下是我的代码库:-Ios avspeechsynthesis writeutrance。如何找到完整的话语是否被写入文件?,ios,objective-c,avspeechsynthesizer,avspeechutterance,Ios,Objective C,Avspeechsynthesizer,Avspeechutterance,我指的是下面的链接:- 想知道我问题的答案。但是上面的链接工作得很好w.r.t.只从AVSpeechSynthesizer编写一个音频文件。如果我需要将多个原始pcm音频文件写入数据库,该怎么办 我面临的问题是缓冲区回调被多次调用。因此,我无法确定文件写入是否为第一次发言完成,是否可以开始写入另一个文件。 以下是我的代码库:- AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init]; AVSpeechUttera
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"test 123"];
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
[utterance setVoice:voice];
__block AVAudioFile *output = nil;
[synthesizer writeUtterance:utterance
toBufferCallback:^(AVAudioBuffer * _Nonnull buffer) {
AVAudioPCMBuffer *pcmBuffer = (AVAudioPCMBuffer*)buffer;
if (!pcmBuffer) {
NSLog(@"Error");
return;
}
if (pcmBuffer.frameLength != 0) {
//append buffer to file
if (output == nil) {
output = [[AVAudioFile alloc] initForWriting:[NSURL fileURLWithPath:@"test.caf"]
settings:pcmBuffer.format.settings
commonFormat:AVAudioPCMFormatInt16
interleaved:NO error:nil];
}
[output writeFromBuffer:pcmBuffer error:nil];
}
}];
啊。终于明白了。这是AvaudiOpcBuffer类的frameLength属性,它帮助我确定音频流是否完全写入。 使用此属性并通过在合成器块外声明新的var来跟踪写入文件的总帧长。您可以参考以下代码:-
__block long currentFrameLength = 0;
[synthesizer writeUtterance:utterance
toBufferCallback:^(AVAudioBuffer * _Nonnull buffer) {
if(currentFrameLength == 0) {
{ // This check signifies that previous file is completely written
}
currentFrameLength = currentFrameLength + pcmBuffer.frameLength;