日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS如何实现语音播报及后台播放

發(fā)布時(shí)間:2025/3/15 编程问答 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS如何实现语音播报及后台播放 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  最近項(xiàng)目剛剛交付,偶然間用到了語音播報(bào)和語音搜索的功能。語音搜索我用的是訊飛的demo,感覺效果還不錯(cuò),感興趣的話可以去官網(wǎng)上面下載demo,里面講的特別的詳細(xì),不過稍顯麻煩一些。語音播報(bào)訊飛也有demo,不過做開發(fā)當(dāng)然要尋求最簡潔的處理方式,ios7.0之后新添加了一些新的功能,里面就有系統(tǒng)自帶的語音播報(bào)庫AVFoundation。關(guān)于語音播報(bào)的文章其實(shí)挺多的。文本轉(zhuǎn)語音技術(shù),?也叫TTS, 是Text To Speech的縮寫. iOS如果想做有聲書等功能的時(shí)候, 會用到這門技術(shù).

  一,使用iOS自帶TTS需要注意的幾點(diǎn):

  • iOS7之后才有該功能
  • 需要 AVFoundation 庫
  • AVSpeechSynthesizer: 語音合成器, 可以假想成一個(gè)可以說話的人, 是最主要的接口
  • AVSpeechSynthesisVoice: 可以假想成人的聲音
  • AVSpeechUtterance: 可以假想成要說的一段話
  •   二,代碼示例, 播放語音

    //語音播報(bào)AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"床前明月光,疑是地上霜。"];utterance.pitchMultiplier=0.8;//中式發(fā)音AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//英式發(fā)音 // AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"];utterance.voice = voice;NSLog(@"%@",[AVSpeechSynthesisVoice speechVoices]);AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc]init];[synth speakUtterance:utterance];

      三,AVSpeechSynthesizer介紹

      這個(gè)類就像一個(gè)會說話的人, 可以”說話”, 可以”暫?!闭f話, 可以”繼續(xù)”說話, 可以判斷他當(dāng)前是否正在說話.有以下的方法或者屬性:

    • 說話: speakUtterance
    • 控制: continueSpeaking(繼續(xù)說), pauseSpeakingAtBoundary(暫停說話), paused(暫停狀態(tài)的屬性), speaking(說話的狀態(tài)), stopSpeakingAtBoundary(停止說話)
    • 委托: delegate

      四,AVSpeechBoundary介紹

    ?  這是一個(gè)枚舉. 在暫停, 或者停止說話的時(shí)候, 停下的方式用這個(gè)枚舉標(biāo)示. 包括兩種:

    • AVSpeechBoundaryImmediate: 立即停
    • AVSpeechBoundaryWord : 說完一個(gè)整詞再停

      五,AVSpeechSynthesizerDelegate介紹

      合成器的委托, 對于一些事件, 提供了響應(yīng)的接口.

    • didCancelSpeechUtterance: 已經(jīng)取消說話
    • didContinueSpeechUtterance: 已經(jīng)繼續(xù)說話
    • didFinishSpeechUtterance: 已經(jīng)說完
    • didPauseSpeechUtterance: 已經(jīng)暫停
    • didStartSpeechUtterance:已經(jīng)開始
    • willSpeakRangeOfSpeechString:將要說某段話

      六,AVSpeechSynthesisVoice介紹

      AVSpeechSynthesisVoice定義了一系列的聲音, 主要是不同的語言和地區(qū).

    • voiceWithLanguage: 根據(jù)制定的語言, 獲得一個(gè)聲音.
    • speechVoices: 獲得當(dāng)前設(shè)備支持的聲音
    • currentLanguageCode: 獲得當(dāng)前聲音的語言字符串, 比如”ZH-cn”
    • language: 獲得當(dāng)前的語言

      七,AVSpeechUtterance介紹

    ?  這個(gè)類就是一段要說的話. 主要的屬性和方法有:

    • pitchMultiplier: 音高
    • postUtteranceDelay: 讀完一段后的停頓時(shí)間
    • preUtteranceDelay: 讀一段話之前的停頓
    • rate: 讀地速度, 系統(tǒng)提供了三個(gè)速度: AVSpeechUtteranceMinimumSpeechRate, AVSpeechUtteranceMaximumSpeechRate, AVSpeechUtteranceDefaultSpeechRate
    • speechString: 要讀的字符串
    • voice: 使用的聲音, 是AVSpeechSynthesisVoice對象

      上面這些是關(guān)于語音播報(bào)的基本用法和一些屬性、方法,但是如何結(jié)合程序推送,在程序后臺運(yùn)行的時(shí)候?qū)崿F(xiàn)語音播報(bào)的效果呢?當(dāng)然還有很多需要注意的地方。

  • 1.啟用推送喚醒
  • 和上面的后臺獲取類似,更改Info.plist,在UIBackgroundModes下加入remote-notification即可開啟,當(dāng)然同樣的更簡單直接的辦法是使用Capabilities,勾選下面的三個(gè)modes。

  • 2.更改推送的payload
  • 在iOS7中,如果想要使用推送來喚醒應(yīng)用運(yùn)行代碼的話,需要在payload中加入content-available,并設(shè)置為1。

    ? {"aps":{"content-available":1,"alert":"今天是個(gè)好天氣"}}

    ?

    ??"content-available":1??推送喚醒

    ??"alert":""??推送內(nèi)容

    ??"badge":1 ??app右上角數(shù)字

     “sound”:”default” ??默認(rèn)聲音

       aps

      { ?

    ?? ?   content-available: 1

    ?? ?   alert: {...}

      }

  • 3.實(shí)現(xiàn)推送喚醒代碼并通知系統(tǒng)
  •   最后在appDelegate中實(shí)現(xiàn)-application:didReceiveRemoteNotification:fetchCompletionHandle:。這部分內(nèi)容和上面的后臺獲取部分完全一樣,在此不再重復(fù)。

    //接收到推送消息

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {

    ? ? NSLog(@"remote: %@", userInfo);

    ? ? //回調(diào)

    ? ? completionHandler(UIBackgroundFetchResultNewData);

    ? ? //語音播報(bào)

    ? ? AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:userInfo[@"aps"][@"alert"]];

    ? ? AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];

    ? ? [synth speakUtterance:utterance];

    }

      完成以上步驟就可在后臺進(jìn)行語音播報(bào)了。

    參考文章鏈接:?
    一、http://www.jianshu.com/p/174fd2673897?
    二、https://onevcat.com/2013/08/ios7-background-multitask/?
    三、http://hayageek.com/ios-silent-push-notifications/

    四、http://blog.csdn.net/u012477117/article/details/52039506

    五、http://www.cnblogs.com/luerniu/p/5901350.html

    六、https://www.oschina.net/question/2556708_2194798

    轉(zhuǎn)載于:https://www.cnblogs.com/bigant9527/p/6144292.html

    總結(jié)

    以上是生活随笔為你收集整理的iOS如何实现语音播报及后台播放的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。