How can i control my headset for my music player?

前端 未结 1 776
迷失自我
迷失自我 2020-12-10 09:44

I am creating an music player iOS app and getting data from firebase. I can able to control play, pause, next and previous in simulator or iPhone. While headset is connect t

相关标签:
1条回答
  • 2020-12-10 10:19

    This document says that to receive player event notifications you need to

    1. begin playing audio
    2. be the "Now playing app"

    The definition of "Now playing app" is hard to pin down, but it seems to be any app that has an active, non-mixable audio session and is playing audio (or has very recently played audio, there seems to be a brief grace period here) . One possible non-mixable audio session is:

    let session = AVAudioSession.sharedInstance()
    do {
        try session.setCategory(AVAudioSessionCategoryPlayback)
        try session.setActive(true)
    } catch let err as NSError {
        print("Error setting up non mixable audio session \(err)")
    }
    

    p.s. if you want the headset controls to work while the screen is locked (or if you want the lockscreen controls to work for that matter), you will need to add audio to Background Modes, because technically, if the screen is locked then your app has been backgrounded:

    0 讨论(0)
提交回复
热议问题