2

I implement an audio player using AVAudioPlayer as the following.

 class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient, with: AVAudioSessionCategoryOptions.allowBluetooth)
        try?  AVAudioSession.sharedInstance().setActive(true)

        if let path = Bundle.main.path(forResource: "LadyGaga-MillionReasons", ofType: "mp3") {
            let url = URL(string:path)
            self.audioPlayer = try? AVAudioPlayer(contentsOf: url!)
            self.audioPlayer?.prepareToPlay()
        }
        self.becomeFirstResponder()
        UIApplication.shared.beginReceivingRemoteControlEvents()
    }

    override var canBecomeFirstResponder: Bool{
        get{
            return true
        }
    }

    override var canResignFirstResponder: Bool{
        get{
            return true
        }
    }

    @IBAction func btnPlay_TouchUpInsde(_ sender: Any) {
        if (self.audioPlayer?.isPlaying)! {
            self.audioPlayer?.pause()
            self.btnPlay.setTitle("Play", for: .normal)
        }else{
            self.audioPlayer?.play()
            self.btnPlay.setTitle("Pause", for: .normal)
        }
    }

    override func remoteControlReceived(with event: UIEvent?) {
        if let e = event , e.type == .remoteControl {
            if  e.subtype == UIEventSubtype.remoteControlPause {
                self.audioPlayer?.pause()
            }else if(e.subtype == .remoteControlPlay){
                self.audioPlayer?.play()
            }else if(e.subtype == .remoteControlTogglePlayPause){
                if self.audioPlayer!.isPlaying {
                    self.audioPlayer?.pause()
                }else{
                    self.audioPlayer?.play()
                }
            }
        }
    }

}

The app is launched, then click the play button, the audio plays ok. Then I pause the audio by a headset,and all works ok.

Another situation: The app is launched, then I want to start the audio by a headset, it does not work.

It seems to that the view is not the first responder before I click the button, even I add self.becomeFirstResponder in init function.

Who know the why the app can not get the remoteControlReceived event when do not click the button.

I implement a sample. https://github.com/leogeng/AuidoTest.git

Leo
  • 835
  • 1
  • 12
  • 31
  • you probably meant to override `canBecomeFirstResponder` rather than `becomeFirstResponder`. However, I think your problem is probably just that until you actually start playing audio, you won't be the "now playing" app, and therefore won't receive remote control events even if you are the first responder. You could try pausing the audio in `init` (not sure if you have to play first) and see if you can then start it with the remote – CupawnTae Dec 03 '16 at 18:33
  • 1. canBecomeFirstResponder is a variable in UIReponder(swift 3),can not be override. 2.I have tried to call pausing the audio in init, and does not work, and call play function in init, everything works ok. – Leo Dec 04 '16 at 12:42
  • it is a var but to become first responder you must override its getter method, see https://developer.apple.com/reference/uikit/uiresponder/1621130-canbecomefirstresponder - "Subclasses must override this method to be able to become first responder". You only need to override `becomeFirstResponder` if you want to *do something* when you become first responder. Sorry the suggestion didn't work - did you try playing and then pausing immediately (or asynchronously after a delay maybe)? – CupawnTae Dec 04 '16 at 14:52
  • related question http://stackoverflow.com/questions/10885047/receive-remote-control-events-without-audio – CupawnTae Dec 04 '16 at 15:02

0 Answers0