From 9c38d9f4ef42c3e275ee39a08aa227e6b976fdb2 Mon Sep 17 00:00:00 2001 From: Wojciech Ogrodowczyk Date: Thu, 29 Aug 2024 12:28:27 +0200 Subject: [PATCH] fix(ios): Add handler for Earpods play/pause command (#4116) --- ios/Video/NowPlayingInfoCenterManager.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ios/Video/NowPlayingInfoCenterManager.swift b/ios/Video/NowPlayingInfoCenterManager.swift index 63e0e15f..153a5daf 100644 --- a/ios/Video/NowPlayingInfoCenterManager.swift +++ b/ios/Video/NowPlayingInfoCenterManager.swift @@ -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() @@ -172,6 +173,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 + } + + if player.rate == 0 { + player.play() + } else { + player.pause() + } + + return .success + } } private func invalidateCommandTargets() { @@ -180,6 +196,7 @@ class NowPlayingInfoCenterManager { remoteCommandCenter.skipForwardCommand.removeTarget(skipForwardTarget) remoteCommandCenter.skipBackwardCommand.removeTarget(skipBackwardTarget) remoteCommandCenter.changePlaybackPositionCommand.removeTarget(playbackPositionTarget) + remoteCommandCenter.togglePlayPauseCommand.removeTarget(togglePlayPauseTarget) } public func updateNowPlayingInfo() {