[AXTTSCommon] _BeginSpeaking: couldn't begin playback

AVSpeechSynthesizer 后台播放时电话中断暂停和恢复播放报_BeginSpeaking: couldn't begin playback, 需要配置开启后台任务
1.在AppDelegate didFinishLaunchingWithOptions 中加入以下代码
、、、

NSError *error = NULL;

AVAudioSession *session = [AVAudioSession sharedInstance];
 
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
 
if(error) {
    // Do some error handling
     
}
 
[session setActive:YES error:&error];
 
if (error) {
    // Do some error handling
}
 
// 让app支持接受远程控制事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

、、、

2.添加属性
、、、

  @property (nonatomic, unsafe_unretained) UIBackgroundTaskIdentifier 
  backgroundTaskIdentifier;

、、、

  1. 在applicationWillResignActive中加入如下代码
    、、、

// 开启后台处理多媒体事件

 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

 AVAudioSession *session=[AVAudioSession sharedInstance];

 [session setActive:YES error:nil];

 // 后台播放

 [session setCategory:AVAudioSessionCategoryPlayback error:nil];

_backgroundTaskIdentifier=[AppDelegate backgroundPlayerID:_backgroundTaskIdentifier];

// 其中的_bgTaskId是后台任务UIBackgroundTaskIdentifier _bgTaskId;
、、、

、、、

//实现一下backgroundPlayerID:这个方法:
+(UIBackgroundTaskIdentifier)backgroundPlayerID:
(UIBackgroundTaskIdentifier)backTaskId{

 //设置并激活音频会话类别

 AVAudioSession *session=[AVAudioSession sharedInstance];

 [session setCategory:AVAudioSessionCategoryPlayback error:nil];

 [session setActive:YES error:nil];

 //允许应用程序接收远程控制

 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

 //设置后台任务ID

 UIBackgroundTaskIdentifier newTaskId=UIBackgroundTaskInvalid;

 newTaskId=[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];

 if(newTaskId!=UIBackgroundTaskInvalid&&backTaskId!=UIBackgroundTaskInvalid){

    [[UIApplication sharedApplication] endBackgroundTask:backTaskId];

 }

 return newTaskId;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容