mirror of
https://github.com/yattee/yattee.git
synced 2025-01-08 22:07:10 +00:00
Set AVAudioSession, disable screensaver during playback on macOS
This commit is contained in:
parent
ca8298a9e9
commit
68b5abd122
@ -47,11 +47,19 @@ final class PlayerModel: ObservableObject {
|
|||||||
private var shouldResumePlaying = true
|
private var shouldResumePlaying = true
|
||||||
private var statusObservation: NSKeyValueObservation?
|
private var statusObservation: NSKeyValueObservation?
|
||||||
|
|
||||||
|
#if os(macOS)
|
||||||
|
var playerTimeControlStatusObserver: Any?
|
||||||
|
#endif
|
||||||
|
|
||||||
init(accounts: AccountsModel? = nil, instances: InstancesModel? = nil) {
|
init(accounts: AccountsModel? = nil, instances: InstancesModel? = nil) {
|
||||||
self.accounts = accounts ?? AccountsModel()
|
self.accounts = accounts ?? AccountsModel()
|
||||||
self.instances = instances ?? InstancesModel()
|
self.instances = instances ?? InstancesModel()
|
||||||
addItemDidPlayToEndTimeObserver()
|
addItemDidPlayToEndTimeObserver()
|
||||||
addTimeObserver()
|
addTimeObserver()
|
||||||
|
|
||||||
|
#if os(macOS)
|
||||||
|
addPlayerTimeControlStatusObserver()
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
func presentPlayer() {
|
func presentPlayer() {
|
||||||
@ -123,6 +131,9 @@ final class PlayerModel: ObservableObject {
|
|||||||
of video: Video,
|
of video: Video,
|
||||||
preservingTime: Bool = false
|
preservingTime: Bool = false
|
||||||
) {
|
) {
|
||||||
|
#if !os(macOS)
|
||||||
|
try? AVAudioSession.sharedInstance().setActive(false)
|
||||||
|
#endif
|
||||||
resetSegments()
|
resetSegments()
|
||||||
sponsorBlock.loadSegments(videoID: video.videoID)
|
sponsorBlock.loadSegments(videoID: video.videoID)
|
||||||
|
|
||||||
@ -172,6 +183,10 @@ final class PlayerModel: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let startPlaying = {
|
let startPlaying = {
|
||||||
|
#if !os(macOS)
|
||||||
|
try? AVAudioSession.sharedInstance().setActive(true)
|
||||||
|
#endif
|
||||||
|
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
|
||||||
self.play()
|
self.play()
|
||||||
}
|
}
|
||||||
@ -302,6 +317,10 @@ final class PlayerModel: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@objc func itemDidPlayToEndTime() {
|
@objc func itemDidPlayToEndTime() {
|
||||||
|
#if !os(macOS)
|
||||||
|
try? AVAudioSession.sharedInstance().setActive(false)
|
||||||
|
#endif
|
||||||
|
|
||||||
if queue.isEmpty {
|
if queue.isEmpty {
|
||||||
addCurrentItemToHistory()
|
addCurrentItemToHistory()
|
||||||
resetQueue()
|
resetQueue()
|
||||||
@ -360,4 +379,19 @@ final class PlayerModel: ObservableObject {
|
|||||||
self.handleSegments(at: time)
|
self.handleSegments(at: time)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if os(macOS)
|
||||||
|
private func addPlayerTimeControlStatusObserver() {
|
||||||
|
playerTimeControlStatusObserver = player.observe(\.timeControlStatus) { player, _ in
|
||||||
|
guard self.player == player else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if player.timeControlStatus == .playing {
|
||||||
|
ScreenSaverManager.shared.disable(reason: "Yattee is playing video")
|
||||||
|
} else {
|
||||||
|
ScreenSaverManager.shared.enable()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
34
Model/Player/ScreenSaverManager.swift
Normal file
34
Model/Player/ScreenSaverManager.swift
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import Foundation
|
||||||
|
import IOKit.pwr_mgt
|
||||||
|
|
||||||
|
struct ScreenSaverManager {
|
||||||
|
static var shared = ScreenSaverManager()
|
||||||
|
|
||||||
|
var noSleepAssertion: IOPMAssertionID = 0
|
||||||
|
var noSleepReturn: IOReturn?
|
||||||
|
|
||||||
|
var enabled: Bool {
|
||||||
|
noSleepReturn == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult mutating func disable(reason: String = "Unknown reason") -> Bool {
|
||||||
|
guard enabled else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
noSleepReturn = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep as CFString,
|
||||||
|
IOPMAssertionLevel(kIOPMAssertionLevelOn),
|
||||||
|
reason as CFString,
|
||||||
|
&noSleepAssertion)
|
||||||
|
return noSleepReturn == kIOReturnSuccess
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult mutating func enable() -> Bool {
|
||||||
|
if noSleepReturn != nil {
|
||||||
|
_ = IOPMAssertionRelease(noSleepAssertion) == kIOReturnSuccess
|
||||||
|
noSleepReturn = nil
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
@ -301,6 +301,7 @@
|
|||||||
37BE0BD726A1D4A90092E2DB /* PlayerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37BE0BD526A1D4A90092E2DB /* PlayerViewController.swift */; };
|
37BE0BD726A1D4A90092E2DB /* PlayerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37BE0BD526A1D4A90092E2DB /* PlayerViewController.swift */; };
|
||||||
37BE0BDA26A214630092E2DB /* PlayerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37BE0BD926A214630092E2DB /* PlayerViewController.swift */; };
|
37BE0BDA26A214630092E2DB /* PlayerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37BE0BD926A214630092E2DB /* PlayerViewController.swift */; };
|
||||||
37BE0BDC26A2367F0092E2DB /* Player.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37BE0BDB26A2367F0092E2DB /* Player.swift */; };
|
37BE0BDC26A2367F0092E2DB /* Player.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37BE0BDB26A2367F0092E2DB /* Player.swift */; };
|
||||||
|
37C069782725962F00F7F6CB /* ScreenSaverManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C069772725962F00F7F6CB /* ScreenSaverManager.swift */; };
|
||||||
37C194C726F6A9C8005D3B96 /* RecentsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C194C626F6A9C8005D3B96 /* RecentsModel.swift */; };
|
37C194C726F6A9C8005D3B96 /* RecentsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C194C626F6A9C8005D3B96 /* RecentsModel.swift */; };
|
||||||
37C194C826F6A9C8005D3B96 /* RecentsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C194C626F6A9C8005D3B96 /* RecentsModel.swift */; };
|
37C194C826F6A9C8005D3B96 /* RecentsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C194C626F6A9C8005D3B96 /* RecentsModel.swift */; };
|
||||||
37C3A241272359900087A57A /* Double+Format.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C3A240272359900087A57A /* Double+Format.swift */; };
|
37C3A241272359900087A57A /* Double+Format.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37C3A240272359900087A57A /* Double+Format.swift */; };
|
||||||
@ -575,6 +576,7 @@
|
|||||||
37BE0BD526A1D4A90092E2DB /* PlayerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerViewController.swift; sourceTree = "<group>"; };
|
37BE0BD526A1D4A90092E2DB /* PlayerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerViewController.swift; sourceTree = "<group>"; };
|
||||||
37BE0BD926A214630092E2DB /* PlayerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerViewController.swift; sourceTree = "<group>"; };
|
37BE0BD926A214630092E2DB /* PlayerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerViewController.swift; sourceTree = "<group>"; };
|
||||||
37BE0BDB26A2367F0092E2DB /* Player.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Player.swift; sourceTree = "<group>"; };
|
37BE0BDB26A2367F0092E2DB /* Player.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Player.swift; sourceTree = "<group>"; };
|
||||||
|
37C069772725962F00F7F6CB /* ScreenSaverManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScreenSaverManager.swift; sourceTree = "<group>"; };
|
||||||
37C194C626F6A9C8005D3B96 /* RecentsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecentsModel.swift; sourceTree = "<group>"; };
|
37C194C626F6A9C8005D3B96 /* RecentsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecentsModel.swift; sourceTree = "<group>"; };
|
||||||
37C3A240272359900087A57A /* Double+Format.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+Format.swift"; sourceTree = "<group>"; };
|
37C3A240272359900087A57A /* Double+Format.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+Format.swift"; sourceTree = "<group>"; };
|
||||||
37C3A24427235DA70087A57A /* ChannelPlaylist.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelPlaylist.swift; sourceTree = "<group>"; };
|
37C3A24427235DA70087A57A /* ChannelPlaylist.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelPlaylist.swift; sourceTree = "<group>"; };
|
||||||
@ -815,6 +817,7 @@
|
|||||||
374C053E272472C0009BDDBE /* PlayerSegments.swift */,
|
374C053E272472C0009BDDBE /* PlayerSegments.swift */,
|
||||||
37DD87C6271C9CFE0027CBF9 /* PlayerStreams.swift */,
|
37DD87C6271C9CFE0027CBF9 /* PlayerStreams.swift */,
|
||||||
374C053A2724614F009BDDBE /* PlayerTVMenu.swift */,
|
374C053A2724614F009BDDBE /* PlayerTVMenu.swift */,
|
||||||
|
37C069772725962F00F7F6CB /* ScreenSaverManager.swift */,
|
||||||
);
|
);
|
||||||
path = Player;
|
path = Player;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@ -1777,6 +1780,7 @@
|
|||||||
3797758C2689345500DD52A8 /* Store.swift in Sources */,
|
3797758C2689345500DD52A8 /* Store.swift in Sources */,
|
||||||
37141674267A8E10006CA35D /* Country.swift in Sources */,
|
37141674267A8E10006CA35D /* Country.swift in Sources */,
|
||||||
37FD43E42704847C0073EE42 /* View+Fixtures.swift in Sources */,
|
37FD43E42704847C0073EE42 /* View+Fixtures.swift in Sources */,
|
||||||
|
37C069782725962F00F7F6CB /* ScreenSaverManager.swift in Sources */,
|
||||||
37AAF2A126741C97007FC770 /* SubscriptionsView.swift in Sources */,
|
37AAF2A126741C97007FC770 /* SubscriptionsView.swift in Sources */,
|
||||||
37732FF12703A26300F04329 /* AccountValidationStatus.swift in Sources */,
|
37732FF12703A26300F04329 /* AccountValidationStatus.swift in Sources */,
|
||||||
37BA794C26DC30EC002A0235 /* AppSidebarPlaylists.swift in Sources */,
|
37BA794C26DC30EC002A0235 /* AppSidebarPlaylists.swift in Sources */,
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import AVFAudio
|
||||||
import Defaults
|
import Defaults
|
||||||
import SDWebImage
|
import SDWebImage
|
||||||
import SDWebImagePINPlugin
|
import SDWebImagePINPlugin
|
||||||
@ -91,6 +92,9 @@ struct ContentView: View {
|
|||||||
SiestaLog.Category.enabled = .common
|
SiestaLog.Category.enabled = .common
|
||||||
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
|
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
|
||||||
SDWebImageManager.defaultImageCache = PINCache(name: "net.yattee.app")
|
SDWebImageManager.defaultImageCache = PINCache(name: "net.yattee.app")
|
||||||
|
#if !os(macOS)
|
||||||
|
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback)
|
||||||
|
#endif
|
||||||
|
|
||||||
if let account = accounts.lastUsed ??
|
if let account = accounts.lastUsed ??
|
||||||
instances.lastUsed?.anonymousAccount ??
|
instances.lastUsed?.anonymousAccount ??
|
||||||
|
@ -11,9 +11,6 @@ final class PlayerViewController: UIViewController {
|
|||||||
super.viewWillAppear(animated)
|
super.viewWillAppear(animated)
|
||||||
|
|
||||||
loadPlayer()
|
loadPlayer()
|
||||||
|
|
||||||
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback)
|
|
||||||
try? AVAudioSession.sharedInstance().setActive(true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadPlayer() {
|
func loadPlayer() {
|
||||||
|
@ -5,4 +5,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
|
|||||||
func applicationShouldTerminateAfterLastWindowClosed(_: NSApplication) -> Bool {
|
func applicationShouldTerminateAfterLastWindowClosed(_: NSApplication) -> Bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func applicationWillTerminate(_: Notification) {
|
||||||
|
ScreenSaverManager.shared.enable()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user