support seeking to end of live streams (#108)

* support seeking to end of live streams

suggested edit for #96

* fix type casting
This commit is contained in:
LKHO 2020-06-07 14:31:36 +08:00 committed by GitHub
parent ee0c4cd7cd
commit f66413c007
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 11 deletions

View File

@ -268,11 +268,13 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Met
break; break;
case "seek": case "seek":
Object position = args.get(0); Object position = args.get(0);
long position2;
if (position instanceof Integer) { if (position instanceof Integer) {
seek((Integer)position, result); position2 = (Integer)position;
} else { } else {
seek((Long)position, result); position2 = (Long)position;
} }
seek(position2 == -2 ? C.TIME_UNSET : position2, result);
break; break;
case "dispose": case "dispose":
dispose(); dispose();
@ -336,7 +338,7 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener, Met
private long getCurrentPosition() { private long getCurrentPosition() {
if (state == PlaybackState.none || state == PlaybackState.connecting) { if (state == PlaybackState.none || state == PlaybackState.connecting) {
return 0; return 0;
} else if (seekPos != null) { } else if (seekPos != null && seekPos != C.TIME_UNSET) {
return seekPos; return seekPos;
} else { } else {
return player.getCurrentPosition(); return player.getCurrentPosition();

View File

@ -140,7 +140,7 @@
- (int)getCurrentPosition { - (int)getCurrentPosition {
if (_state == none || _state == connecting) { if (_state == none || _state == connecting) {
return 0; return 0;
} else if (_seekPos != -1) { } else if (_seekPos >= 0) {
return _seekPos; return _seekPos;
} else { } else {
return (int)(1000 * CMTimeGetSeconds([_player currentTime])); return (int)(1000 * CMTimeGetSeconds([_player currentTime]));
@ -360,11 +360,19 @@
NSLog(@"seek. enter buffering"); NSLog(@"seek. enter buffering");
_buffering = YES; _buffering = YES;
[self broadcastPlaybackEvent]; [self broadcastPlaybackEvent];
[_player seekToTime:CMTimeMake(position, 1000) if (position == -2) {
completionHandler:^(BOOL finished) { [_player seekToTime:kCMTimePositiveInfinity
NSLog(@"seek completed"); completionHandler:^(BOOL finished) {
[self onSeekCompletion:result]; NSLog(@"seek completed");
}]; [self onSeekCompletion:result];
}];
} else {
[_player seekToTime:CMTimeMake(position, 1000)
completionHandler:^(BOOL finished) {
NSLog(@"seek completed");
[self onSeekCompletion:result];
}];
}
} }
- (void)onSeekCompletion:(FlutterResult)result { - (void)onSeekCompletion:(FlutterResult)result {

View File

@ -421,11 +421,13 @@ class AudioPlayer {
[automaticallyWaitsToMinimizeStalling]); [automaticallyWaitsToMinimizeStalling]);
} }
/// Seeks to a particular position. It is legal to invoke this method from /// Seeks to a particular position. Specify [null] to seek to the end of live streams.
/// It is legal to invoke this method from
/// any state except for [AudioPlaybackState.none] and /// any state except for [AudioPlaybackState.none] and
/// [AudioPlaybackState.connecting]. /// [AudioPlaybackState.connecting].
Future<void> seek(final Duration position) async { Future<void> seek(final Duration position) async {
await _invokeMethod('seek', [position.inMilliseconds]); await _invokeMethod(
'seek', [position != null ? position.inMilliseconds : -2]);
} }
/// Release all resources associated with this player. You must invoke this /// Release all resources associated with this player. You must invoke this