macOS AVSpeechSynthesizer writeUtterance got error 1768846202

44 Views Asked by At

I try to save AVSpeechSynthesizer voice like this :

it runs well on iOS, but crash on macOS

- (void)convert2{
    NSLog(@"-- start convert2");
    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
    self.player = synthesizer;
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"test 123 hahah"];
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
    [utterance setVoice:voice];
    [utterance setVolume:0.8];
    [utterance setRate:0.166];
    [utterance setPitchMultiplier:1.25];
      
    NSLog(@"-- audioFileSettings : %@", [voice audioFileSettings]);
    
    __block AVAudioFile *output = nil;

    [synthesizer writeUtterance:utterance
               toBufferCallback:^(AVAudioBuffer * _Nonnull buffer) {
         
        AVAudioPCMBuffer *pcmBuffer = (AVAudioPCMBuffer*)buffer; 
        if (!pcmBuffer) {
            NSLog(@"Error");
            return;
        }
        
        NSLog(@"-- pcmBuffer.frameLength : %u", pcmBuffer.frameLength);
        
        if (pcmBuffer.frameLength != 0) {
            //append buffer to file
            if (output == nil) {
                
                NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
                path = [path stringByAppendingPathComponent:@"0001.caf"]; 
//                NSURL *outputURL = [NSURL fileURLWithPath:@"/Users/xx/Desktop/0001.caf"];
                NSURL *outputURL = [NSURL fileURLWithPath:path];
                NSLog(@"-- outputURL : %@", outputURL);
                output = [[AVAudioFile alloc] initForWriting:outputURL
                                                    settings:pcmBuffer.format.settings
                                                commonFormat:AVAudioPCMFormatInt16
                                                 interleaved:NO error:nil];
            }
            NSLog(@"-- output : %@", output); 
            [output writeFromBuffer:pcmBuffer error:nil]; 
            NSLog(@"-- write success" );
        }
    }]; 
    
}

and got crush, err message as below

Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'error 1768846202'


I tried :

  • open / close sandbox
  • adjust text and language
0

There are 0 best solutions below