mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-28 06:14:00 +02:00
Small improvements
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user