remoteControlReceived(with event: UIEvent?) Doesn't triggered

*爱你&永不变心* 提交于 2020-01-07 04:41:08

问题


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 */
    }
}

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
}

But anyway remoteControlReceived(with event: UIEvent?) is never triggered.

Also I tried MPRemoteCommandCenter:

MPRemoteCommandCenter.shared().togglePlayPauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
    return .success
}

Doesn't triggered.

Swift or objective-c answers accepted :)

What is wrong? Or should I add something in .plist?


回答1:


Use this code to do your job. App's document can be found here

MPRemoteCommandCenter.sharedCommandCenter().togglePlayPauseCommand.addTarget(self, action: #selector(togglePlayStop))
func togglePlayStop(){
    //isPlaying = !isPlaying
}



回答2:


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])

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 :(



来源:https://stackoverflow.com/questions/44784739/remotecontrolreceivedwith-event-uievent-doesnt-triggered

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!