diff --git a/ios/App/App/AudioPlayer.swift b/ios/App/App/AudioPlayer.swift index 4413c147..1a06fe1e 100644 --- a/ios/App/App/AudioPlayer.swift +++ b/ios/App/App/AudioPlayer.swift @@ -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 } diff --git a/ios/App/App/MyNativeAudio.m b/ios/App/App/MyNativeAudio.m index b6dde25e..d0fc3a73 100644 --- a/ios/App/App/MyNativeAudio.m +++ b/ios/App/App/MyNativeAudio.m @@ -1,16 +1,20 @@ - #import #import 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); + ) diff --git a/ios/App/App/MyNativeAudio.swift b/ios/App/App/MyNativeAudio.swift index e4c6232b..5c83bbb0 100644 --- a/ios/App/App/MyNativeAudio.swift +++ b/ios/App/App/MyNativeAudio.swift @@ -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,