2014-02-20 2 views
1

Я разрабатываю небольшое приложение, в котором в этом приложении должен быть голос, который говорит несколько слов.Использовать голоса в приложении

Могу ли я использовать голос, который использует некоторые программы, такие как «Google Translate», «Vozme» или подобное? Если нет, как я могу это сделать?

+2

https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVSpeechSynthesizer_Ref/Reference/Reference.html – Kevin

+0

Возможный дубликат [Как программно использовать синтезаторы голоса iOS? (текст в речь)] (http://stackoverflow.com/questions/9939589/how-to-programmatically-use-ios-voice-synthesizers-text-to-speech) – MZimmerman6

ответ

2

AVSpeechSynthesizer Class Reference можно приобрести у iOS7. Документация очень хорошая. Обязательно соедините рамку AVFoundation с вашим проектом.

Вот рабочий пример, который говорит текст, введенный из UITextField когда UIButton сливают - (предполагая UIViewController подкласс имени YOURViewController)

в .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 

и 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 
+0

Это замечательно. Я не знал, что это что-то. +1 за то, что дал мне некоторые знания. – Forrest

+0

Спасибо !! Я собираюсь проверить это. Это хорошее решение, но что произойдет, если устройства не имеют iOS7? Мне нужна «multi» версия iOS. –

+1

Отличный ответ! Чтобы вернуться к предыдущей версии 7, вам придется идти на третью сторону, есть openEars http://www.politepix.com/openears/ или есть iSpeech https://www.ispeech.org/developers – Jef

Смежные вопросы