Small improvements

This commit is contained in:
Rasmus Krämer
2022-05-03 12:55:13 +02:00
parent 394363c8cb
commit 9701c767b2
8 changed files with 86 additions and 66 deletions
+7 -8
View File
@@ -70,6 +70,7 @@ class AudioPlayer: NSObject {
public func destroy() {
// Pause is not synchronous causing this error on below lines:
// AVAudioSession_iOS.mm:1206 Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session
// It is related to L79 `AVAudioSession.sharedInstance().setActive(false)`
pause()
audioPlayer.replaceCurrentItem(with: nil)
@@ -80,11 +81,9 @@ class AudioPlayer: NSObject {
print(error)
}
// Throws error Possibly related to the error above
// DispatchQueue.main.sync {
// UIApplication.shared.endReceivingRemoteControlEvents()
// }
DispatchQueue.runOnMainQueue {
UIApplication.shared.endReceivingRemoteControlEvents()
}
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.closed.rawValue), object: nil)
}
@@ -186,9 +185,9 @@ class AudioPlayer: NSObject {
// MARK: - Now playing
private func setupRemoteTransportControls() {
// DispatchQueue.main.sync {
DispatchQueue.runOnMainQueue {
UIApplication.shared.beginReceivingRemoteControlEvents()
// }
}
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
@@ -246,7 +245,7 @@ class AudioPlayer: NSObject {
}
private func updateNowPlaying() {
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.update.rawValue), object: nil)
NowPlayingInfo.update(duration: getDuration(), currentTime: getCurrentTime(), rate: rate)
NowPlayingInfo.shared.update(duration: getDuration(), currentTime: getCurrentTime(), rate: rate)
}
// MARK: - Observer
+18 -15
View File
@@ -20,16 +20,16 @@ class PlayerHandler {
player = nil
}
NowPlayingInfo.setSessionMetadata(metadata: NowPlayingMetadata(id: session.id, itemId: session.libraryItemId!, artworkUrl: session.coverPath, title: session.displayTitle ?? "Unknown title", author: session.displayAuthor, series: nil))
NowPlayingInfo.shared.setSessionMetadata(metadata: NowPlayingMetadata(id: session.id, itemId: session.libraryItemId!, artworkUrl: session.coverPath, title: session.displayTitle ?? "Unknown title", author: session.displayAuthor, series: nil))
self.session = session
player = AudioPlayer(playbackSession: session, playWhenReady: playWhenReady, playbackRate: playbackRate)
// DispatchQueue.main.sync {
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
self.tick()
DispatchQueue.runOnMainQueue {
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
self.tick()
}
}
// }
}
public static func stopPlayback() {
player?.destroy()
@@ -38,7 +38,7 @@ class PlayerHandler {
timer?.invalidate()
timer = nil
NowPlayingInfo.reset()
NowPlayingInfo.shared.reset()
}
public static func getCurrentTime() -> Double? {
@@ -63,20 +63,20 @@ class PlayerHandler {
}
public static func seekForward(amount: Double) {
if player == nil {
guard let player = player else {
return
}
let destinationTime = player!.getCurrentTime() + amount
player!.seek(destinationTime)
let destinationTime = player.getCurrentTime() + amount
player.seek(destinationTime)
}
public static func seekBackward(amount: Double) {
if player == nil {
guard let player = player else {
return
}
let destinationTime = player!.getCurrentTime() - amount
player!.seek(destinationTime)
let destinationTime = player.getCurrentTime() - amount
player.seek(destinationTime)
}
public static func seek(amount: Double) {
player?.seek(amount)
@@ -109,13 +109,16 @@ class PlayerHandler {
}
}
public static func syncProgress() {
if player == nil || session == nil {
if session == nil {
return
}
guard let player = player else {
return
}
let report = PlaybackReport(currentTime: player!.getCurrentTime(), duration: player!.getDuration(), timeListened: listeningTimePassedSinceLastSync)
let report = PlaybackReport(currentTime: player.getCurrentTime(), duration: player.getDuration(), timeListened: listeningTimePassedSinceLastSync)
session!.currentTime = player!.getCurrentTime()
session!.currentTime = player.getCurrentTime()
listeningTimePassedSinceLastSync = 0
// TODO: check if online