Add initial version of music mode

This commit is contained in:
Arkadiusz Fal 2022-06-07 23:27:48 +02:00
parent e387db1f56
commit 35fd392307
8 changed files with 142 additions and 41 deletions

View File

@ -62,6 +62,10 @@ final class MPVBackend: PlayerBackend {
private var controlsUpdates = false
private var timeObserverThrottle = Throttle(interval: 2)
var tracks: Int {
client?.tracksCount ?? -1
}
init(model: PlayerModel, controls: PlayerControlsModel? = nil) {
self.model = model
self.controls = controls

View File

@ -50,7 +50,7 @@ final class PiPDelegate: NSObject, AVPictureInPictureControllerDelegate {
}
#endif
if !player.currentItem.isNil {
if !player.currentItem.isNil, !player.musicMode {
player?.show()
}

View File

@ -68,9 +68,15 @@ final class PlayerControlsModel: ObservableObject {
}
func hide() {
player?.backend.stopControlsUpdates()
guard let player = player,
!player.musicMode
else {
return
}
guard !(player?.currentItem.isNil ?? true) else {
player.backend.stopControlsUpdates()
guard !player.currentItem.isNil else {
return
}
@ -83,9 +89,7 @@ final class PlayerControlsModel: ObservableObject {
}
func toggle() {
withAnimation(PlayerControls.animation) {
presentingControls.toggle()
}
presentingControls ? hide() : show()
}
func reset() {
@ -101,6 +105,11 @@ final class PlayerControlsModel: ObservableObject {
#endif
removeTimer()
guard !player.musicMode else {
return
}
timer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false) { _ in
withAnimation(PlayerControls.animation) { [weak self] in
self?.presentingControls = false

View File

@ -62,6 +62,7 @@ final class PlayerModel: ObservableObject {
@Published var lastSkipped: Segment? { didSet { rebuildTVMenu() } }
@Published var restoredSegments = [Segment]()
@Published var musicMode = false
@Published var returnYouTubeDislike = ReturnYouTubeDislikeAPI()
#if os(iOS)
@ -114,7 +115,7 @@ final class PlayerModel: ObservableObject {
self.avPlayerBackend = AVPlayerBackend(model: self, controls: controls)
self.mpvBackend = MPVBackend(model: self)
self.activeBackend = Defaults[.activeBackend]
Defaults[.activeBackend] = .mpv
}
func show() {
@ -361,6 +362,12 @@ final class PlayerModel: ObservableObject {
return
}
if to == .mpv {
addVideoTrackFromStream()
} else {
musicMode = false
}
inactiveBackends().forEach { $0.pause() }
let fromBackend: PlayerBackend = from == .appleAVPlayer ? avPlayerBackend : mpvBackend
@ -561,4 +568,37 @@ final class PlayerModel: ObservableObject {
func setNeedsDrawing(_ needsDrawing: Bool) {
backends.forEach { $0.setNeedsDrawing(needsDrawing) }
}
func toggleMusicMode() {
musicMode.toggle()
if musicMode {
if playingInPictureInPicture {
avPlayerBackend.pause()
avPlayerBackend.switchToMPVOnPipClose = false
closePiP()
}
changeActiveBackend(from: .appleAVPlayer, to: .mpv)
controls.presentingControls = true
controls.removeTimer()
mpvBackend.setVideoToNo()
} else {
addVideoTrackFromStream()
mpvBackend.setVideoToAuto()
controls.resetTimer()
}
}
func addVideoTrackFromStream() {
if let videoTrackURL = stream?.videoAsset?.url,
mpvBackend.tracks < 2
{
logger.info("adding video track")
mpvBackend.addVideoTrack(videoTrackURL)
}
mpvBackend.setVideoToAuto()
}
}

View File

@ -1,10 +1,12 @@
import Foundation
import SDWebImageSwiftUI
import SwiftUI
struct PlayerControls: View {
static let animation = Animation.easeInOut(duration: 0.2)
private var player: PlayerModel!
private var thumbnails: ThumbnailsModel!
@EnvironmentObject<PlayerControlsModel> private var model
@ -20,8 +22,9 @@ struct PlayerControls: View {
@FocusState private var focusedField: Field?
#endif
init(player: PlayerModel) {
init(player: PlayerModel, thumbnails: ThumbnailsModel) {
self.player = player
self.thumbnails = thumbnails
}
var body: some View {
@ -86,10 +89,26 @@ struct PlayerControls: View {
}
#else
.background(PlayerGestures())
.background(controlsBackground)
#endif
.environment(\.colorScheme, .dark)
}
@ViewBuilder var controlsBackground: some View {
if player.musicMode,
let item = self.player.currentItem,
let url = thumbnails.best(item.video)
{
WebImage(url: url)
.resizable()
.placeholder {
Rectangle().fill(Color("PlaceholderColor"))
}
.retryOnAppear(true)
.indicator(.activity)
}
}
var timeline: some View {
TimelineView(duration: durationBinding, current: currentTimeBinding, cornerRadius: 0)
}
@ -186,9 +205,11 @@ struct PlayerControls: View {
closeVideoButton
button("Music Mode", systemImage: "music.note", active: player.musicMode, action: player.toggleMusicMode)
.disabled(player.activeBackend == .appleAVPlayer)
Spacer()
#endif
// button("Music Mode", systemImage: "music.note")
}
}
@ -355,6 +376,7 @@ struct PlayerControls: View {
systemImage: String = "arrow.up.left.and.arrow.down.right",
size: Double = 30,
cornerRadius: Double = 3,
active: Bool = false,
action: @escaping () -> Void = {}
) -> some View {
Button {
@ -367,7 +389,7 @@ struct PlayerControls: View {
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.foregroundColor(.primary)
.foregroundColor(active ? .accentColor : .primary)
.frame(width: size, height: size)
#if os(macOS)
.background(VisualEffectBlur(material: .hudWindow))
@ -396,7 +418,7 @@ struct PlayerControls_Previews: PreviewProvider {
let view = ZStack {
Color.gray
PlayerControls(player: PlayerModel())
PlayerControls(player: PlayerModel(), thumbnails: ThumbnailsModel())
.injectFixtureEnvironmentObjects()
.environmentObject(model)
}

View File

@ -48,6 +48,7 @@ struct VideoPlayerView: View {
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<PlayerControlsModel> private var playerControls
@EnvironmentObject<PlayerModel> private var player
@EnvironmentObject<ThumbnailsModel> private var thumbnails
var body: some View {
#if os(macOS)
@ -126,9 +127,7 @@ struct VideoPlayerView: View {
#else
GeometryReader { geometry in
VStack(spacing: 0) {
if player.currentItem.isNil {
playerPlaceholder(geometry: geometry)
} else if player.playingInPictureInPicture {
if player.playingInPictureInPicture {
pictureInPicturePlaceholder(geometry: geometry)
} else {
playerView
@ -140,6 +139,7 @@ struct VideoPlayerView: View {
fullScreen: playerControls.playingFullscreen
)
)
.overlay(playerPlaceholder(geometry: geometry))
#endif
}
}
@ -273,7 +273,7 @@ struct VideoPlayerView: View {
PlayerGestures()
#endif
PlayerControls(player: player)
PlayerControls(player: player, thumbnails: thumbnails)
}
#if os(iOS)
.onAppear {
@ -298,7 +298,8 @@ struct VideoPlayerView: View {
#endif
}
func playerPlaceholder(geometry: GeometryProxy) -> some View {
@ViewBuilder func playerPlaceholder(geometry: GeometryProxy) -> some View {
if player.currentItem.isNil {
ZStack(alignment: .topLeading) {
HStack {
Spacer()
@ -328,9 +329,11 @@ struct VideoPlayerView: View {
.foregroundColor(.gray)
#endif
}
.background(Color.black)
.contentShape(Rectangle())
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: geometry.size.width / Self.defaultAspectRatio)
}
}
func pictureInPicturePlaceholder(geometry: GeometryProxy) -> some View {
HStack {

View File

@ -65,6 +65,10 @@ struct VideoCell: View {
return
}
if player.musicMode {
player.toggleMusicMode()
}
if watchingNow {
if !player.playingInPictureInPicture {
player.show()

View File

@ -57,6 +57,9 @@ struct VideoContextMenuView: View {
#if os(iOS)
playNowInPictureInPictureButton
#endif
#if !os(tvOS)
playNowInMusicMode
#endif
}
Section {
@ -131,6 +134,10 @@ struct VideoContextMenuView: View {
private var playNowButton: some View {
Button {
if player.musicMode {
player.toggleMusicMode()
}
player.play(video)
} label: {
Label("Play Now", systemImage: "play")
@ -149,6 +156,18 @@ struct VideoContextMenuView: View {
}
}
private var playNowInMusicMode: some View {
Button {
if !player.musicMode {
player.toggleMusicMode()
}
player.play(video, at: watch?.timeToRestart, showingPlayer: false)
} label: {
Label("Play Music", systemImage: "music.note")
}
}
private var playNextButton: some View {
Button {
player.playNext(video)