Add setting for closing player after playing last item (fix #98)

This commit is contained in:
Arkadiusz Fal 2022-06-25 15:14:16 +02:00
parent c940fb3198
commit 17787fa69c
7 changed files with 44 additions and 20 deletions

View File

@ -464,20 +464,24 @@ final class AVPlayerBackend: PlayerBackend {
} }
@objc func itemDidPlayToEndTime() { @objc func itemDidPlayToEndTime() {
model.prepareCurrentItemForHistory(finished: true) if Defaults[.closeLastItemOnPlaybackEnd] {
model.prepareCurrentItemForHistory(finished: true)
}
if model.queue.isEmpty { if model.queue.isEmpty {
#if !os(macOS) #if !os(macOS)
try? AVAudioSession.sharedInstance().setActive(false) try? AVAudioSession.sharedInstance().setActive(false)
#endif #endif
model.resetQueue() if Defaults[.closeLastItemOnPlaybackEnd] {
#if os(tvOS) model.resetQueue()
controller?.playerView.dismiss(animated: false) { [weak self] in #if os(tvOS)
self?.controller?.dismiss(animated: true) controller?.playerView.dismiss(animated: false) { [weak self] in
} self?.controller?.dismiss(animated: true)
#else }
model.hide() #else
#endif model.hide()
#endif
}
} else { } else {
if model.playingInPictureInPicture { if model.playingInPictureInPicture {
startPictureInPictureOnPlay = true startPictureInPictureOnPlay = true

View File

@ -376,6 +376,8 @@ final class MPVBackend: PlayerBackend {
} }
func handle(_ event: UnsafePointer<mpv_event>!) { func handle(_ event: UnsafePointer<mpv_event>!) {
logger.info(.init(stringLiteral: "RECEIVED event: \(String(cString: mpv_event_name(event.pointee.event_id)))"))
switch event.pointee.event_id { switch event.pointee.event_id {
case MPV_EVENT_SHUTDOWN: case MPV_EVENT_SHUTDOWN:
mpv_destroy(client.mpv) mpv_destroy(client.mpv)
@ -401,9 +403,11 @@ final class MPVBackend: PlayerBackend {
onFileLoaded = nil onFileLoaded = nil
case MPV_EVENT_PAUSE: case MPV_EVENT_PAUSE:
isPlaying = false
networkStateTimer.resume() networkStateTimer.resume()
case MPV_EVENT_UNPAUSE: case MPV_EVENT_UNPAUSE:
isPlaying = true
isLoadingVideo = false isLoadingVideo = false
isSeeking = false isSeeking = false
networkStateTimer.resume() networkStateTimer.resume()
@ -413,7 +417,7 @@ final class MPVBackend: PlayerBackend {
case MPV_EVENT_END_FILE: case MPV_EVENT_END_FILE:
DispatchQueue.main.async { [weak self] in DispatchQueue.main.async { [weak self] in
self?.handleEndOfFile(event) self?.handleEndOfFile()
} }
default: default:
@ -421,20 +425,26 @@ final class MPVBackend: PlayerBackend {
} }
} }
func handleEndOfFile(_: UnsafePointer<mpv_event>!) { func handleEndOfFile() {
guard handleEOF, !isLoadingVideo else { guard handleEOF, !isLoadingVideo else {
return return
} }
model.prepareCurrentItemForHistory(finished: true) getClientUpdates()
if Defaults[.closeLastItemOnPlaybackEnd] {
model.prepareCurrentItemForHistory(finished: true)
}
if model.queue.isEmpty { if model.queue.isEmpty {
#if !os(macOS) #if !os(macOS)
try? AVAudioSession.sharedInstance().setActive(false) try? AVAudioSession.sharedInstance().setActive(false)
#endif #endif
model.resetQueue()
model.hide() if Defaults[.closeLastItemOnPlaybackEnd] {
model.resetQueue()
model.hide()
}
} else { } else {
model.advanceToNextItem() model.advanceToNextItem()
} }

View File

@ -48,6 +48,7 @@ final class MPVClient: ObservableObject {
checkError(mpv_set_option_string(mpv, "cache-pause-initial", "yes")) checkError(mpv_set_option_string(mpv, "cache-pause-initial", "yes"))
checkError(mpv_set_option_string(mpv, "cache-secs", "20")) checkError(mpv_set_option_string(mpv, "cache-secs", "20"))
checkError(mpv_set_option_string(mpv, "cache-pause-wait", "2")) checkError(mpv_set_option_string(mpv, "cache-pause-wait", "2"))
checkError(mpv_set_option_string(mpv, "keep-open", "yes"))
checkError(mpv_set_option_string(mpv, "hwdec", "auto-safe")) checkError(mpv_set_option_string(mpv, "hwdec", "auto-safe"))
checkError(mpv_set_option_string(mpv, "vo", "libmpv")) checkError(mpv_set_option_string(mpv, "vo", "libmpv"))

View File

@ -41,16 +41,21 @@ extension PlayerModel {
return return
} }
self.prepareCurrentItemForHistory(finished: true)
self.pause() self.pause()
if Defaults[.closeLastItemOnPlaybackEnd] {
self.prepareCurrentItemForHistory(finished: true)
}
if self.queue.isEmpty { if self.queue.isEmpty {
#if !os(macOS) #if !os(macOS)
try? AVAudioSession.sharedInstance().setActive(false) try? AVAudioSession.sharedInstance().setActive(false)
#endif #endif
self.resetQueue() if Defaults[.closeLastItemOnPlaybackEnd] {
self.hide() self.resetQueue()
self.hide()
}
} else { } else {
self.advanceToNextItem() self.advanceToNextItem()
} }

View File

@ -63,6 +63,7 @@ extension Defaults.Keys {
static let commentsPlacement = Key<CommentsPlacement>("commentsPlacement", default: .separate) static let commentsPlacement = Key<CommentsPlacement>("commentsPlacement", default: .separate)
#endif #endif
static let pauseOnHidingPlayer = Key<Bool>("pauseOnHidingPlayer", default: defaultForPauseOnHidingPlayer) static let pauseOnHidingPlayer = Key<Bool>("pauseOnHidingPlayer", default: defaultForPauseOnHidingPlayer)
static let closeLastItemOnPlaybackEnd = Key<Bool>("closeLastItemOnPlaybackEnd", default: false)
static let closePiPOnNavigation = Key<Bool>("closePiPOnNavigation", default: false) static let closePiPOnNavigation = Key<Bool>("closePiPOnNavigation", default: false)
static let closePiPOnOpeningPlayer = Key<Bool>("closePiPOnOpeningPlayer", default: false) static let closePiPOnOpeningPlayer = Key<Bool>("closePiPOnOpeningPlayer", default: false)
@ -98,8 +99,6 @@ extension Defaults.Keys {
static let showMPVPlaybackStats = Key<Bool>("showMPVPlaybackStats", default: false) static let showMPVPlaybackStats = Key<Bool>("showMPVPlaybackStats", default: false)
static let playerDetailsPageButtonLabelStyle = Key<PlayerDetailsPageButtonLabelStyle>("playerDetailsPageButtonLabelStyle", default: defaultForPlayerDetailsPageButtonLabelStyle) static let playerDetailsPageButtonLabelStyle = Key<PlayerDetailsPageButtonLabelStyle>("playerDetailsPageButtonLabelStyle", default: defaultForPlayerDetailsPageButtonLabelStyle)
static let controlsBarInPlayer = Key<Bool>("controlsBarInPlayer", default: true)
} }
enum ResolutionSetting: String, CaseIterable, Defaults.Serializable { enum ResolutionSetting: String, CaseIterable, Defaults.Serializable {

View File

@ -43,7 +43,6 @@ struct VideoDetails: View {
@Default(.showKeywords) private var showKeywords @Default(.showKeywords) private var showKeywords
@Default(.playerDetailsPageButtonLabelStyle) private var playerDetailsPageButtonLabelStyle @Default(.playerDetailsPageButtonLabelStyle) private var playerDetailsPageButtonLabelStyle
@Default(.controlsBarInPlayer) private var controlsBarInPlayer
var currentPage: DetailsPage { var currentPage: DetailsPage {
DetailsPage.allCases.first { $0.index == page.index } ?? .info DetailsPage.allCases.first { $0.index == page.index } ?? .info

View File

@ -15,6 +15,7 @@ struct PlayerSettings: View {
@Default(.showHistoryInPlayer) private var showHistory @Default(.showHistoryInPlayer) private var showHistory
@Default(.showKeywords) private var showKeywords @Default(.showKeywords) private var showKeywords
@Default(.pauseOnHidingPlayer) private var pauseOnHidingPlayer @Default(.pauseOnHidingPlayer) private var pauseOnHidingPlayer
@Default(.closeLastItemOnPlaybackEnd) private var closeLastItemOnPlaybackEnd
#if os(iOS) #if os(iOS)
@Default(.honorSystemOrientationLock) private var honorSystemOrientationLock @Default(.honorSystemOrientationLock) private var honorSystemOrientationLock
@Default(.lockOrientationInFullScreen) private var lockOrientationInFullScreen @Default(.lockOrientationInFullScreen) private var lockOrientationInFullScreen
@ -62,6 +63,7 @@ struct PlayerSettings: View {
sourcePicker sourcePicker
qualityPicker qualityPicker
pauseOnHidingPlayerToggle pauseOnHidingPlayerToggle
closeLastItemOnPlaybackEndToggle
} }
Section(header: SettingsHeader(text: "Comments")) { Section(header: SettingsHeader(text: "Comments")) {
@ -209,6 +211,10 @@ struct PlayerSettings: View {
Toggle("Pause when player is closed", isOn: $pauseOnHidingPlayer) Toggle("Pause when player is closed", isOn: $pauseOnHidingPlayer)
} }
private var closeLastItemOnPlaybackEndToggle: some View {
Toggle("Close video after playing last in the queue", isOn: $closeLastItemOnPlaybackEnd)
}
#if os(iOS) #if os(iOS)
private var honorSystemOrientationLockToggle: some View { private var honorSystemOrientationLockToggle: some View {
Toggle("Honor orientation lock", isOn: $honorSystemOrientationLock) Toggle("Honor orientation lock", isOn: $honorSystemOrientationLock)