Added sleep timer

This commit is contained in:
Rasmus Krämer 2022-03-09 22:13:07 +01:00
parent 926c17543f
commit 61433ef678
3 changed files with 123 additions and 22 deletions

View file

@ -75,8 +75,6 @@ class AudioPlayer: NSObject {
self.rate = self.tmpRate
self.audioPlayer.rate = self.tmpRate
print("oof", self.rate, self.tmpRate)
updateNowPlaying()
}
public func pause() {
@ -107,8 +105,6 @@ class AudioPlayer: NSObject {
self.audioPlayer.rate = rate
}
if rate > 0.0 && !(observed && rate == 1) {
print("yikes", self.rate, self.tmpRate)
self.tmpRate = rate
}

View file

@ -1,16 +1,20 @@
#import <Foundation/Foundation.h>
#import <Capacitor/Capacitor.h>
CAP_PLUGIN(MyNativeAudio, "MyNativeAudio",
CAP_PLUGIN_METHOD(initPlayer, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(playPlayer, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(pausePlayer, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(seekForward, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(seekBackward, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(seekPlayer, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(terminateStream, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getStreamSyncData, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getCurrentTime, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setPlaybackSpeed, CAPPluginReturnPromise);
)
CAP_PLUGIN_METHOD(initPlayer, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(playPlayer, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(pausePlayer, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(seekForward, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(seekBackward, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(seekPlayer, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(terminateStream, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getStreamSyncData, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getCurrentTime, 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);
)

View file

@ -3,6 +3,10 @@ import Capacitor
import MediaPlayer
import AVKit
func parseSleepTime(millis: String?) -> Double {
(Double(millis ?? "0") ?? 0) / 1000
}
@objc(MyNativeAudio)
public class MyNativeAudio: CAPPlugin {
var currentCall: CAPPluginCall?
@ -10,10 +14,8 @@ public class MyNativeAudio: CAPPlugin {
var playerContext = 0
override public func load() {
NSLog("Load MyNativeAudio")
// NotificationCenter.default.addObserver(self, selector: #selector(stop), name: Notification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
}
var currentSleepTimer: Timer? = nil
var remainingSleepDuration: Double = 0
@objc func initPlayer(_ call: CAPPluginCall) {
NSLog("Init Player")
@ -32,13 +34,18 @@ public class MyNativeAudio: CAPPlugin {
token: call.getString("token") ?? ""
)
let playWhenReady = call.getBool("playWhenReady", false)
if self.currentPlayer != nil && self.currentPlayer?.audiobook.streamId == audiobook.streamId {
if playWhenReady {
self.currentPlayer?.play()
}
call.resolve(["success": true])
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)
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 ])
}
@objc func setPlaybackSpeed(_ call: CAPPluginCall) {
if self.currentPlayer == nil {
call.resolve()
@ -160,11 +168,104 @@ public class MyNativeAudio: CAPPlugin {
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() {
if self.currentPlayer == nil {
return
}
self.notifyListeners("onMetadata", data: [
"duration": self.currentPlayer!.getDuration() * 1000,
"currentTime": self.currentPlayer!.getCurrentTime() * 1000,