mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-07-29 07:58:43 +02:00
Added basic player methods
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
Reference in New Issue
Block a user