1

I Understand TTS is only available in iOS 7 Before down vote please read through.

One of my customers want to add text-to-speech feature to existing project based on iOS 6. I understand in iOS 7 we have built in TTS so I want to use it.

The following code is working fine when project is based on iOS 7.

AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Please raise my salary!!!"];
[synthesizer speakUtterance:utterance];

But I am not able to add this code to existing project based on iOS 6.

I am using this thread way. Am I missing something? Or this built in TTS is something different stuff?

Community
  • 1
  • 1
brianLikeApple
  • 4,262
  • 1
  • 27
  • 46

2 Answers2

2

Since you are trying to run code that cannot run on iOS 6, you need to have runtime checks that will make sure that you can run the code you are trying to run. A quick way to do this is to check if the class is available:

if([AVSpeechSynthesizer class]) {
    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Please raise my salary!!!"];
    [synthesizer speakUtterance:utterance];
} else {
    // Feature not available, do something else
}

You will need to do this regardless of whether or not the code compiles or not.

If your code is not even compiling correctly, then there are a few things you can check:

  • Is your base SDK set to iOS 6.0?
  • AVSpeechSynthesizer is part of AVFoundation - are you linking your project to it?
cjwirth
  • 2,015
  • 1
  • 15
  • 24
1
if ( [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f ){

    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Please raise my salary!!!"];
    [synthesizer speakUtterance:utterance];
}
//And an else block if you have a back-up plan
Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
  • Have you tried it on your side? Yes, I know. It doesn't work from my side. otherwise I wouldn't ask such stupid questions. – brianLikeApple Dec 11 '13 at 13:06
  • 2
    This code cannot work, because AVSpeechSynthesizer requires iOS7 (https://developer.apple.com/library/IOs/documentation/AVFoundation/Reference/AVSpeechSynthesizer_Ref/Reference/Reference.html). This will only fix compile error on iOS6. You need to find an alternative way for OS's older than 7. – Yunus Nedim Mehel Dec 11 '13 at 13:08
  • Your code is correct. So I give you up vote. But otiose found the point so I gave him accepted answer : ) – brianLikeApple Dec 11 '13 at 13:54