- iPhone鎖屏后 app(集成環(huán)信) 語音通話無聲音(此時 app 還在運(yùn)行中)解決辦法
最近測試給我報了個 bug,iPhone 手機(jī)上跑我的應(yīng)用時,鎖屏后實(shí)時語音就沒有聲音了,解鎖后 app 還在運(yùn)行,通話也還連接著。百度了一陣一直沒有類似的答案,坑爹的環(huán)信各種 bug浆竭。 然后我猜這種情況類似于環(huán)信的 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 的文檔惨寿,發(fā)現(xiàn)都有如下幾句話
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.
然鵝邦泄,并沒有卵用!A芽选顺囊!
但此時我發(fā)現(xiàn)了另外一個東東:
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 模式下才顯示全稱
這之后重新編譯蕉拢,就解決了環(huán)信實(shí)時通話在鎖屏后沒有聲音的問題特碳,我猜測其他應(yīng)用應(yīng)該也可以這樣解決