0

I'm developing a little app where in this app, have to have a voice who say some words.

Can I use the voice that some programs uses like "Google Translate", "Vozme" or similar? If not, how can I do this?

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
mk_lopez
  • 9
  • 1
  • 2
    https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVSpeechSynthesizer_Ref/Reference/Reference.html – Kevin Feb 20 '14 at 15:16
  • possible duplicate of [How to programmatically use iOS voice synthesizers? (text to speech)](http://stackoverflow.com/questions/9939589/how-to-programmatically-use-ios-voice-synthesizers-text-to-speech) – MZimmerman6 Feb 20 '14 at 15:18

1 Answers1

2

AVSpeechSynthesizer Class Reference is available from iOS7. The documentation is very good. Be sure to link the AVFoundation framework to your project.

Here is a working example that speaks text entered from a UITextField when a UIButton is tapped - (assuming a UIViewController sub-class named YOURViewController)

in .h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface YOURViewController : UIViewController <AVSpeechSynthesizerDelegate, UITextFieldDelegate> {
    IBOutlet UITextField *textFieldInput;// connect to a UITextField in IB
}

- (IBAction)speakTheText:(id)sender;// connect to a UIButton in IB

@end

and in .m

#import "YOURViewController.h"

@interface YOURViewController ()

@end

@implementation YOURViewController

- (IBAction)speakTheText:(id)sender {
    // create string of text to talk
    NSString *talkText = textFieldInput.text;
    // convert string
    AVSpeechUtterance *speechUtterance = [self convertTextToSpeak:talkText];
    // speak it...!
    [self speak:speechUtterance];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (AVSpeechUtterance*)convertTextToSpeak:(NSString*)textToSpeak {
    AVSpeechUtterance *speechUtterance = [[AVSpeechUtterance alloc] initWithString:textToSpeak];
    speechUtterance.rate = 0.2; // default = 0.5 ; min = 0.0 ; max = 1.0
    speechUtterance.pitchMultiplier = 1.0; // default = 1.0 ; range of 0.5 - 2.0
    speechUtterance.voice = [self customiseVoice];
    return speechUtterance;
}

- (AVSpeechSynthesisVoice*)customiseVoice {
    NSArray *arrayVoices = [AVSpeechSynthesisVoice speechVoices];
    NSUInteger numVoices = [arrayVoices count];
    AVSpeechSynthesisVoice *voice = nil;

    for (int k = 0; k < numVoices; k++) {
        AVSpeechSynthesisVoice *availCustomVoice = [arrayVoices objectAtIndex:k];
        if ([availCustomVoice.language  isEqual: @"en-GB"]) {
            voice = [arrayVoices objectAtIndex:k];
        }
// This logs the codes for different nationality voices available
// Note that the index that they appear differs from 32bit and 64bit architectures
        NSLog(@"#%d %@", k, availCustomVoice.language);
    }
    return voice;
}

- (void)speak:(AVSpeechUtterance*)speechUtterance {
    AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
    speechSynthesizer.delegate = self;// all methods are optional
    [speechSynthesizer speakUtterance:speechUtterance];
}

@end
Bamsworld
  • 5,670
  • 2
  • 32
  • 37
  • This is great. I wasn't aware that this was a thing. +1 for giving me some knowledge. – Forrest Feb 20 '14 at 16:28
  • Thank you!! I'm going to test it. It's a good solution, but, what happens if the devices don't have iOS7? I need a "multi" iOS version. – mk_lopez Feb 20 '14 at 19:12
  • 1
    Great answer! To go back pre iOs7 you'll have to go third party, there's openEars http://www.politepix.com/openears/ or there's iSpeech https://www.ispeech.org/developers – Jef Feb 21 '14 at 01:35