iOS Text To Speech Api

后端 未结 4 1885
萌比男神i
萌比男神i 2020-12-04 06:26

I can\'t seem to find anything on this. Are there any Siri classes or API\'s in iOS7 that let you do text to speech? All I am trying to do is something like the following:

4条回答
  •  孤街浪徒
    2020-12-04 06:58

    Since iOS 7 you have a new TTS Api.

    In Objective C

    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Some text"];
    [utterance setRate:0.2f];
    [synthesizer speakUtterance:utterance];
    

    In Swift

    let synthesizer = AVSpeechSynthesizer()
    let utterance = AVSpeechUtterance(string: "Some text")
    utterance.rate = 0.2
    

    You can also change the voice like this :

    utterance.voice = AVSpeechSynthesisVoice(language: "fr-FR")
    

    And then speek

    • In Swift 2 synthesizer.speakUtterance(utterance)

    • In Swift 3 synthesizer.speak(utterance)

    Don't forget to import AVFoundation

    Helpful methods

    You can Stop or Pause all speech using these two methods :

    - (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
    - (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;
    

    The AVSpeechBoundary indicates if the speech should pause or stop immediately (AVSpeechBoundaryImmediate) or it should pause or stop after the word currently being spoken (AVSpeechBoundaryWord).

    Check the AVSpeechSynthesizer Doc

提交回复
热议问题