Added basic player methods

This commit is contained in:
Rasmus Krämer
2022-04-14 14:39:09 +02:00
parent 6c65c8a33e
commit 7a9f6ff2ca
7 changed files with 198 additions and 2 deletions
+3
View File
@@ -78,6 +78,8 @@ class AudioPlayer: NSObject {
// DispatchQueue.main.sync {
UIApplication.shared.endReceivingRemoteControlEvents()
// }
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.closed.rawValue), object: nil)
}
// MARK: - Methods
@@ -239,6 +241,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)
}
+60
View File
@@ -21,4 +21,64 @@ class PlayerHandler {
self.session = session
player = AudioPlayer(playbackSession: session, playWhenReady: playWhenReady)
}
public static func stopPlayback() {
player?.destroy()
player = nil
NowPlayingInfo.reset()
}
public static func getCurrentTime() -> Double? {
self.player?.getCurrentTime()
}
public static func setPlaybackSpeed(speed: Float) {
self.player?.setPlaybackRate(speed)
}
public static func play() {
self.player?.play()
}
public static func pause() {
self.player?.play()
}
public static func playPause() {
if paused() {
self.player?.play()
} else {
self.player?.pause()
}
}
public static func seekForward(amount: Double) {
if player == nil {
return
}
let destinationTime = player!.getCurrentTime() + amount
player!.seek(destinationTime)
}
public static func seekBackward(amount: Double) {
if player == nil {
return
}
let destinationTime = player!.getCurrentTime() - amount
player!.seek(destinationTime)
}
public static func seek(amount: Double) {
player?.seek(amount)
}
public static func paused() -> Bool {
player?.rate == 0.0
}
public static func getMetdata() -> [String: Any] {
return [
"duration": player?.getDuration() ?? 0,
"currentTime": player?.getCurrentTime() ?? 0,
"playerState": !paused(),
"currentRate": player?.rate ?? 0,
]
}
}
+8 -1
View File
@@ -47,6 +47,13 @@ class ApiClient {
ApiClient.postResource(endpoint: endpoint, parameters: [
"forceTranscode": "true", // TODO: direct play
"mediaPlayer": "AVPlayer",
], decodable: PlaybackSession.self, callback: callback)
], decodable: PlaybackSession.self) { obj in
var session = obj
session.serverConnectionConfigId = Store.serverConfig.id
session.serverAddress = Store.serverConfig.address
callback(session)
}
}
}
+13
View File
@@ -0,0 +1,13 @@
//
// PlayerEvents.swift
// App
//
// Created by Rasmus Krämer on 14.04.22.
//
import Foundation
enum PlayerEvents: String {
case update = "com.audiobookshelf.app.player.update"
case closed = "com.audiobookshelf.app.player.closed"
}