[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;

}

你可能感兴趣的:([AXTTSCommon] _BeginSpeaking: couldn't begin playback)