Skip to content

Commit 9c2fca9

Browse files
author
Hans Seiffert
committed
Fix current time in now playing info and prevent current time values which are higher than the max time
1 parent 422d88c commit 9c2fca9

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

AudioPlayerManager/Core/Public/Player/AudioPlayerManager.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public class AudioPlayerManager: NSObject {
9191
if let _player = self.player {
9292
_player.play()
9393
if (updateNowPlayingInfo == true || self.didStopPlayback == true) {
94-
self.updateNowPlayingInfo()
94+
self.updateNowPlayingInfoIfNeeded()
9595
}
9696
self.didStopPlayback = false
9797
self.startPlaybackTimeChangeTimer()
@@ -215,7 +215,7 @@ public class AudioPlayerManager: NSObject {
215215
// Move to the beginning of the track if we aren't in the beginning.
216216
self.player?.seekToTime(CMTimeMake(0, 1))
217217
// Update the now playing info to show the new playback time
218-
self.updateNowPlayingInfo()
218+
self.updateNowPlayingInfoIfNeeded()
219219
// Call the callbacks to inform about the new time
220220
self.callPlaybackTimeChangeCallbacks()
221221
}
@@ -393,15 +393,18 @@ public class AudioPlayerManager: NSObject {
393393

394394
// MARK: - Internal helper
395395

396-
private func updateNowPlayingInfo() {
396+
private func updateNowPlayingInfoIfNeeded() {
397397
if (self.useNowPlayingInfoCenter == true) {
398+
// Update the current play time
399+
self.currentTrack?.updateNowPlayingInfoPlaybackTime()
398400
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = self.currentTrack?.nowPlayingInfo
399401
}
400402
}
401403

402404
// MARK: - Plaback time change callback
403405

404406
func callPlaybackTimeChangeCallbacks() {
407+
self.updateNowPlayingInfoIfNeeded()
405408
// Increase the current tracks playing time if the player is playing
406409
if let _currentTrack = self.currentTrack {
407410
for sender in self.playbackPositionChangeCallbacks.keys {

AudioPlayerManager/Core/Public/Track/AudioTrack.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ public class AudioTrack : NSObject {
5757

5858
public func currentTimeInSeconds() -> Float {
5959
if let _playerItem = self.playerItem {
60-
return Float(CMTimeGetSeconds(_playerItem.currentTime()))
60+
let currentTime = Float(CMTimeGetSeconds(_playerItem.currentTime()))
61+
let duration = self.durationInSeconds()
62+
guard (duration <= 0.0 || currentTime <= duration) else {
63+
return duration
64+
}
65+
66+
return currentTime
6167
}
6268
return Float(0)
6369
}
@@ -101,11 +107,18 @@ public class AudioTrack : NSObject {
101107
if let _playerItem = self.playerItem {
102108
let timeInSeconds = CMTimeGetSeconds(_playerItem.asset.duration)
103109
// Check ig the time isn't NaN. This can happen eg. for podcasts
104-
let duration : NSNumber? = ((timeInSeconds.isNaN == false) ? NSNumber(integer: Int(timeInSeconds)) : nil)
110+
let duration : NSNumber? = ((timeInSeconds.isNaN == false) ? NSNumber(float: Float(timeInSeconds)) : nil)
105111
self.nowPlayingInfo?[MPMediaItemPropertyPlaybackDuration] = duration
106112
}
107113
}
108114

115+
public func updateNowPlayingInfoPlaybackTime() {
116+
let currentTime = self.currentTimeInSeconds()
117+
// Check ig the time isn't NaN.
118+
let currentTimeAsNumber : NSNumber? = ((currentTime.isNaN == false) ? NSNumber(float: Float(currentTime)) : nil)
119+
self.nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentTimeAsNumber
120+
}
121+
109122
// MARK: - Helper
110123

111124
public func identifier() -> String? {

0 commit comments

Comments
 (0)