Created
December 16, 2017 05:22
-
-
Save Nexengineer/2b810cc1c3141d530d7d0a1f7029bb97 to your computer and use it in GitHub Desktop.
Text To Speech GIST is for what it says
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol TextToSpeechDelegate { | |
func speechDidFinish() | |
} | |
class TextToSpeech: NSObject, AVSpeechSynthesizerDelegate { | |
// Creating Singlton | |
static let sharedInstance = TextToSpeech() | |
private override init() { | |
super.init() | |
voiceToUse = AVSpeechSynthesisVoice.speechVoices().filter({ $0.name == "Karen" }).first | |
self.voiceSynth.delegate = self | |
} | |
// Variables for Converting Text to speech | |
let voices = AVSpeechSynthesisVoice.speechVoices() | |
let voiceSynth = AVSpeechSynthesizer() | |
var voiceToUse: AVSpeechSynthesisVoice? | |
var delegateTextToSpeech: TextToSpeechDelegate! | |
func convertTextToSpeech(_ text: String) { | |
let avAudioSession = AVAudioSession.sharedInstance() | |
do { | |
try avAudioSession.setCategory(AVAudioSessionCategoryPlayback) | |
try avAudioSession.setMode(AVAudioSessionModeMoviePlayback) | |
} catch { | |
print("Audio Session is not active") | |
} | |
let utterance = AVSpeechUtterance(string: text) | |
utterance.voice = voiceToUse | |
utterance.rate = 0.6 | |
voiceSynth.speak(utterance) | |
} | |
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) { | |
self.delegateTextToSpeech.speechDidFinish() | |
} | |
} |
try with AVAudioSessionCategoryPlayback.
I tried with the same but with AVAudioSessionCategoryPlayback, Its not playing on Bluetooth Speaker. Means the audio is not heard on the bluetooth speaker. I tried using . allowBluetoothA2DP only but the same behavior i.e the audio is not being heard on the Bluetooth speaker
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am converting text to speech and playing it on the Car's bluetooth speaker. But when it plays on the car's bluetooth , the car's bluetooth screen show that it initiated the call on the self number. If the iPhone do not contains the SIM card it starts showing the timer. iPhone has no sign of calling being initiated.
I am using following code to play the text in car's bluetooth :
let audioSession = AVAudioSession.sharedInstance()
try? audioSession.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: [.defaultToSpeaker, .allowBluetooth,.allowBluetoothA2DP])
do {
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
} catch {
print("Error in the session")
}
let utterance = AVSpeechUtterance(string: text)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
synth.speak(utterance)
synth.continueSpeaking()
I tried using only . allowBluetoothA2DP but audio cannot be heard in Bluetooth speaker when I am doing this