iOS如何實現語音播報及後臺播放
最近專案剛剛交付,偶然間用到了語音播報和語音搜尋的功能。語音搜尋我用的是訊飛的demo,感覺效果還不錯,感興趣的話可以去官網上面下載demo,裡面講的特別的詳細,不過稍顯麻煩一些。語音播報訊飛也有demo,不過做開發當然要尋求最簡潔的處理方式,ios7.0之後新添加了一些新的功能,裡面就有系統自帶的語音播報庫AVFoundation。關於語音播報的文章其實挺多的。文字轉語音技術, 也叫TTS, 是Text To Speech的縮寫. iOS如果想做有聲書等功能的時候, 會用到這門技術.
一,使用iOS自帶TTS需要注意的幾點:
- iOS7之後才有該功能
- 需要 AVFoundation 庫
- AVSpeechSynthesizer: 語音合成器, 可以假想成一個可以說話的人, 是最主要的介面
- AVSpeechSynthesisVoice: 可以假想成人的聲音
- AVSpeechUtterance: 可以假想成要說的一段話
二,程式碼示例, 播放語音
//語音播報 AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"床前明月光,疑是地上霜。"]; utterance.pitchMultiplier=0.8; //中式發音 AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]; //英式發音 // AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"]; utterance.voice = voice; NSLog(@"%@",[AVSpeechSynthesisVoice speechVoices]); AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc]init]; [synth speakUtterance:utterance];
三,AVSpeechSynthesizer介紹
這個類就像一個會說話的人, 可以”說話”, 可以”暫停”說話, 可以”繼續”說話, 可以判斷他當前是否正在說話.有以下的方法或者屬性:
- 說話: speakUtterance
- 控制: continueSpeaking(繼續說), pauseSpeakingAtBoundary(暫停說話), paused(暫停狀態的屬性), speaking(說話的狀態), stopSpeakingAtBoundary(停止說話)
- 委託: delegate
四,AVSpeechBoundary介紹
這是一個列舉. 在暫停, 或者停止說話的時候, 停下的方式用這個列舉標示. 包括兩種:
- AVSpeechBoundaryImmediate: 立即停
- AVSpeechBoundaryWord : 說完一個整詞再停
五,AVSpeechSynthesizerDelegate介紹
合成器的委託, 對於一些事件, 提供了響應的介面.
- didCancelSpeechUtterance: 已經取消說話
- didContinueSpeechUtterance: 已經繼續說話
- didFinishSpeechUtterance: 已經說完
- didPauseSpeechUtterance: 已經暫停
- didStartSpeechUtterance:已經開始
- willSpeakRangeOfSpeechString:將要說某段話
六,AVSpeechSynthesisVoice介紹
AVSpeechSynthesisVoice定義了一系列的聲音, 主要是不同的語言和地區.
- voiceWithLanguage: 根據制定的語言, 獲得一個聲音.
- speechVoices: 獲得當前裝置支援的聲音
- currentLanguageCode: 獲得當前聲音的語言字串, 比如”ZH-cn”
- language: 獲得當前的語言
七,AVSpeechUtterance介紹
這個類就是一段要說的話. 主要的屬性和方法有:
- pitchMultiplier: 音高
- postUtteranceDelay: 讀完一段後的停頓時間
- preUtteranceDelay: 讀一段話之前的停頓
- rate: 讀地速度, 系統提供了三個速度: AVSpeechUtteranceMinimumSpeechRate, AVSpeechUtteranceMaximumSpeechRate, AVSpeechUtteranceDefaultSpeechRate
- speechString: 要讀的字串
- voice: 使用的聲音, 是AVSpeechSynthesisVoice物件
上面這些是關於語音播報的基本用法和一些屬性、方法,但是如何結合程式推送,在程式後臺執行的時候實現語音播報的效果呢?當然還有很多需要注意的地方。
- 1.啟用推送喚醒
和上面的後臺獲取類似,更改Info.plist,在UIBackgroundModes下加入remote-notification即可開啟,當然同樣的更簡單直接的辦法是使用Capabilities,勾選下面的三個modes。
- 2.更改推送的payload
在iOS7中,如果想要使用推送來喚醒應用執行程式碼的話,需要在payload中加入content-available,並設定為1。
{"aps":{"content-available":1,"alert":"今天是個好天氣"}}
"content-available":1 推送喚醒
"alert":"" 推送內容
"badge":1 app右上角數字
“sound”:”default” 預設聲音
aps
{
content-available: 1
alert: {...}
}
- 3.實現推送喚醒程式碼並通知系統
最後在appDelegate中實現-application:didReceiveRemoteNotification:fetchCompletionHandle:。這部分內容和上面的後臺獲取部分完全一樣,在此不再重複。
//接收到推送訊息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"remote: %@", userInfo);
//回撥
completionHandler(UIBackgroundFetchResultNewData);
//語音播報
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:userInfo[@"aps"][@"alert"]];
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
[synth speakUtterance:utterance];
}
完成以上步驟就可在後臺進行語音播報了。