Skip to content

Commit

Permalink
Fix negative duration bug on iOS/macOS.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Feb 17, 2021
1 parent a994079 commit e4e4265
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 8 additions & 3 deletions just_audio/darwin/Classes/AudioPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ - (void)broadcastPlaybackEvent {
@"bufferedPosition": @((long long)1000 * [self getBufferedPosition]),
// TODO: Icy Metadata
@"icyMetadata": (id)[NSNull null],
@"duration": @((long long)1000 * [self getDuration]),
@"duration": @([self getDurationMicroseconds]),
@"currentIndex": @(_index),
});
}
Expand Down Expand Up @@ -347,6 +347,11 @@ - (int)getDuration {
}
}

- (long long)getDurationMicroseconds {
int duration = [self getDuration];
return duration < 0 ? -1 : ((long long)1000 * duration);
}

- (void)removeItemObservers:(AVPlayerItem *)playerItem {
[playerItem removeObserver:self forKeyPath:@"status"];
[playerItem removeObserver:self forKeyPath:@"playbackBufferEmpty"];
Expand Down Expand Up @@ -597,7 +602,7 @@ - (void)load:(NSDictionary *)source initialPosition:(CMTime)initialPosition init
}

if (_player.currentItem.status == AVPlayerItemStatusReadyToPlay) {
_loadResult(@{@"duration": @((long long)1000 * [self getDuration])});
_loadResult(@{@"duration": @([self getDurationMicroseconds])});
_loadResult = nil;
} else {
// We send result after the playerItem is ready in observeValueForKeyPath.
Expand Down Expand Up @@ -718,7 +723,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
}
[self broadcastPlaybackEvent];
if (_loadResult) {
_loadResult(@{@"duration": @((long long)1000 * [self getDuration])});
_loadResult(@{@"duration": @([self getDurationMicroseconds])});
_loadResult = nil;
}
if (CMTIME_IS_VALID(_initialPos) && CMTIME_COMPARE_INLINE(_initialPos, >, kCMTimeZero)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ class LoadResponse {
LoadResponse({@required this.duration});

static LoadResponse fromMap(Map<dynamic, dynamic> map) => LoadResponse(
duration: map['duration'] != null
? Duration(microseconds: map['duration'])
: null);
duration: map['duration'] == null || map['duration'] < 0
? null
: Duration(microseconds: map['duration']));
}

/// Information communicated to the platform implementation when playing an
Expand Down

0 comments on commit e4e4265

Please sign in to comment.