0

In my app I'm using AVSpeechSynthesizer, the text speeches is russian, the problem is when I'm changing system language to English the text pronounce with english accent, sounds like transcription of russian language. How can I manage this problem?

here is some code

    utterance.voice = AVSpeechSynthesisVoice(language: "ru-Ru")
    synthesizer.pauseSpeaking(at: .word)
    utterance = AVSpeechUtterance(string: "Какой-то текст который нужно произнести")
    synthesizer.speak(utterance)
Alexandr Kolesnik
  • 1,929
  • 1
  • 17
  • 30

1 Answers1

0

Maybe it will be helpful for somebody, I found solution, but it looks like workaround before speaking I change AppLanguage, here is the code

    UserDefaults.standard.set(["ru"], forKey: "AppleLanguages")
    UserDefaults.standard.synchronize()
    utterance.voice = AVSpeechSynthesisVoice(identifier: "ru-RU")

after synchronizing AVSpeechSynthesisVoice.currentLanguageCode() became "ru" ignoring system language

Here is full code example for Turkish

import UIKit

import AVFoundation

class ViewController: UIViewController {

let synthesizer : AVSpeechSynthesizer = AVSpeechSynthesizer()
var utterance : AVSpeechUtterance = AVSpeechUtterance(string: "")

override func viewDidLoad() {
    super.viewDidLoad()
    UserDefaults.standard.set(["tr"], forKey: "AppleLanguages")
    UserDefaults.standard.synchronize()
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .interruptSpokenAudioAndMixWithOthers)
    utterance.voice = AVSpeechSynthesisVoice(identifier: "tr-TR")
    synthesizer.pauseSpeaking(at: .word)
    testSpeech()
}

private func testSpeech() {
    utterance = AVSpeechUtterance(string: "Çeviri için deney metni")
    speak()
}

private func speak() {
    if synthesizer.isSpeaking {
        synthesizer.pauseSpeaking(at: .immediate)
        return
    }
    synthesizer.speak(utterance)
}

}

Alexandr Kolesnik
  • 1,929
  • 1
  • 17
  • 30
  • Regardless language keys it works with the system language. – Ahmet Akkök Jun 13 '17 at 06:25
  • Can you show a part of code where you want to use this code? In my case I used it to remove english accent because the app is russian. – Alexandr Kolesnik Jun 13 '17 at 08:54
  • There you go : let lName = defaults.getLanguage() defaults.store?.setObject([defaults.lang2LetterCode(lName)], forKey: "AppleLanguages") defaults.store?.synchronize() let langCode = defaults.lang5LetterCode(lName) if let voiceSL = AVSpeechSynthesisVoice(language: langCode) { utterance.voice = voiceSL } – Ahmet Akkök Jun 13 '17 at 22:41
  • I tried your code using en-GB as langCode, and it works correct. What exact doesn't work? You still have deviceLanguage accent though the text you want to play differs? – Alexandr Kolesnik Jun 14 '17 at 07:27
  • When device language is English and if I set it up for "tr-TR" it does not read Turkish. – Ahmet Akkök Jun 15 '17 at 11:42
  • I will try and let you know – Ahmet Akkök Jun 18 '17 at 22:23