remoteControlReceivedWithEvent called on iOS 7.0 device but not iOS 8.0

后端 未结 2 663
情深已故
情深已故 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

    Finally figured out the issue I was having. Ultimately it seemed that the events from the Remote Control on the home screen were never making it into my app and down to my view controllers. I ended up subclassing the UIWindow so that I could see what events were making their way through the chain. As UIWindow is a UIResponder I also added the - (void)remoteControlReceivedWithEvent:(UIEvent *)event to the subclass. Then in the makeKeyAndVisible I added the:

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
    

    I started up the debugger and the -(void)makeKeyAndVisible was never called! I then searched my app delegate for the window member variable and the [window makeKeyAndVisible]; line was nowhere to be found! I added it back in (as it should have been there) and presto events are routing to the correct locations like magic. Why this was working on some versions of iOS and not others and without any other noticeable issues is beyond me.

    Hope this helps someone out in the future.

    0 讨论(0)
  • 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...")
    }
    
    0 讨论(0)
提交回复
热议问题