Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Handle shuffle and repeat modes from CarPlay.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-T committed Jul 15, 2021
1 parent e00524b commit 995ac57
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class PlaylistMediaStreamer {
// MPNowPlayingInfoPropertyDefaultPlaybackRate: 1.0,
MPNowPlayingInfoPropertyAssetURL: URL(string: item.pageSrc) as Any,
MPNowPlayingInfoPropertyElapsedPlaybackTime: 0.0, //player.currentTime.seconds
//MPNowPlayingInfoPropertyPlaybackQueueIndex: 0,
//MPNowPlayingInfoPropertyPlaybackQueueCount: 0
]
}

Expand Down
35 changes: 33 additions & 2 deletions Client/Frontend/Browser/Playlist/VideoPlayer/MediaPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class MediaPlayer: NSObject {
case repeatAll
}

public enum ShuffleMode: CaseIterable {
case none
case items
case collection
}

// MARK: - Public Variables

private(set) public var isSeeking = false
Expand All @@ -27,7 +33,8 @@ class MediaPlayer: NSObject {
private(set) public var pendingMediaItem: AVPlayerItem?
private(set) public var pictureInPictureController: AVPictureInPictureController?
private(set) var repeatState: RepeatMode = .none
private(set) var previousRate: Float = 0.0
private(set) var shuffleState: ShuffleMode = .none
private(set) var previousRate: Float = -1.0

public var isPlaying: Bool {
// It is better NOT to keep tracking of isPlaying OR rate > 0.0
Expand Down Expand Up @@ -63,12 +70,19 @@ class MediaPlayer: NSObject {
super.init()

playerLayer.player = self.player

// Register for notifications
registerNotifications()
registerControlCenterNotifications()
registerPictureInPictureNotifications()

// For now, disable shuffling
MPRemoteCommandCenter.Command.changeShuffleModeCommand.command.isEnabled = false

// Start receiving remote commands
UIApplication.shared.beginReceivingRemoteControlEvents()

// Enable our audio session
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
Expand All @@ -78,19 +92,22 @@ class MediaPlayer: NSObject {
}

deinit {
// Unregister for notifications
notificationObservers.removeAll()

if let periodicTimeObserver = periodicTimeObserver {
player.removeTimeObserver(periodicTimeObserver)
}

// Disable our audio session
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, policy: .default, options: [])
try AVAudioSession.sharedInstance().setActive(false)
} catch {
log.error(error)
}

// Stop receiving remote commands
UIApplication.shared.endReceivingRemoteControlEvents()
}

Expand Down Expand Up @@ -151,7 +168,7 @@ class MediaPlayer: NSObject {
func play() {
if !isPlaying {
player.play()
player.rate = previousRate
player.rate = previousRate < 0.0 ? 1.0 : previousRate
playSubscriber.send(EventNotification(mediaPlayer: self, event: .play))
}
}
Expand Down Expand Up @@ -231,12 +248,16 @@ class MediaPlayer: NSObject {
}

func toggleRepeatMode() {
let command = MPRemoteCommandCenter.shared().changeRepeatModeCommand
switch repeatState {
case .none:
command.currentRepeatType = .off
self.repeatState = .repeatOne
case .repeatOne:
command.currentRepeatType = .one
self.repeatState = .repeatAll
case .repeatAll:
command.currentRepeatType = .all
self.repeatState = .none
}

Expand All @@ -245,6 +266,16 @@ class MediaPlayer: NSObject {
}

func toggleShuffleMode() {
let command = MPRemoteCommandCenter.shared().changeShuffleModeCommand
switch shuffleState {
case .none:
command.currentShuffleType = .items
case .items:
command.currentShuffleType = .collections
case .collection:
command.currentShuffleType = .off
}

changeShuffleModeSubscriber.send(EventNotification(mediaPlayer: self,
event: .changeShuffleMode))
}
Expand Down

0 comments on commit 995ac57

Please sign in to comment.