类别 |
输入 |
输出 |
与ipOD混合 |
遵从静音 |
AVAudioSessionCategoryAmbient |
No |
Yes |
Yes |
Yes |
AVAudioSessionCategorySoloAmbient |
No |
Yes |
No |
Yes |
AVAudioSessionCategoryPlayback |
No |
Yes |
No |
No |
AVAudioSessionCategoryRecord |
Yes |
No |
No |
No |
AVAudioSessionCategoryPlayAndRecord |
Yes |
Yes |
No |
No |
+ (UIBackgroundTaskIdentifier)backgroundPlayerID:(UIBackgroundTaskIdentifier)backTaskId { // 1. 设置并激活音频会话类别 AVAudioSession *session = [AVAudioSession sharedInstance]; [session AVAudioSessionCategoryPlayback error:nil]; [session setActive:YES error:nil]; // 2. 允许应用程序接收远程控制 [[UIapplication sharedApplication] beginReceivingRemoteControlEvents]; // 3. 设置后台任务ID UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid; newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]; if (newTaskId != UIBackgroundTaskInvalid && backTaskId != UIBackgroundTaskInvalid) { [[UIApplication sharedApplication] endBackgroundTask:backTaskId]; } return newTaskId; }
//后台播放音频设置 AVAudioSession *session = [AVAudioSession sharedInstance]; [session setActive:YES error:nil]; [session setCategory:AVAudioSessionCategoryPlayback error:nil]; //让app支持接受远程控制事件 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// 后台播放任务Id UIBackgroundTaskIdentifier _bgTaskId; // 设置音频会话,允许后台播放 _bgTaskId = [SoundTool backgroundPlayerID:_bgTaskId];
作者: 清澈Saup
出处: http://www.cnblogs.com/qingche/
本文版权归作者和博客园共有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接。