remoteControlReceivedWithEvent called on iOS 7.0 device but not iOS 8.0

后端 未结 2 661
情深已故
情深已故 2021-01-18 21:41

I have an application that plays audio in the background. I am trying to fix a bug where the audio controls (play/pause), on the home screen (etc.), DO NOT work on iOS 8.0+

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 22:36

    In your ViewController add

    override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
    
            UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
            self.becomeFirstResponder()
    }
    
    override func remoteControlReceivedWithEvent(event: UIEvent) {
            // your stuff
        }
    

    In AppDelegate add

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
    var error: NSError?
            AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: &error)
            AVAudioSession.sharedInstance().setActive(true, error: &error)
    }
    

    SWIFT 3

    UIApplication.shared.beginReceivingRemoteControlEvents()
    self.becomeFirstResponder()
    
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)
    } catch {
        print("hmmm...")
    }
    

提交回复
热议问题