我有一个应用程序,可以显示视频,我想下面的行为。
本质上,我希望能够在“混合”和“非混合”之间切换我的应用程序的音频模式。
下面是一些代码,展示了我是如何修改AVAudioSession以获得这个功能的。
开启混合模式
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryAmbient
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];
[session setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];关闭混合模式
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback
withOptions:0
error:nil];
[session setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];我遇到的问题是,在以前关闭音频之后,打开音频混合,不会通知其他应用程序他们可以再次播放音频。我真的非常想要那样的行为。那是不可能的事。
这似乎是通知其他应用程序可以恢复的唯一方法是将我的应用程序的音频会话设置为“非活动”。不幸的是,在播放视频时,会在控制台中打印一个警告,要求在停用音频会话之前停止播放音频。这也有基本破坏AVPlayer的效果,因为它不能再播放任何音频。
有人知道如何解决这些问题吗?任何解决方案都是受欢迎的,即使是非常复杂的解决方案或使用私有API的解决方案也是如此。
发布于 2019-01-08 12:37:39
因此,正如其他答案所指出的那样,您可以“暂停”另一个应用程序的音频。但是,您可以通过使用.duckWithOthers来拒绝它。一旦您的音频完成,您可以重新设置该选项为.mixWithOthers,并通知其他应用程序,他们的音量可以恢复到完整的水平。
假设您的音频在ViewController出现时启动,当ViewController消失时停止。然后可以使用以下代码:
override func viewWillAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Use duckOthers so the audio in your VC is slightly louder than the background audio such as Spotify
updateAVAudioSessionOptions(to: .duckOthers)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Reset to mixWithOthers since we are no longer playing audio
updateAVAudioSessionOptions(to: .mixWithOthers)
// probably a good idea to stop the audio as well
player.pause()
}
private func updateAVAudioSessionOptions(to options: AVAudioSession.CategoryOptions) {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: options)
if options == .mixWithOthers {
// Notify other apps that they volume can be restored to the full level
try AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
}
} catch let error {
print("Error: \(error)")
}
}发布于 2015-10-28 23:35:40
我觉得你运气不好。即使是AVAudioSessionCategoryOptionDuckOthers,听起来应该适合您的用例,也要求您停用会话才能将其他应用程序的音量恢复到正常,但正如您所看到的,这会导致错误。
发布于 2015-10-28 09:41:29
也许你应该在你的“关闭混合模式”块中将“是”改为“否”。
[session setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];https://stackoverflow.com/questions/33386405
复制相似问题