mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-19 09:08:26 +02:00
Added sleep timer
This commit is contained in:
parent
926c17543f
commit
61433ef678
3 changed files with 123 additions and 22 deletions
|
@ -75,8 +75,6 @@ class AudioPlayer: NSObject {
|
||||||
self.rate = self.tmpRate
|
self.rate = self.tmpRate
|
||||||
self.audioPlayer.rate = self.tmpRate
|
self.audioPlayer.rate = self.tmpRate
|
||||||
|
|
||||||
print("oof", self.rate, self.tmpRate)
|
|
||||||
|
|
||||||
updateNowPlaying()
|
updateNowPlaying()
|
||||||
}
|
}
|
||||||
public func pause() {
|
public func pause() {
|
||||||
|
@ -107,8 +105,6 @@ class AudioPlayer: NSObject {
|
||||||
self.audioPlayer.rate = rate
|
self.audioPlayer.rate = rate
|
||||||
}
|
}
|
||||||
if rate > 0.0 && !(observed && rate == 1) {
|
if rate > 0.0 && !(observed && rate == 1) {
|
||||||
|
|
||||||
print("yikes", self.rate, self.tmpRate)
|
|
||||||
self.tmpRate = rate
|
self.tmpRate = rate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,20 @@
|
||||||
|
|
||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
#import <Capacitor/Capacitor.h>
|
#import <Capacitor/Capacitor.h>
|
||||||
|
|
||||||
CAP_PLUGIN(MyNativeAudio, "MyNativeAudio",
|
CAP_PLUGIN(MyNativeAudio, "MyNativeAudio",
|
||||||
CAP_PLUGIN_METHOD(initPlayer, CAPPluginReturnPromise);
|
CAP_PLUGIN_METHOD(initPlayer, CAPPluginReturnPromise);
|
||||||
CAP_PLUGIN_METHOD(playPlayer, CAPPluginReturnPromise);
|
CAP_PLUGIN_METHOD(playPlayer, CAPPluginReturnPromise);
|
||||||
CAP_PLUGIN_METHOD(pausePlayer, CAPPluginReturnPromise);
|
CAP_PLUGIN_METHOD(pausePlayer, CAPPluginReturnPromise);
|
||||||
CAP_PLUGIN_METHOD(seekForward, CAPPluginReturnPromise);
|
CAP_PLUGIN_METHOD(seekForward, CAPPluginReturnPromise);
|
||||||
CAP_PLUGIN_METHOD(seekBackward, CAPPluginReturnPromise);
|
CAP_PLUGIN_METHOD(seekBackward, CAPPluginReturnPromise);
|
||||||
CAP_PLUGIN_METHOD(seekPlayer, CAPPluginReturnPromise);
|
CAP_PLUGIN_METHOD(seekPlayer, CAPPluginReturnPromise);
|
||||||
CAP_PLUGIN_METHOD(terminateStream, CAPPluginReturnPromise);
|
CAP_PLUGIN_METHOD(terminateStream, CAPPluginReturnPromise);
|
||||||
CAP_PLUGIN_METHOD(getStreamSyncData, CAPPluginReturnPromise);
|
CAP_PLUGIN_METHOD(getStreamSyncData, CAPPluginReturnPromise);
|
||||||
CAP_PLUGIN_METHOD(getCurrentTime, CAPPluginReturnPromise);
|
CAP_PLUGIN_METHOD(getCurrentTime, CAPPluginReturnPromise);
|
||||||
CAP_PLUGIN_METHOD(setPlaybackSpeed, CAPPluginReturnPromise);
|
CAP_PLUGIN_METHOD(setPlaybackSpeed, CAPPluginReturnPromise);
|
||||||
)
|
|
||||||
|
CAP_PLUGIN_METHOD(setSleepTimer, CAPPluginReturnPromise);
|
||||||
|
CAP_PLUGIN_METHOD(increaseSleepTime, CAPPluginReturnPromise);
|
||||||
|
CAP_PLUGIN_METHOD(decreaseSleepTime, CAPPluginReturnPromise);
|
||||||
|
CAP_PLUGIN_METHOD(cancelSleepTimer, CAPPluginReturnPromise);
|
||||||
|
)
|
||||||
|
|
|
@ -3,6 +3,10 @@ import Capacitor
|
||||||
import MediaPlayer
|
import MediaPlayer
|
||||||
import AVKit
|
import AVKit
|
||||||
|
|
||||||
|
func parseSleepTime(millis: String?) -> Double {
|
||||||
|
(Double(millis ?? "0") ?? 0) / 1000
|
||||||
|
}
|
||||||
|
|
||||||
@objc(MyNativeAudio)
|
@objc(MyNativeAudio)
|
||||||
public class MyNativeAudio: CAPPlugin {
|
public class MyNativeAudio: CAPPlugin {
|
||||||
var currentCall: CAPPluginCall?
|
var currentCall: CAPPluginCall?
|
||||||
|
@ -10,10 +14,8 @@ public class MyNativeAudio: CAPPlugin {
|
||||||
|
|
||||||
var playerContext = 0
|
var playerContext = 0
|
||||||
|
|
||||||
override public func load() {
|
var currentSleepTimer: Timer? = nil
|
||||||
NSLog("Load MyNativeAudio")
|
var remainingSleepDuration: Double = 0
|
||||||
// NotificationCenter.default.addObserver(self, selector: #selector(stop), name: Notification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func initPlayer(_ call: CAPPluginCall) {
|
@objc func initPlayer(_ call: CAPPluginCall) {
|
||||||
NSLog("Init Player")
|
NSLog("Init Player")
|
||||||
|
@ -32,13 +34,18 @@ public class MyNativeAudio: CAPPlugin {
|
||||||
|
|
||||||
token: call.getString("token") ?? ""
|
token: call.getString("token") ?? ""
|
||||||
)
|
)
|
||||||
|
let playWhenReady = call.getBool("playWhenReady", false)
|
||||||
|
|
||||||
if self.currentPlayer != nil && self.currentPlayer?.audiobook.streamId == audiobook.streamId {
|
if self.currentPlayer != nil && self.currentPlayer?.audiobook.streamId == audiobook.streamId {
|
||||||
|
if playWhenReady {
|
||||||
|
self.currentPlayer?.play()
|
||||||
|
}
|
||||||
|
|
||||||
call.resolve(["success": true])
|
call.resolve(["success": true])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
self.currentPlayer = AudioPlayer(audiobook: audiobook, playWhenReady: call.getBool("playWhenReady", false))
|
self.currentPlayer = AudioPlayer(audiobook: audiobook, playWhenReady: playWhenReady)
|
||||||
self.currentPlayer!.addObserver(self, forKeyPath: #keyPath(AudioPlayer.status), options: .new, context: &playerContext)
|
self.currentPlayer!.addObserver(self, forKeyPath: #keyPath(AudioPlayer.status), options: .new, context: &playerContext)
|
||||||
|
|
||||||
call.resolve(["success": true])
|
call.resolve(["success": true])
|
||||||
|
@ -148,6 +155,7 @@ public class MyNativeAudio: CAPPlugin {
|
||||||
|
|
||||||
call.resolve([ "isPlaying": self.currentPlayer!.rate > 0.0, "lastPauseTime": 0, "id": self.currentPlayer?.audiobook.streamId as Any ])
|
call.resolve([ "isPlaying": self.currentPlayer!.rate > 0.0, "lastPauseTime": 0, "id": self.currentPlayer?.audiobook.streamId as Any ])
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func setPlaybackSpeed(_ call: CAPPluginCall) {
|
@objc func setPlaybackSpeed(_ call: CAPPluginCall) {
|
||||||
if self.currentPlayer == nil {
|
if self.currentPlayer == nil {
|
||||||
call.resolve()
|
call.resolve()
|
||||||
|
@ -160,11 +168,104 @@ public class MyNativeAudio: CAPPlugin {
|
||||||
call.resolve()
|
call.resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc func setSleepTimer(_ call: CAPPluginCall) {
|
||||||
|
if self.currentPlayer == nil {
|
||||||
|
call.resolve()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let time = parseSleepTime(millis: call.getString("time"))
|
||||||
|
setSleepTimer(seconds: time)
|
||||||
|
|
||||||
|
call.resolve([ "success": true ])
|
||||||
|
}
|
||||||
|
@objc func increaseSleepTime(_ call: CAPPluginCall) {
|
||||||
|
if self.currentPlayer == nil {
|
||||||
|
call.resolve()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var time = self.remainingSleepDuration + parseSleepTime(millis: call.getString("time"))
|
||||||
|
if time > self.currentPlayer!.getDuration() {
|
||||||
|
time = self.currentPlayer!.getDuration()
|
||||||
|
}
|
||||||
|
|
||||||
|
setSleepTimer(seconds: time)
|
||||||
|
call.resolve([ "success": true ])
|
||||||
|
}
|
||||||
|
@objc func decreaseSleepTime(_ call: CAPPluginCall) {
|
||||||
|
if self.currentSleepTimer == nil {
|
||||||
|
call.resolve()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var time = parseSleepTime(millis: call.getString("time"))
|
||||||
|
if time < 0 {
|
||||||
|
time = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
setSleepTimer(seconds: time)
|
||||||
|
call.resolve([
|
||||||
|
"success": true,
|
||||||
|
])
|
||||||
|
}
|
||||||
|
@objc func cancelSleepTimer(_ call: CAPPluginCall) {
|
||||||
|
setSleepTimer(seconds: 0)
|
||||||
|
call.resolve([
|
||||||
|
"success": true,
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
func setSleepTimer(seconds: Double) {
|
||||||
|
if currentPlayer == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
remainingSleepDuration = seconds
|
||||||
|
currentSleepTimer?.invalidate()
|
||||||
|
|
||||||
|
self.notifyListeners("onSleepTimerSet", data: [
|
||||||
|
"value": self.remainingSleepDuration,
|
||||||
|
])
|
||||||
|
|
||||||
|
if seconds == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.currentSleepTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in
|
||||||
|
self.updateSleepTime()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func updateSleepTime() {
|
||||||
|
if currentPlayer == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.remainingSleepDuration <= 0 {
|
||||||
|
if currentSleepTimer != nil {
|
||||||
|
currentSleepTimer!.invalidate()
|
||||||
|
}
|
||||||
|
self.notifyListeners("onSleepTimerEnded", data: [
|
||||||
|
"value": currentPlayer!.getCurrentTime(),
|
||||||
|
])
|
||||||
|
|
||||||
|
currentPlayer!.pause()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
remainingSleepDuration -= 1
|
||||||
|
self.notifyListeners("onSleepTimerSet", data: [
|
||||||
|
"value": self.remainingSleepDuration,
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
func sendMetadata() {
|
func sendMetadata() {
|
||||||
if self.currentPlayer == nil {
|
if self.currentPlayer == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
self.notifyListeners("onMetadata", data: [
|
self.notifyListeners("onMetadata", data: [
|
||||||
"duration": self.currentPlayer!.getDuration() * 1000,
|
"duration": self.currentPlayer!.getDuration() * 1000,
|
||||||
"currentTime": self.currentPlayer!.getCurrentTime() * 1000,
|
"currentTime": self.currentPlayer!.getCurrentTime() * 1000,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue