问题描述
我有录音/播放应用程序.但是,当用户使用常规的有线iPhone耳机上的播放/暂停按钮时,我想暂停播放.所以我实现了远程事件的处理:
I have audio recording/playing app. But I want to pause playing, when user uses play/pause button on the regular wired iPhone headset.So i implemented handling of remote events:
// MARK: Overrides
internal extension AppDelegate {
override func remoteControlReceived(with event: UIEvent?) {
super.remoteControlReceived(with: event)
/* some other logic */
}
}
然后我开始在application: didFinishLaunchingWithOptions:
中接收远程事件:
Then I started receiving remote events in application: didFinishLaunchingWithOptions:
:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
application.beginReceivingRemoteControlEvents()
becomeFirstResponder()
/* some other logic */
return true
}
但是无论如何都不会触发remoteControlReceived(with event: UIEvent?)
.
But anyway remoteControlReceived(with event: UIEvent?)
is never triggered.
我也尝试了MPRemoteCommandCenter:
Also I tried MPRemoteCommandCenter:
MPRemoteCommandCenter.shared().togglePlayPauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
return .success
}
没有触发.
接受快速或客观答案:)
Swift or objective-c answers accepted :)
怎么了?还是应该在.plist中添加一些内容?
What is wrong? Or should I add something in .plist?
推荐答案
好,我找到了原因....我正在使用我的应用程序将声音与其他应用程序混合,因此我要通过mixWithOthers
选项:
Ok, I found the reason.... I'm using my app to mix it's sound with other apps, so I'm activating audio session with mixWithOthers
option:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [.mixWithOthers])
但是,如果我使用的是mixWithOthers
,则应用会停止接收远程事件.这意味着,如果我想从耳机接收toggle play/pause
事件,则不能使用此选项:
But, if I'm using mixWithOthers
, app stops to receive remote events. That means, that if I want to receive toggle play/pause
event from headset, I can't use this option:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
这很奇怪,但这就是我们所拥有的.因此,就我而言,我不能使用耳机按钮:(
This is weird, but this is what we have. So in my case I can't use headset buttons :(
这篇关于remoteControlReceived(带有事件:UIEvent?)未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!