HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 在didReceiveRemoteNotification中播放声音,而在后退中,使用文字到语音功能大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我目前正在尝试的是在应用程序在后台收到远程通知时发出消息(或者可能从暂停状态唤醒).

从暂停模式唤醒应用程式后,声音根本就不会播放.

当应用程序处于前台时,会在didReceiveRemoteNotification:方法调用后立即播放声音.

当从挂起的模式唤醒应用程序时,当接收到ReemiveRemoteNotification:方法时,会立即播放声音的恰当方式是什么?

这是一些代码(语音管理器类):

-(void)textToSpeechWithmessage:(NSString*)message andLanguageCode:(NSString*)languageCode{

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
DLog(@"ActivaTing audio session");
if (![audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionMixWithOthers error:&error]) {
    DLog(@"Unable to set audio session category: %@",error);
}
BOOL result = [audioSession setActive:YES error:&error];
if (!result) {
    DLog(@"Error activaTing audio session: %@",error);

}else{
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:message];

    [utterance setRate:0.5f];

    [utterance setVolume:0.8f];

    utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:languageCode];

    [self.synthesizer speakUtterance:utterance];
}

}

-(void)textToSpeechWithmessage:(NSString*)message{

[self textToSpeechWithmessage:message andLanguageCode:[[NSLocale preferredLanguages] objectATindex:0]];

}

后来在AppDelegate中:

[[MCSpeechManager sharedInstance] textToSpeechWithmessage:messageText];

我启用了功能 – 背景模式部分中的音频,AirPlay和画中画选项.

编辑:

也许我应该开始一个后台任务并运行到期处理程序,如果需要的话?我想这可能有效,但也想听听解决这种情况的常见方式.

此外,当我在后台收到一个通知时,我得到下一个错误

代码561015905适用于:

它被描述为:

但是我收到与其他类别相同的错误(AVAudioSessionCategoryAmbient和AVAudioSessionCategorySoloAmbient)

解决方法

由于我无法重现您所描述的错误,请让我提供几个指针和一些代码.

>您是否针对最新的SDK进行构建/测试/运行? iOS X中的通知机制有重大变化
>我必须假设对didReceiveRemoteNotification的调用必须发生在响应于所述通知用户操作,例如点击通知消息.
>没有必要设置任何背景模式保存应用程序下载内容以响应推送通知.

如果上述所有陈述都是真实的,那么现在的答案将集中在通知到达时会发生什么.

>设备接收通知

>用户点击消息
>应用程式启动
> didReceiveRemoteNotification被调用

在步骤4,textToSpeechWithmessage按预期工作:

func application(_ application: UIApplication,didReceiveRemoteNotification
                 userInfo: [AnyHashable : Any],fetchCompletionHandler completionHandler:
                 @escaping (UIBACkgroundFetchResult) -> Void) {
    textToSpeechWithmessage(message: "Speak up","en-US")
}

为了简单起见,我使用OneSignal连接通知

import Onesignal
...
_ = Onesignal.init(launchOptions: launchOptions,appId: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
// or
_ = Onesignal.init(launchOptions: launchOptions,appId: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
                   {
                       (s:string?,t:[AnyHashable : Any]?,u:Bool) in
                       self.textToSpeechWithmessage(message: "OneDignal","en-US")
                   }

textToSpeechWithmessage大部分是不变的,这里是Swift 3的完整性:

import AVFoundation
...
let synthesizer = AVSpeechSynthesizer()
func textToSpeechWithmessage(message:string,_ languageCode:string)
{
    let audioSession = AVAudioSession.sharedInstance()

    print("ActivaTing audio session")
    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord,with: [AVAudioSessionCategoryOptions.defaultToSpeaker,AVAudioSessionCategoryOptions.mixWithOthers]
        )
        try audioSession.setActive(true)

        let utterance = AVSpeechUtterance(String:messagE)
        utterance.rate = 0.5
        utterance.volume = 0.8
        utterance.voice = AVSpeechSynthesisVoice(language: languageCodE)
        self.synthesizer.speak(utterancE)

    } catch {
        print("Unable to set audio session category: %@",error);
    }
}

大佬总结

以上是大佬教程为你收集整理的ios – 在didReceiveRemoteNotification中播放声音,而在后退中,使用文字到语音功能全部内容,希望文章能够帮你解决ios – 在didReceiveRemoteNotification中播放声音,而在后退中,使用文字到语音功能所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。