- iPhone锁屏后 app(集成环信) 语音通话无声音(此时 app 还在运行中)解决办法
最近测试给我报了个 bug,iPhone 手机上跑我的应用时,锁屏后实时语音就没有声音了,解锁后 app 还在运行,通话也还连接着。百度了一阵一直没有类似的答案,坑爹的环信各种 bug。 然后我猜这种情况类似于环信的 demo中 静音的效果,于是找到静音的代码:
- (void)speakerOutAction
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
if (_speakerOutButton.selected) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
}else {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
[audioSession setActive:YES error:nil];
_speakerOutButton.selected = !_speakerOutButton.selected;
}
查看了一下 AVAudioSessionCategoryPlayAndRecord 和 AVAudioSessionCategoryPlayback 的文档,发现都有如下几句话
When using this category, your app audio continues with the Silent switch set to silent or when the screen locks. (The switch is called the Ring/Silent switch on iPhone.) To continue playing audio when your app transitions to the background (for example, when the screen locks), add the audio value to the UIBackgroundModes key in your information property list file.
然鹅,并没有卵用!!!
但此时我发现了另外一个东东:
App plays audio or streams audio/video using AirPlay
就在info.plist 文件中加入如下
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
App plays audio or streams audio/video using AirPlay 在 plist 的 source 中就是 audio,在 plist 模式下才显示全称
这之后重新编译,就解决了环信实时通话在锁屏后没有声音的问题,我猜测其他应用应该也可以这样解决