Go pack a few seconds when resuming after a long time

This commit is contained in:
Rasmus Krämer
2022-03-22 15:38:31 +01:00
parent 37b5554b39
commit d8d02984fd
3 changed files with 57 additions and 19 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.beginReceivingRemoteControlEvents() // UIApplication.shared.beginReceivingRemoteControlEvents()
// Override point for customization after application launch. // Override point for customization after application launch.
return true return true
} }
+42 -4
View File
@@ -22,7 +22,9 @@ class AudioPlayer: NSObject {
// enums and @objc are not compatible // enums and @objc are not compatible
@objc dynamic var status: Int @objc dynamic var status: Int
@objc dynamic var rate: Float @objc dynamic var rate: Float
private var tmpRate: Float = 1.0 private var tmpRate: Float = 1.0
private var lastPlayTime: Double = 0.0
private var playerContext = 0 private var playerContext = 0
private var playerItemContext = 0 private var playerItemContext = 0
@@ -63,13 +65,34 @@ class AudioPlayer: NSObject {
} }
func destroy() { func destroy() {
pause() pause()
audioPlayer.replaceCurrentItem(with: nil)
nowPlayingInfo = [:] DispatchQueue.main.sync {
updateNowPlaying() UIApplication.shared.endReceivingRemoteControlEvents()
}
do {
try AVAudioSession.sharedInstance().setActive(false)
} catch {
NSLog("Failed to set AVAudioSession inactive")
print(error)
}
nowPlayingInfo.removeAll()
nowPlayingInfo[MPMediaItemPropertyTitle] = ""
nowPlayingInfo[MPMediaItemPropertyArtist] = ""
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
} }
// MARK: - Methods // MARK: - Methods
public func play() { public func play(allowSeekBack: Bool = false) {
if allowSeekBack {
if lastPlayTime + 5 < Date.timeIntervalSinceReferenceDate {
seek(getCurrentTime() - 1.5)
}
}
lastPlayTime = Date.timeIntervalSinceReferenceDate
self.audioPlayer.play() self.audioPlayer.play()
self.status = 1 self.status = 1
self.rate = self.tmpRate self.rate = self.tmpRate
@@ -83,6 +106,7 @@ class AudioPlayer: NSObject {
self.rate = 0.0 self.rate = 0.0
updateNowPlaying() updateNowPlaying()
lastPlayTime = Date.timeIntervalSinceReferenceDate
} }
public func seek(_ to: Double) { public func seek(_ to: Double) {
let continuePlaing = rate > 0.0 let continuePlaing = rate > 0.0
@@ -144,11 +168,14 @@ class AudioPlayer: NSObject {
// MARK: - Now playing // MARK: - Now playing
func setupRemoteTransportControls() { func setupRemoteTransportControls() {
DispatchQueue.main.sync {
UIApplication.shared.beginReceivingRemoteControlEvents()
}
let commandCenter = MPRemoteCommandCenter.shared() let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true commandCenter.playCommand.isEnabled = true
commandCenter.playCommand.addTarget { [unowned self] event in commandCenter.playCommand.addTarget { [unowned self] event in
play() play(allowSeekBack: true)
return .success return .success
} }
commandCenter.pauseCommand.isEnabled = true commandCenter.pauseCommand.isEnabled = true
@@ -187,6 +214,17 @@ class AudioPlayer: NSObject {
self.seek(event.positionTime) self.seek(event.positionTime)
return .success return .success
} }
commandCenter.changePlaybackRateCommand.isEnabled = true
commandCenter.changePlaybackRateCommand.supportedPlaybackRates = [0.5, 0.75, 0.9, 1, 1.2, 1.5, 2]
commandCenter.changePlaybackRateCommand.addTarget { event in
guard let event = event as? MPChangePlaybackRateCommandEvent else {
return .noSuchContent
}
self.setPlaybackRate(event.playbackRate)
return .success
}
} }
func invokeMetadataUpdate() { func invokeMetadataUpdate() {
+13 -13
View File
@@ -9,7 +9,6 @@ func parseSleepTime(millis: String?) -> Double {
@objc(MyNativeAudio) @objc(MyNativeAudio)
public class MyNativeAudio: CAPPlugin { public class MyNativeAudio: CAPPlugin {
var currentCall: CAPPluginCall?
var currentPlayer: AudioPlayer? var currentPlayer: AudioPlayer?
var playerContext = 0 var playerContext = 0
@@ -36,17 +35,19 @@ public class MyNativeAudio: CAPPlugin {
) )
let playWhenReady = call.getBool("playWhenReady", false) let playWhenReady = call.getBool("playWhenReady", false)
if self.currentPlayer != nil && self.currentPlayer?.audiobook.streamId == audiobook.streamId { if currentPlayer != nil && currentPlayer?.audiobook.streamId == audiobook.streamId {
if playWhenReady { if playWhenReady {
self.currentPlayer?.play() self.currentPlayer?.play()
} }
call.resolve(["success": true]) call.resolve(["success": true])
return return
} else if currentPlayer != nil && currentPlayer?.audiobook.streamId != audiobook.streamId {
stop()
} }
self.currentPlayer = AudioPlayer(audiobook: audiobook, playWhenReady: playWhenReady) currentPlayer = AudioPlayer(audiobook: audiobook, playWhenReady: playWhenReady)
self.currentPlayer!.addObserver(self, forKeyPath: #keyPath(AudioPlayer.status), options: .new, context: &playerContext) currentPlayer!.addObserver(self, forKeyPath: #keyPath(AudioPlayer.status), options: .new, context: &playerContext)
call.resolve(["success": true]) call.resolve(["success": true])
} }
@@ -116,7 +117,7 @@ public class MyNativeAudio: CAPPlugin {
return return
} }
self.currentPlayer!.play() self.currentPlayer!.play(allowSeekBack: true)
sendPlaybackStatusUpdate(true) sendPlaybackStatusUpdate(true)
call.resolve() call.resolve()
@@ -126,15 +127,14 @@ public class MyNativeAudio: CAPPlugin {
stop() stop()
call.resolve() call.resolve()
} }
@objc func stop() { @objc func stop(_ call: CAPPluginCall? = nil) {
if let call = currentCall { if self.currentPlayer != nil {
if self.currentPlayer != nil { self.currentPlayer!.destroy()
self.currentPlayer!.destroy() }
} self.currentPlayer = nil
self.currentPlayer = nil if call != nil {
currentCall = nil; call!.resolve([ "result": true ])
call.resolve([ "result": true ])
} }
} }