Skip to content

Commit

Permalink
Listen for AirPods connection changes (#1186)
Browse files Browse the repository at this point in the history
* Toggle the video player's pause or play state when airpods are connected or disconnected.

* Remove AVAudioSession route change listener in favor of the remote command center callbacks.
  • Loading branch information
chickdan authored Aug 19, 2024
1 parent c0ca96c commit e5f408a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Shared/ViewModels/VideoPlayerManager/VideoPlayerManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
// Copyright (c) 2024 Jellyfin & Jellyfin Contributors
//

import AVFoundation
import Combine
import Defaults
import Foundation
import JellyfinAPI
import MediaPlayer
import UIKit
import VLCUI

Expand Down Expand Up @@ -66,6 +68,14 @@ class VideoPlayerManager: ViewModel {
private var currentProgressWorkItem: DispatchWorkItem?
private var hasSentStart = false

private let commandCenter = MPRemoteCommandCenter.shared()

override init() {
super.init()

setupControlListeners()
}

func selectNextViewModel() {
guard let nextViewModel else { return }
currentViewModel = nextViewModel
Expand Down Expand Up @@ -285,4 +295,18 @@ class VideoPlayerManager: ViewModel {
logger.debug("sent progress task")
}
}

func setupControlListeners() {
commandCenter.pauseCommand.addTarget { [weak self] _ in
self?.proxy.pause()

return .success
}

commandCenter.playCommand.addTarget { [weak self] _ in
self?.proxy.play()

return .success
}
}
}

0 comments on commit e5f408a

Please sign in to comment.