Skip to content

Commit

Permalink
Add support for AirPlay (#22)
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
sindresorhus committed Sep 7, 2021
1 parent 62aa1e1 commit 1f43824
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Plug/AudioPlayer.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Cocoa
import AVFoundation
import CoreMedia
import HypeMachineAPI
import UserNotifications
import Combine
import HypeMachineAPI

typealias OnShuffleChangedSwignal = Swignal1Arg<Bool>
typealias OnTrackPlaying = Swignal1Arg<Bool>
Expand All @@ -13,7 +14,18 @@ typealias OnSkipBackward = Swignal1Arg<Bool>
final class AudioPlayer: NSObject {
static let shared = AudioPlayer()

var player: AVPlayer?
private var playerDidChangeSubject = PassthroughSubject<Void, Never>()

var playerDidChangePublisher: AnyPublisher<Void, Never> {
playerDidChangeSubject.eraseToAnyPublisher()
}

var player: AVPlayer? {
didSet {
playerDidChangeSubject.send()
}
}

var playerItem: AVPlayerItem?

var currentDataSource: TracksDataSource? {
Expand Down
38 changes: 38 additions & 0 deletions Plug/FooterViewController.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import Cocoa
import AVKit
import Combine

final class FooterViewController: NSViewController {
var volumeIcon: VolumeIconView!
var volumeSlider: NSSlider!
var shuffleButton: SwissArmyButton!

private var cancellables = Set<AnyCancellable>()
private let routeDetector = AVRouteDetector()

func toggleShuffle() {
AudioPlayer.shared.toggleShuffle()
}
Expand Down Expand Up @@ -34,6 +39,8 @@ final class FooterViewController: NSViewController {
override func loadView() {
view = NSView(frame: .zero)

routeDetector.isRouteDetectionEnabled = true

let backgroundView = DraggableVisualEffectsView()
backgroundView.blendingMode = .withinWindow
view.addSubview(backgroundView)
Expand Down Expand Up @@ -75,6 +82,37 @@ final class FooterViewController: NSViewController {
make.left.equalTo(backgroundView).offset(40)
}

let airPlayButton = AVRoutePickerView()
airPlayButton.isHidden = !routeDetector.multipleRoutesDetected
airPlayButton.isRoutePickerButtonBordered = false
airPlayButton.setRoutePickerButtonColor(.tertiaryLabelColor, for: .normal)
airPlayButton.setRoutePickerButtonColor(.secondaryLabelColor, for: .normalHighlighted)
airPlayButton.player = AudioPlayer.shared.player
backgroundView.addSubview(airPlayButton)

airPlayButton.snp.makeConstraints { make in
make.centerY.equalTo(backgroundView)
make.left.equalTo(volumeSlider).offset(70)
}

NotificationCenter.default.publisher(for: .AVRouteDetectorMultipleRoutesDetectedDidChange)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else {
return
}

airPlayButton.isHidden = !self.routeDetector.multipleRoutesDetected
}
.store(in: &cancellables)

AudioPlayer.shared.playerDidChangePublisher
.receive(on: DispatchQueue.main)
.sink {
airPlayButton.player = AudioPlayer.shared.player
}
.store(in: &cancellables)

shuffleButton = SwissArmyButton(frame: .zero)
let shuffleCell = TransparentButtonCell(textCell: "")
shuffleCell.allowsSelectedState = true
Expand Down

0 comments on commit 1f43824

Please sign in to comment.