Skip to content

Commit

Permalink
feat(ios): support the voice property (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecc521 authored Mar 9, 2023
1 parent b13d5a7 commit 8b97820
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 11 additions & 1 deletion ios/Plugin/TextToSpeech.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Capacitor
self.resolveCurrentCall()
}

@objc public func speak(_ text: String, _ lang: String, _ rate: Float, _ pitch: Float, _ category: String, _ volume: Float, _ call: CAPPluginCall) throws {
@objc public func speak(_ text: String, _ lang: String, _ rate: Float, _ pitch: Float, _ category: String, _ volume: Float, _ voice : Int, _ call: CAPPluginCall) throws {
self.synthesizer.stopSpeaking(at: .immediate)

var avAudioSessionCategory = AVAudioSession.Category.ambient
Expand All @@ -36,6 +36,16 @@ import Capacitor
utterance.rate = adjustRate(rate)
utterance.pitchMultiplier = pitch
utterance.volume = volume

//Find the voice associated with the voice parameter if a voice specified.
//If the specified voice is not available we will fall back to default voice rather than raising an error.
if (voice >= 0) {
let allVoices = AVSpeechSynthesisVoice.speechVoices()
if (voice < allVoices.count) {
utterance.voice = allVoices[voice]
}
}

synthesizer.speak(utterance)
}

Expand Down
3 changes: 2 additions & 1 deletion ios/Plugin/TextToSpeechPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class TextToSpeechPlugin: CAPPlugin {
let rate = call.getFloat("rate") ?? 1.0
let pitch = call.getFloat("pitch") ?? 1.0
let volume = call.getFloat("volume") ?? 1.0
let voice = call.getInt("voice") ?? -1
let category = call.getString("category") ?? "ambient"

let isLanguageSupported = implementation.isLanguageSupported(lang)
Expand All @@ -27,7 +28,7 @@ public class TextToSpeechPlugin: CAPPlugin {
}

do {
try implementation.speak(text, lang, rate, pitch, category, volume, call)
try implementation.speak(text, lang, rate, pitch, category, volume, voice, call)
} catch {
call.reject(error.localizedDescription)
}
Expand Down

0 comments on commit 8b97820

Please sign in to comment.