0
votes

I'm making an iOS radio app. I'd like to be able to use Siri to pause and play once it is already playing (Siri doesn't need to start the app, just control audio once it has already been initiated). Right now "pause" works in iOS from my phone (but not from my Watch), but I can't get it to "play" once it is paused, even though I still see the player in my lock screen. The lock screen controls work fine when tapped.

Here is some code that may be relevant:

func setupRemoteCommandCenter() {
    let commandCenter = MPRemoteCommandCenter.shared()


    // MARK: PLAY/PAUSE
    commandCenter.togglePlayPauseCommand.addTarget { event in
        self.togglePlayPause()
        return .success
    }


    // MARK: Skip
    func skipBackward(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
        guard let command = event.command as? MPSkipIntervalCommand else {
            return .noSuchContent
        }

        goBackward15()

        return .success
    }

    let skipBackwardCommand = commandCenter.skipBackwardCommand
    skipBackwardCommand.isEnabled = !playingLivestream
    skipBackwardCommand.addTarget(handler: skipBackward)
    skipBackwardCommand.preferredIntervals = [-15]

    func skipForward(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
        guard let command = event.command as? MPSkipIntervalCommand else {
            return .noSuchContent
        }

        goForward30()

        return .success
    }

    let skipForwardCommand = commandCenter.skipForwardCommand
    skipForwardCommand.isEnabled = !playingLivestream
    skipForwardCommand.addTarget(handler: skipForward)
    skipForwardCommand.preferredIntervals = [30]        
}

@objc func handleInterruption(notification: Notification) {
    guard let userInfo = notification.userInfo,
        let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
        let type = AVAudioSession.InterruptionType(rawValue: typeValue) else {
            return
    }

    if type == .began {
        print("Interruption began")
        // Interruption began, take appropriate actions
    }
    else if type == .ended {
        if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt {
            let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue)
            if options.contains(.shouldResume) {
                // Interruption Ended - playback should resume
                print("Interruption Ended - playback should resume")
                play()
            } else {
                // Interruption Ended - playback should NOT resume
                print("Interruption Ended - playback should NOT resume")
                playerPaused = true
            }
        }
    }
}

Thank you.

1
Do you normally have to add anything about Siri to enable play and pause functionality? I guess I assumed it would be attached to the MPRemoteCommandCenter.atdonsm
Matt, thanks for the response and please pardon my ignorance. Am I correct in saying that if one has set up MPCommandCenter controls, then you can control play/pause through Siri automatically? Or do I need to code in that functionality. That's the essence of my question. Thanks.atdonsm
If I'm playing audio in the app, activate Siri and say "pause", the audio pauses and she responds "OK. Paused." But if I reactivate Siri and say "play", Siri says "OK..." but the audio does not play.atdonsm
That's a great idea. I'll try that and get back to you.atdonsm
Matt, that did the trick! I really appreciate you bearing with me even though my question was initially unclear. Can you submit an answer and I'll mark it answered?atdonsm

1 Answers

1
votes

I notice you implemented playpause in the command center but not play or pause. You probably need all three of them.