Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(iOS): add handler for Apple Earpods play/pause command #4116

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions ios/Video/NowPlayingInfoCenterManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class NowPlayingInfoCenterManager {
private var skipBackwardTarget: Any?
private var playbackPositionTarget: Any?
private var seekTarget: Any?
private var togglePlayPauseTarget: Any?

private let remoteCommandCenter = MPRemoteCommandCenter.shared()

Expand Down Expand Up @@ -176,6 +177,21 @@ class NowPlayingInfoCenterManager {
}
return .commandFailed
}

// Handler for togglePlayPauseCommand, sent by Apple's Earpods wired headphones
togglePlayPauseTarget = remoteCommandCenter.togglePlayPauseCommand.addTarget { [weak self] _ in
guard let self, let player = self.currentPlayer else {
return .commandFailed
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    //Additional check: Is the player ready to play?
    guard player.currentItem?.status == .readyToPlay else {
        return .commandFailed
    }

@KrzysztofMoch
WDYT?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is same code than play and pause handling. Looks good to me

if player.rate == 0 {
player.play()
} else {
player.pause()
}

return .success
}
}

private func invalidateCommandTargets() {
Expand All @@ -184,6 +200,7 @@ class NowPlayingInfoCenterManager {
remoteCommandCenter.skipForwardCommand.removeTarget(skipForwardTarget)
remoteCommandCenter.skipBackwardCommand.removeTarget(skipBackwardTarget)
remoteCommandCenter.changePlaybackPositionCommand.removeTarget(playbackPositionTarget)
remoteCommandCenter.togglePlayPauseCommand.removeTarget(togglePlayPauseTarget)
}

public func updateMetadata() {
Expand Down
Loading