mirror of
https://github.com/yattee/yattee.git
synced 2025-12-15 04:28:14 +00:00
Compare commits
1 Commits
refined-au
...
video-deta
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4bcd0c0a0 |
@@ -181,9 +181,7 @@ final class AVPlayerBackend: PlayerBackend {
|
|||||||
{
|
{
|
||||||
seek(to: 0, seekType: .loopRestart)
|
seek(to: 0, seekType: .loopRestart)
|
||||||
}
|
}
|
||||||
#if !os(macOS)
|
|
||||||
model.setAudioSessionActive(true)
|
|
||||||
#endif
|
|
||||||
avPlayer.play()
|
avPlayer.play()
|
||||||
|
|
||||||
// Setting hasStarted to true the first time player started
|
// Setting hasStarted to true the first time player started
|
||||||
@@ -198,9 +196,7 @@ final class AVPlayerBackend: PlayerBackend {
|
|||||||
guard avPlayer.timeControlStatus != .paused else {
|
guard avPlayer.timeControlStatus != .paused else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
#if !os(macOS)
|
|
||||||
model.setAudioSessionActive(false)
|
|
||||||
#endif
|
|
||||||
avPlayer.pause()
|
avPlayer.pause()
|
||||||
model.objectWillChange.send()
|
model.objectWillChange.send()
|
||||||
}
|
}
|
||||||
@@ -214,9 +210,6 @@ final class AVPlayerBackend: PlayerBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func stop() {
|
func stop() {
|
||||||
#if !os(macOS)
|
|
||||||
model.setAudioSessionActive(false)
|
|
||||||
#endif
|
|
||||||
avPlayer.replaceCurrentItem(with: nil)
|
avPlayer.replaceCurrentItem(with: nil)
|
||||||
hasStarted = false
|
hasStarted = false
|
||||||
}
|
}
|
||||||
@@ -371,7 +364,11 @@ final class AVPlayerBackend: PlayerBackend {
|
|||||||
|
|
||||||
let startPlaying = {
|
let startPlaying = {
|
||||||
#if !os(macOS)
|
#if !os(macOS)
|
||||||
self.model.setAudioSessionActive(true)
|
do {
|
||||||
|
try AVAudioSession.sharedInstance().setActive(true)
|
||||||
|
} catch {
|
||||||
|
self.logger.error("Error setting up audio session: \(error)")
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
self.setRate(self.model.currentRate)
|
self.setRate(self.model.currentRate)
|
||||||
|
|||||||
@@ -253,7 +253,11 @@ final class MPVBackend: PlayerBackend {
|
|||||||
|
|
||||||
let startPlaying = {
|
let startPlaying = {
|
||||||
#if !os(macOS)
|
#if !os(macOS)
|
||||||
self.model.setAudioSessionActive(true)
|
do {
|
||||||
|
try AVAudioSession.sharedInstance().setActive(true)
|
||||||
|
} catch {
|
||||||
|
self.logger.error("Error setting up audio session: \(error)")
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
DispatchQueue.main.async { [weak self] in
|
DispatchQueue.main.async { [weak self] in
|
||||||
@@ -355,9 +359,6 @@ final class MPVBackend: PlayerBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func play() {
|
func play() {
|
||||||
#if !os(macOS)
|
|
||||||
model.setAudioSessionActive(true)
|
|
||||||
#endif
|
|
||||||
startClientUpdates()
|
startClientUpdates()
|
||||||
startRefreshRateUpdates()
|
startRefreshRateUpdates()
|
||||||
|
|
||||||
@@ -386,9 +387,6 @@ final class MPVBackend: PlayerBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func pause() {
|
func pause() {
|
||||||
#if !os(macOS)
|
|
||||||
model.setAudioSessionActive(false)
|
|
||||||
#endif
|
|
||||||
stopClientUpdates()
|
stopClientUpdates()
|
||||||
stopRefreshRateUpdates()
|
stopRefreshRateUpdates()
|
||||||
|
|
||||||
@@ -410,9 +408,6 @@ final class MPVBackend: PlayerBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func stop() {
|
func stop() {
|
||||||
#if !os(macOS)
|
|
||||||
model.setAudioSessionActive(false)
|
|
||||||
#endif
|
|
||||||
stopClientUpdates()
|
stopClientUpdates()
|
||||||
stopRefreshRateUpdates()
|
stopRefreshRateUpdates()
|
||||||
client?.stop()
|
client?.stop()
|
||||||
|
|||||||
@@ -232,14 +232,6 @@ final class PlayerModel: ObservableObject {
|
|||||||
name: AVAudioSession.interruptionNotification,
|
name: AVAudioSession.interruptionNotification,
|
||||||
object: nil
|
object: nil
|
||||||
)
|
)
|
||||||
|
|
||||||
// Register for audio session route change notifications
|
|
||||||
NotificationCenter.default.addObserver(
|
|
||||||
self,
|
|
||||||
selector: #selector(handleRouteChange(_:)),
|
|
||||||
name: AVAudioSession.routeChangeNotification,
|
|
||||||
object: AVAudioSession.sharedInstance()
|
|
||||||
)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
playbackMode = Defaults[.playbackMode]
|
playbackMode = Defaults[.playbackMode]
|
||||||
@@ -258,15 +250,7 @@ final class PlayerModel: ObservableObject {
|
|||||||
|
|
||||||
#if !os(macOS)
|
#if !os(macOS)
|
||||||
deinit {
|
deinit {
|
||||||
NotificationCenter.default.removeObserver(
|
NotificationCenter.default.removeObserver(self, name: AVAudioSession.interruptionNotification, object: nil)
|
||||||
self, name: AVAudioSession.interruptionNotification, object: nil
|
|
||||||
)
|
|
||||||
|
|
||||||
NotificationCenter.default.removeObserver(
|
|
||||||
self,
|
|
||||||
name: AVAudioSession.routeChangeNotification,
|
|
||||||
object: AVAudioSession.sharedInstance()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -1292,27 +1276,12 @@ final class PlayerModel: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if !os(macOS)
|
#if !os(macOS)
|
||||||
func setAudioSessionActive(_ setActive: Bool) {
|
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
|
|
||||||
do {
|
|
||||||
try AVAudioSession.sharedInstance().setActive(setActive)
|
|
||||||
} catch {
|
|
||||||
self.logger.error("Error setting up audio session: \(error)")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleAudioSessionInterruption(_ notification: Notification) {
|
@objc func handleAudioSessionInterruption(_ notification: Notification) {
|
||||||
logger.info("Audio session interruption received.")
|
logger.info("Audio session interruption received.")
|
||||||
logger.info("Notification object: \(String(describing: notification.object))")
|
logger.info("Notification received: \(notification)")
|
||||||
|
|
||||||
guard let info = notification.userInfo else {
|
guard let info = notification.userInfo,
|
||||||
logger.info("userInfo is missing in the notification.")
|
let typeValue = info[AVAudioSessionInterruptionTypeKey] as? UInt,
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract the interruption type
|
|
||||||
guard let typeValue = info[AVAudioSessionInterruptionTypeKey] as? UInt,
|
|
||||||
let type = AVAudioSession.InterruptionType(rawValue: typeValue)
|
let type = AVAudioSession.InterruptionType(rawValue: typeValue)
|
||||||
else {
|
else {
|
||||||
logger.info("AVAudioSessionInterruptionTypeKey is missing or not a UInt in userInfo.")
|
logger.info("AVAudioSessionInterruptionTypeKey is missing or not a UInt in userInfo.")
|
||||||
@@ -1321,103 +1290,23 @@ final class PlayerModel: ObservableObject {
|
|||||||
|
|
||||||
logger.info("Interruption type received: \(type)")
|
logger.info("Interruption type received: \(type)")
|
||||||
|
|
||||||
// Check availability for iOS 14.5 or newer to handle interruption reason
|
|
||||||
// Currently only for debugging purpose
|
|
||||||
#if os(iOS)
|
|
||||||
if #available(iOS 14.5, *) {
|
|
||||||
// Extract the interruption reason, if available
|
|
||||||
if let reasonValue = info[AVAudioSessionInterruptionReasonKey] as? UInt,
|
|
||||||
let reason = AVAudioSession.InterruptionReason(rawValue: reasonValue)
|
|
||||||
{
|
|
||||||
logger.info("Interruption reason received: \(reason)")
|
|
||||||
switch reason {
|
|
||||||
case .default:
|
|
||||||
logger.info("Interruption reason: Default or unspecified interruption occurred.")
|
|
||||||
case .appWasSuspended:
|
|
||||||
logger.info("Interruption reason: The app was suspended during the interruption.")
|
|
||||||
@unknown default:
|
|
||||||
logger.info("Unknown interruption reason received.")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
logger.info("AVAudioSessionInterruptionReasonKey is missing or not a UInt in userInfo.")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
logger.info("Interruption reason handling is not available on this iOS version.")
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Handle the specific interruption type
|
|
||||||
switch type {
|
switch type {
|
||||||
case .began:
|
case .began:
|
||||||
|
logger.info("Audio session interrupted.")
|
||||||
|
// We need to call pause() to set all variables correctly, and play()
|
||||||
|
// directly afterwards, because the .began interrupt is sent after audio
|
||||||
|
// ducking ended and playback would pause. Audio ducking usually happens
|
||||||
|
// when using headphones.
|
||||||
pause()
|
pause()
|
||||||
logger.info("Audio session interrupted (began).")
|
play()
|
||||||
case .ended:
|
case .ended:
|
||||||
// Extract any interruption options, if available
|
|
||||||
if let optionsValue = info[AVAudioSessionInterruptionOptionKey] as? UInt {
|
|
||||||
logger.info("Interruption options received: \(optionsValue)")
|
|
||||||
if optionsValue & AVAudioSession.InterruptionOptions.shouldResume.rawValue != 0 {
|
|
||||||
play()
|
|
||||||
logger.info("Interruption option indicates playback should resume automatically.")
|
|
||||||
} else {
|
|
||||||
logger.info("Interruption option indicates playback should not resume automatically.")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
logger.info("AVAudioSessionInterruptionOptionKey is missing or not a UInt in userInfo.")
|
|
||||||
}
|
|
||||||
logger.info("Audio session interruption ended.")
|
logger.info("Audio session interruption ended.")
|
||||||
// Check if audio was resumed or if there's any indication of ducking
|
// We need to call pause() to set all variables correctly.
|
||||||
let currentVolume = AVAudioSession.sharedInstance().outputVolume
|
// Otherwise, playback does not resume when the interruption ends.
|
||||||
logger.info("Current output volume: \(currentVolume)")
|
pause()
|
||||||
|
play()
|
||||||
default:
|
default:
|
||||||
logger.info("Unknown interruption type received.")
|
break
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func handleRouteChange(_ notification: Notification) {
|
|
||||||
logger.info("Audio route change received.")
|
|
||||||
|
|
||||||
guard let info = notification.userInfo else {
|
|
||||||
logger.info("userInfo is missing in the notification.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let reasonValue = info[AVAudioSessionRouteChangeReasonKey] as? UInt,
|
|
||||||
let reason = AVAudioSession.RouteChangeReason(rawValue: reasonValue)
|
|
||||||
else {
|
|
||||||
logger.info("AVAudioSessionRouteChangeReasonKey is missing or not a UInt in userInfo.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info("Route change reason received: \(reason)")
|
|
||||||
|
|
||||||
let currentCategory = AVAudioSession.sharedInstance().category
|
|
||||||
logger.info("Current audio session category before change: \(currentCategory)")
|
|
||||||
|
|
||||||
switch reason {
|
|
||||||
case .categoryChange:
|
|
||||||
logger.info("Audio session category changed.")
|
|
||||||
let newCategory = AVAudioSession.sharedInstance().category
|
|
||||||
logger.info("New audio session category: \(newCategory)")
|
|
||||||
case .oldDeviceUnavailable, .newDeviceAvailable:
|
|
||||||
logger.info("Audio route change may indicate ducking or device change.")
|
|
||||||
let currentRoute = AVAudioSession.sharedInstance().currentRoute
|
|
||||||
logger.info("Current audio route: \(currentRoute)")
|
|
||||||
|
|
||||||
for output in currentRoute.outputs {
|
|
||||||
logger.info("Output port type: \(output.portType), UID: \(output.uid)")
|
|
||||||
switch output.portType {
|
|
||||||
case .headphones, .bluetoothA2DP:
|
|
||||||
logger.info("Detected port type \(output.portType). Executing play().")
|
|
||||||
play()
|
|
||||||
default:
|
|
||||||
logger.info("Detected port type \(output.portType). Executing pause().")
|
|
||||||
pause()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case .noSuitableRouteForCategory:
|
|
||||||
logger.info("No suitable route for the current category.")
|
|
||||||
default:
|
|
||||||
logger.info("Unhandled route change reason: \(reason)")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ struct PlayerBackendView: View {
|
|||||||
Color.clear
|
Color.clear
|
||||||
.onAppear { player.playerSize = proxy.size }
|
.onAppear { player.playerSize = proxy.size }
|
||||||
.onChange(of: proxy.size) { _ in player.playerSize = proxy.size }
|
.onChange(of: proxy.size) { _ in player.playerSize = proxy.size }
|
||||||
.onChange(of: player.controls.presentingOverlays) { _ in player.playerSize = proxy.size }
|
.onChange(of: player.currentItem?.id) { _ in player.playerSize = proxy.size }
|
||||||
})
|
})
|
||||||
|
|
||||||
#if !os(tvOS)
|
#if !os(tvOS)
|
||||||
|
|||||||
@@ -56,7 +56,6 @@ extension VideoPlayerView {
|
|||||||
player.seek.gestureStart = time
|
player.seek.gestureStart = time
|
||||||
}
|
}
|
||||||
let timeSeek = (time / player.playerSize.width) * horizontalDrag * seekGestureSpeed
|
let timeSeek = (time / player.playerSize.width) * horizontalDrag * seekGestureSpeed
|
||||||
|
|
||||||
player.seek.gestureSeek = timeSeek
|
player.seek.gestureSeek = timeSeek
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@@ -80,6 +79,54 @@ extension VideoPlayerView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var detailsDragGesture: some Gesture {
|
||||||
|
DragGesture(minimumDistance: 30)
|
||||||
|
.onChanged { value in
|
||||||
|
handleDetailsDragChange(value)
|
||||||
|
}
|
||||||
|
.onEnded { value in
|
||||||
|
handleDetailsDragEnd(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func handleDetailsDragChange(_ value: DragGesture.Value) {
|
||||||
|
let maxOffset = -player.playerSize.height
|
||||||
|
|
||||||
|
// Continuous drag update for smooth movement of VideoDetails
|
||||||
|
if fullScreenDetails {
|
||||||
|
// Allow only downward dragging when in fullscreen
|
||||||
|
if value.translation.height > 0 {
|
||||||
|
detailViewDragOffset = min(value.translation.height, abs(maxOffset))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Allow only upward dragging when not in fullscreen
|
||||||
|
if value.translation.height < 0 {
|
||||||
|
detailViewDragOffset = max(value.translation.height, maxOffset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func handleDetailsDragEnd(_ value: DragGesture.Value) {
|
||||||
|
if value.translation.height < -50, !fullScreenDetails {
|
||||||
|
// Swipe up to enter fullscreen
|
||||||
|
withAnimation(Constants.overlayAnimation) {
|
||||||
|
fullScreenDetails = true
|
||||||
|
detailViewDragOffset = 0
|
||||||
|
}
|
||||||
|
} else if value.translation.height > 50, fullScreenDetails {
|
||||||
|
// Swipe down to exit fullscreen
|
||||||
|
withAnimation(Constants.overlayAnimation) {
|
||||||
|
fullScreenDetails = false
|
||||||
|
detailViewDragOffset = 0
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Reset offset if drag was not significant
|
||||||
|
withAnimation(Constants.overlayAnimation) {
|
||||||
|
detailViewDragOffset = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func onPlayerDragGestureEnded() {
|
func onPlayerDragGestureEnded() {
|
||||||
if horizontalPlayerGestureEnabled, isHorizontalDrag {
|
if horizontalPlayerGestureEnabled, isHorizontalDrag {
|
||||||
isHorizontalDrag = false
|
isHorizontalDrag = false
|
||||||
@@ -108,7 +155,6 @@ extension VideoPlayerView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to temporarily disable the toggle gesture after a fullscreen change
|
|
||||||
private func disableGestureTemporarily() {
|
private func disableGestureTemporarily() {
|
||||||
disableToggleGesture = true
|
disableToggleGesture = true
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
|
||||||
|
|||||||
@@ -223,7 +223,7 @@ struct VideoDetails: View {
|
|||||||
.frame(maxWidth: .infinity, alignment: .leading)
|
.frame(maxWidth: .infinity, alignment: .leading)
|
||||||
.contentShape(Rectangle())
|
.contentShape(Rectangle())
|
||||||
.padding(.horizontal, 16)
|
.padding(.horizontal, 16)
|
||||||
// swiftlint:disable trailing_closure
|
|
||||||
// TODO: when setting tvOS minimum to 16, the platform modifier can be removed
|
// TODO: when setting tvOS minimum to 16, the platform modifier can be removed
|
||||||
#if !os(tvOS)
|
#if !os(tvOS)
|
||||||
.simultaneousGesture( // Simultaneous gesture to prioritize button tap
|
.simultaneousGesture( // Simultaneous gesture to prioritize button tap
|
||||||
@@ -234,7 +234,7 @@ struct VideoDetails: View {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
#endif
|
#endif
|
||||||
// swiftlint:enable trailing_closure
|
|
||||||
if VideoActions().isAnyActionVisible() {
|
if VideoActions().isAnyActionVisible() {
|
||||||
VideoActions(video: player.videoForDisplay)
|
VideoActions(video: player.videoForDisplay)
|
||||||
.padding(.vertical, 5)
|
.padding(.vertical, 5)
|
||||||
|
|||||||
@@ -24,13 +24,12 @@ struct VideoPlayerView: View {
|
|||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
335
|
335
|
||||||
#else
|
#else
|
||||||
200
|
140
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@State private var playerSize: CGSize = .zero { didSet { updateSidebarQueue() } }
|
@State private var playerSize: CGSize = .zero { didSet { updateSidebarQueue() } }
|
||||||
@State private var hoveringPlayer = false
|
@State private var hoveringPlayer = false
|
||||||
@State private var fullScreenDetails = false
|
|
||||||
@State private var sidebarQueue = defaultSidebarQueueValue
|
@State private var sidebarQueue = defaultSidebarQueueValue
|
||||||
|
|
||||||
@Environment(\.colorScheme) private var colorScheme
|
@Environment(\.colorScheme) private var colorScheme
|
||||||
@@ -51,12 +50,14 @@ struct VideoPlayerView: View {
|
|||||||
@State var isHorizontalDrag = false
|
@State var isHorizontalDrag = false
|
||||||
@State var isVerticalDrag = false
|
@State var isVerticalDrag = false
|
||||||
@State var viewDragOffset = Self.hiddenOffset
|
@State var viewDragOffset = Self.hiddenOffset
|
||||||
|
@State var detailViewDragOffset: Double = 0
|
||||||
// swiftlint:enable private_swiftui_state
|
// swiftlint:enable private_swiftui_state
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// swiftlint:disable private_swiftui_state
|
// swiftlint:disable private_swiftui_state
|
||||||
@State var disableToggleGesture = false
|
@State var disableToggleGesture = false
|
||||||
|
@State var fullScreenDetails = false
|
||||||
// swiftlint:enable private_swiftui_state
|
// swiftlint:enable private_swiftui_state
|
||||||
|
|
||||||
@ObservedObject var player = PlayerModel.shared // swiftlint:disable:this swiftui_state_private
|
@ObservedObject var player = PlayerModel.shared // swiftlint:disable:this swiftui_state_private
|
||||||
@@ -307,6 +308,8 @@ struct VideoPlayerView: View {
|
|||||||
#endif
|
#endif
|
||||||
.id(player.currentVideo?.cacheKey)
|
.id(player.currentVideo?.cacheKey)
|
||||||
.transition(.opacity)
|
.transition(.opacity)
|
||||||
|
.offset(y: detailViewDragOffset)
|
||||||
|
.gesture(detailsDragGesture)
|
||||||
} else {
|
} else {
|
||||||
VStack {}
|
VStack {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user