iOS speech synthesis

Add reference

1
import< /span> AVFoundation

play voice

1
2
3
4
5
6
7
8
9
 
let player = AVSpeechSynthesizer();
player.delegate = self;
let u = AVSpeechUtterance(string: "The weather is nice today"); span>
u.voice = AVSpeechSynthesisVoice(language: "zh-CN" );
u.volume = 1.0;//volume[0-1 ] Default = 1
u.rate = 0.48;/ /Play speed
u.pitchMultiplier = 1.0;// Play base pitch [0.5-2] Default = 1
player.speakUtterance(u);

Below iPhone6 rate is set to 0.1Otherwise, you will read the pits of iOS very quickly

agent method

1
< span class="line">2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Start playing
func (synthesizer: AVSpeechSynthesizer, didStartSpeechUtterance utterance: AVSpeechUtterance) {
< span class="built_in">print("start playing")
}

//Playing
< sp an class="line">func (synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
let process = Double span>(characterRange.location) / Double(utterance.speechString.characters.count);
print("During playback, the playback progress is: (process)")
}

//Playing finished
func (synthesizer: AVSpeechSynthesizer, didFinishSpeechUtterance utterance: AVSpeechUtterance) {
print ("Playing completed")
}

common method

1
2
3
4
//Stop playing
player.stopSpeakingAtBoundary(AVSpeechBoundary.Immediate);
//Pause playback
player.pauseSpeakingAtBoundary(AVSpeechBoundary.Immediate);

< /td>

Leave a Comment

Your email address will not be published.