Fixes for MPV in macOS

This commit is contained in:
Arkadiusz Fal
2022-02-27 21:31:17 +01:00
parent 12a7d8cfd6
commit 8e0a710a53
55 changed files with 691 additions and 245 deletions

View File

@@ -166,9 +166,11 @@ struct ContentView: View {
}
func setupNowPlayingInfoCenter() {
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback)
#if !os(macOS)
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback)
UIApplication.shared.beginReceivingRemoteControlEvents()
UIApplication.shared.beginReceivingRemoteControlEvents()
#endif
MPRemoteCommandCenter.shared().playCommand.addTarget { _ in
player.play()

View File

@@ -1,7 +1,7 @@
import Defaults
import SwiftUI
struct AVPlayerView: UIViewControllerRepresentable {
struct AppleAVPlayerView: UIViewControllerRepresentable {
@EnvironmentObject<CommentsModel> private var comments
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerModel> private var player

View File

@@ -8,7 +8,9 @@ struct PlayerControls: View {
@EnvironmentObject<PlayerControlsModel> private var model
@Environment(\.verticalSizeClass) private var verticalSizeClass
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
init(player: PlayerModel) {
self.player = player
@@ -84,7 +86,9 @@ struct PlayerControls: View {
var statusBar: some View {
HStack(spacing: 4) {
hidePlayerButton
#if os(iOS)
hidePlayerButton
#endif
Text(playbackStatus)
Spacer()
@@ -92,6 +96,9 @@ struct PlayerControls: View {
ToggleBackendButton()
Text("")
StreamControl()
#if os(macOS)
.frame(maxWidth: 160)
#endif
}
.foregroundColor(.primary)
.padding(.trailing, 4)
@@ -215,6 +222,7 @@ struct PlayerControls: View {
.padding()
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.foregroundColor(.primary)
.frame(width: size, height: size)
#if os(macOS)
@@ -226,7 +234,11 @@ struct PlayerControls: View {
}
var fullScreenLayout: Bool {
model.playingFullscreen || verticalSizeClass == .compact
#if !os(macOS)
model.playingFullscreen || verticalSizeClass == .compact
#else
model.playingFullscreen
#endif
}
}

View File

@@ -16,7 +16,8 @@ final class MPVViewController: UIViewController {
override func viewDidLoad() {
super.loadView()
glView = client.create(frame: view.frame)
client.create(frame: view.frame)
glView = client.glView
view.addSubview(glView)

View File

@@ -4,7 +4,7 @@ import SwiftUI
struct VideoDetailsPaddingModifier: ViewModifier {
static var defaultAdditionalDetailsPadding: Double {
#if os(macOS)
30
5
#else
10
#endif

View File

@@ -17,6 +17,7 @@ struct VideoPlayerView: View {
}
@State private var playerSize: CGSize = .zero
@State private var hoveringPlayer = false
@State private var fullScreenDetails = false
@Environment(\.colorScheme) private var colorScheme
@@ -32,6 +33,8 @@ struct VideoPlayerView: View {
@State private var motionManager: CMMotionManager!
@State private var orientation = UIInterfaceOrientation.portrait
@State private var lastOrientation: UIInterfaceOrientation?
#elseif os(macOS)
var mouseLocation: CGPoint { NSEvent.mouseLocation }
#endif
@EnvironmentObject<AccountsModel> private var accounts
@@ -95,12 +98,6 @@ struct VideoPlayerView: View {
#else
GeometryReader { geometry in
VStack(spacing: 0) {
if !playerControls.playingFullscreen {
#if os(macOS)
PlaybackBar()
#endif
}
if player.currentItem.isNil {
playerPlaceholder(geometry: geometry)
} else if player.playingInPictureInPicture {
@@ -140,22 +137,33 @@ struct VideoPlayerView: View {
}
}
.frame(maxWidth: fullScreenLayout ? .infinity : nil, maxHeight: fullScreenLayout ? .infinity : nil)
.onHover { hovering in
hoveringPlayer = hovering
hovering ? playerControls.show() : playerControls.hide()
}
#if os(iOS)
.onSwipeGesture(
up: {
withAnimation {
fullScreenDetails = true
}
},
down: { player.hide() }
)
.onHover { hovering in
hovering ? playerControls.show() : playerControls.hide()
.onSwipeGesture(
up: {
withAnimation {
fullScreenDetails = true
}
},
down: { player.hide() }
)
#elseif os(macOS)
.onAppear(perform: {
NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved]) {
if hoveringPlayer {
playerControls.resetTimer()
}
return $0
}
})
#endif
.background(Color.black)
.background(Color.black)
if !playerControls.playingFullscreen {
Group {
@@ -197,12 +205,18 @@ struct VideoPlayerView: View {
}
}
.ignoresSafeArea(.all, edges: fullScreenLayout ? .vertical : Edge.Set())
.statusBar(hidden: playerControls.playingFullscreen)
.navigationBarHidden(true)
#if !os(macOS)
.statusBar(hidden: playerControls.playingFullscreen)
.navigationBarHidden(true)
#endif
}
var fullScreenLayout: Bool {
playerControls.playingFullscreen || verticalSizeClass == .compact
#if !os(macOS)
playerControls.playingFullscreen || verticalSizeClass == .compact
#else
playerControls.playingFullscreen
#endif
}
func playerPlaceholder(geometry: GeometryProxy) -> some View {

View File

@@ -2,10 +2,10 @@ import SwiftUI
#if !os(macOS)
struct MPVPlayerView: UIViewControllerRepresentable {
@EnvironmentObject<PlayerModel> private var player
@State private var controller = MPVViewController()
@EnvironmentObject<PlayerModel> private var player
func makeUIViewController(context _: Context) -> some UIViewController {
player.mpvBackend.controller = controller
player.mpvBackend.client = controller.client
@@ -17,15 +17,23 @@ import SwiftUI
}
#else
struct MPVPlayerView: NSViewRepresentable {
let layer: VideoLayer
@State private var client = MPVClient()
@State private var layer = VideoLayer()
@EnvironmentObject<PlayerModel> private var player
func makeNSView(context _: Context) -> some NSView {
let vview = VideoView()
client.layer = layer
layer.client = client
vview.layer = layer
vview.wantsLayer = true
let view = MPVOGLView()
return vview
view.layer = client.layer
view.wantsLayer = true
player.mpvBackend.client = client
return view
}
func updateNSView(_: NSViewType, context _: Context) {}