How iOS realizes voice broadcast and background playback

Source: Internet
Author: User

Recently the project has just been delivered, and there are occasional voice and voice search features. Voice search I use is to fly the demo, the feeling is also good, interested in the words can go to the official online download demo, which said the special details, but slightly more trouble. Voice broadcast flying also has a demo, but do the development of course to seek the most concise way to deal with, after ios7.0 added some new features, which have the system with the Voice broadcast library avfoundation. There are actually quite a few articles about voice broadcasts. Text-to-Speech technology, also known as TTS, is the abbreviation for text to speech. iOS uses this technique if it wants to do something like audio books.

A few things to note when using iOS self-TTS: IOS7 only after this function requires Avfoundation library Avspeechsynthesizer: Speech synthesizer, can be imagined as a speaker, is the most important interface Avspeechsynthesisvoice: Can imagine the voice of the adult avspeechutterance: can be imagined as a passage to say

Second, code example, play voice

    Voice broadcast
    avspeechutterance *utterance = [avspeechutterance speechutterancewithstring:@ "Bed before Ming Moonlight, doubt is the ground frost." "];

    utterance.pitchmultiplier=0.8;
    
    Chinese pronunciation
    avspeechsynthesisvoice *voice = [Avspeechsynthesisvoice voicewithlanguage:@ "ZH-CN"];
    English pronunciation
//    Avspeechsynthesisvoice *voice = [Avspeechsynthesisvoice voicewithlanguage:@ "EN-GB"];
    
    Utterance.voice = Voice;
    
    NSLog (@ "%@", [Avspeechsynthesisvoice speechvoices]);
    
    Avspeechsynthesizer *synth = [[Avspeechsynthesizer alloc]init];
    
    [Synth speakutterance:utterance];

Three, Avspeechsynthesizer introduction

This class is like a talking person who can "talk", "pause", and "continue" to speak to determine whether he is speaking at the moment. The following methods or attributes are available: Speak: Speakutterance control: Continuespeaking (continued), Pausespeakingatboundary (pause speech), paused (paused state of the attribute), speaking (speaking state), Stopspeakingatboundary (stop Talking) delegate: Delegate

Iv. Introduction to Avspeechboundary

This is an enumeration. When pausing, or stopping speaking, the way to stop is marked with this enumeration. Consists of two kinds: avspeechboundaryimmediate: Immediately stop Avspeechboundaryword: After saying a whole word to stop

Five, Avspeechsynthesizerdelegate Introduction

A delegate for a synthesizer that provides a response interface for some events. Didcancelspeechutterance: The speech has been canceled didcontinuespeechutterance: have continued to speak Didfinishspeechutterance: have finished saying Didpausespeechutterance: Has been paused didstartspeechutterance: Has started willspeakrangeofspeechstring: will say a certain paragraph of words

Vi. Introduction to Avspeechsynthesisvoice

Avspeechsynthesisvoice defines a series of sounds, mainly in different languages and regions. Voicewithlanguage: Get a sound according to the language you make. Speechvoices: Get current device supported sound Currentlanguagecode: Get the language string for the current sound, such as "ZH-CN" language: getting the current language

Seven, Avspeechutterance Introduction

This class is a paragraph to say. The main properties and methods are: Pitchmultiplier: Pitch postutterancedelay: The pause time after reading a paragraph preutterancedelay: pause before reading a word rate: reading speed, the system provides three speed: Avspeechutteranceminimumspeechrate, Avspeechutterancemaximumspeechrate, avspeechutterancedefaultspeechrate Speechstring: The string to be read voice: The sound used is the Avspeechsynthesisvoice object

The above is about the basic usage of voice broadcast and some attributes, methods, but how to combine the program push, in the program backstage running time to achieve the effect of voice broadcast. There are, of course, a lot of places to watch out. 1. Enable Push Wakeup

And above the background to get similar, change info.plist, add remote-notification under Uibackgroundmodes to open, of course, the same simpler direct way is to use capabilities, tick the following three modes.

2. Change payload for push

In IOS7, if you want to use push to wake up the application run code, you need to add content-available to the payload and set to 1.

{"APS": {"content-available": 1, "alert": "Today is a Good day"}}

"Content-available": 1 Push Wake

' Alert ': ' Push content

"badge": 1 app in the upper right corner number

' Sound ': Default sound

Aps

{

Content-available:1

Alert: {...}

3. Implement push Wake code and notify system

Finally, the implementation of-application:didreceiveremotenotification:fetchcompletionhandle in Appdelegate:. This section is exactly the same as the background fetch section above and is not repeated here.

Receive a push message

-(void) Application: (UIApplication *) application didreceiveremotenotification: (nsdictionary *) userInfo Fetchcompletionhandler: (nonnull void (^) (uibackgroundfetchresult)) Completionhandler {

NSLog (@ "Remote:%@", userInfo);

Callback

Completionhandler (Uibackgroundfetchresultnewdata);

Voice broadcast

Avspeechutterance *utterance = [avspeechutterance speechutterancewithstring:userinfo[@ "APS"][@ "alert"]];

Avspeechsynthesizer *synth = [[Avspeechsynthesizer alloc] init];

[Synth speakutterance:utterance];

}

The above steps can be done in the background voice broadcast.

Reference article Links:
First, http://www.jianshu.com/p/174fd2673897
Second, https://onevcat.com/2013/08/ios7-background-multitask/
Third, http://hayageek.com/ios-silent-push-notifications/

Four, http://blog.csdn.net/u012477117/article/details/52039506

Five, http://www.cnblogs.com/luerniu/p/5901350.html

VI. https://www.oschina.net/question/2556708_2194798

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.