iOS comes with text-to-speech

Source: Internet
Author: User

Recent projects used to convert a piece of text through the button click Control to read aloud, has not known the system has this function, so this is recorded so that the next use. Before their own projects have used the text-to-speech technology, but through the actual test, found that it's free online to voice is not very good, affected by the network sound is often intermittent, and its offline turn to voice prices are too expensive, the cheapest to 8000rmb/2000 installed capacity, so many developers look at the halt. So since can not pay the high cost of the news fly, iOS comes with text To speech is also our good choice, but his pronunciation is not ideal, but also acceptable. Before iOS7, you might need a third-party repository to implement voice-broadcast text content. Now after iOS7, the system provides us with the function of voice broadcast text, we can not only broadcast English content, but also can broadcast Chinese characters.

First we create a button to manipulate:

UIButton *startbutton = [UIButton buttonwithtype:uibuttontypecustom];

Startbutton.frame = CGRectMake (100,100,100,50);

[Startbutton settitle:@] began to read aloud "Forstate:uicontrolstatenormal";

[Startbutton Settitlecolor:[uicolorbluecolor] forstate:uicontrolstatenormal];

Startbutton.backgroundcolor = [Uicolor Graycolor];

startbutton.showstouchwhenhighlighted = YES;

[Startbutton addtarget:selfaction: @selector (speaktext:) forcontrolevents:uicontroleventtouchupinside];

[Self.view Addsubview:startbutton];

Then formally start:

First step: You need to import the Avfoundation.framework framework in Target-build Phases-link Binary with Libraries.

Part II: Import header files into the required classes #import <AVFoundation/AVSpeechSynthesis.h>, if you need to use the agent, you need to follow the agent Avspeechsynthesizerdelegate.

Part III: Create a global variable for our control: Avspeechsynthesizer *synth; You can create a local if you don't need to pause or resume playback.

Fourth: In the Click event of the button

-(Ibaction) SpeakText: (ID) Sender {

if ([[[[[Uidevice Currentdevice] systemversion] integervalue] >= 7.0)//Determine if the system is greater than or equal to 7.0

{

if ([synth ispaused]) {

Resume if paused, will continue from where it was paused

[Synth continuespeaking];

} else {

Text that needs to be converted

NSString *str = @ "One is Langzhong, one is a flawless jade . If you say that is not frozen, this life and meet him, if there is a, how to the end of the mind? A sigh sigh ah, an empty labor cares about. One is the moon in the water, the other is the flower in the mirror. Want to see how many tears in the eyes, how to forbid the autumn flow to winter, spring to summer! ";

Avspeechutterance *utterance = [Avspeechutterance speechutterancewithstring:str];

Utterance.rate = 0.5; Set speed, range 0-1, note 0 slowest, 1 fastest; (avspeechutteranceminimumspeechrate slowest, avspeechutterancemaximumspeechrate fastest)

synth = [[Avspeechsynthesizer alloc] init];

Synth.delegate = self;//Set Agent

Get current system voice

NSString *preferredlang = @ "";

Set the pronunciation, this is Mandarin Chinese

Preferredlang = @ "ZH-CN";

Avspeechsynthesisvoice *voice = [Avspeechsynthesisvoice voicewithlanguage:[nsstring stringWithFormat:@ "%@", Preferredlang]];

Utterance.voice = Voice;

[Synth speakutterance:utterance]; Start reading

}

}

}

Then create a button to control pause playback or stop playback

UIButton *stopbutton = [UIButton buttonwithtype:uibuttontypecustom];

Stopbutton.frame = CGRectMake (100,100,100,50);

[Stopbutton settitle:@ "Stop reading" forstate:uicontrolstatenormal];

[Stopbutton Settitlecolor:[uicolorbluecolor] forstate:uicontrolstatenormal];

Stopbutton.backgroundcolor = [Uicolor Graycolor];

stopbutton.showstouchwhenhighlighted = YES;

[Stopbutton addtarget:selfaction: @selector (stopspeaktext:) forcontrolevents:uicontroleventtouchupinside];

[Self.view Addsubview:stopbutton];

Stop/Pause Playback

-(Ibaction) Stopspeaktext: (ID) Sender {

[Synth stopspeakingatboundary:avspeechboundaryword];//stops playing, calls this method, and starts again from the beginning

[Synth pausespeakingatboundary:avspeechboundaryword];//pauses playback, calls this method, and then resumes playback from the paused place.

}

#pragma mark---Here is the Proxy method: Avspeechsynthesizerdelegate

-(void) SpeechSynthesizer: (avspeechsynthesizer*) Synthesizer didstartspeechutterance: (avspeechutterance*) utterance{

NSLog (@ "---start playing");

}

-(void) SpeechSynthesizer: (avspeechsynthesizer*) Synthesizer didfinishspeechutterance: (avspeechutterance*) utterance{

NSLog (@ "---playback complete");

}

-(void) SpeechSynthesizer: (avspeechsynthesizer*) Synthesizer didpausespeechutterance: (avspeechutterance*) utterance{

NSLog (@ "---pause playback");

}

-(void) SpeechSynthesizer: (avspeechsynthesizer*) Synthesizer didcontinuespeechutterance: (avspeechutterance*) utterance{

NSLog (@ "---continue to play");

}

-(void) SpeechSynthesizer: (avspeechsynthesizer*) Synthesizer didcancelspeechutterance: (avspeechutterance*) utterance{

NSLog (@ "---cancel playback");

}

The basic use of this, of course, want to know more, you can go into the Avspeechsynthesizer header file to see its other methods of use and introduction.

iOS comes with text-to-speech

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.