implement automaticallyWaitsToMinimizeStalling on iOS (#49)

* implement automaticallyWaitsToMinimizeStalling on iOS

* Allow automaticallyWaitsToMinimizeStalling to be called before setUrl

Co-authored-by: ryanheise <ryan@ryanheise.com>
This commit is contained in:
Esmond Wong 2020-03-07 08:49:35 +08:00 committed by GitHub
parent f3d9a34b50
commit c79216c818
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -17,6 +17,7 @@
BOOL _buffering;
id _endObserver;
id _timeObserver;
BOOL _automaticallyWaitsToMinimizeStalling;
}
- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar playerId:(NSString*)idParam {
@ -37,6 +38,7 @@
_buffering = NO;
_endObserver = 0;
_timeObserver = 0;
_automaticallyWaitsToMinimizeStalling = YES;
__weak __typeof__(self) weakSelf = self;
[_methodChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
[weakSelf handleMethodCall:call result:result];
@ -66,6 +68,9 @@
} else if ([@"setSpeed" isEqualToString:call.method]) {
[self setSpeed:(float)[args[0] doubleValue]];
result(nil);
} else if ([@"setAutomaticallyWaitsToMinimizeStalling" isEqualToString:call.method]) {
[self setAutomaticallyWaitsToMinimizeStalling:(BOOL)[args[0] boolValue]];
result(nil);
} else if ([@"seek" isEqualToString:call.method]) {
[self seek:[args[0] intValue] result:result];
result(nil);
@ -198,6 +203,9 @@
[_player removeTimeObserver:_timeObserver];
_timeObserver = 0;
}
if (@available(iOS 10.0, *)) {
_player.automaticallyWaitsToMinimizeStalling = _automaticallyWaitsToMinimizeStalling;
}
// TODO: learn about the different ways to define weakSelf.
//__weak __typeof__(self) weakSelf = self;
//typeof(self) __weak weakSelf = self;
@ -301,6 +309,15 @@
}
}
-(void)setAutomaticallyWaitsToMinimizeStalling:(bool)automaticallyWaitsToMinimizeStalling {
_automaticallyWaitsToMinimizeStalling = automaticallyWaitsToMinimizeStalling;
if (@available(iOS 10.0, *)) {
if(_player) {
_player.automaticallyWaitsToMinimizeStalling = automaticallyWaitsToMinimizeStalling;
}
}
}
- (void)seek:(int)position result:(FlutterResult)result {
_seekPos = position;
NSLog(@"seek. enter buffering");

View File

@ -83,6 +83,8 @@ class AudioPlayer {
double _speed = 1.0;
bool _automaticallyWaitsToMinimizeStalling = true;
File _cacheFile;
/// Creates an [AudioPlayer].
@ -158,6 +160,10 @@ class AudioPlayer {
/// The current speed of the player.
double get speed => _speed;
/// Whether the player should automatically delay playback in order to minimize stalling. (iOS 10.0 or later only)
bool get automaticallyWaitsToMinimizeStalling =>
_automaticallyWaitsToMinimizeStalling;
/// Loads audio media from a URL and completes with the duration of that
/// audio, or null if this call was interrupted by another call so [setUrl],
/// [setFilePath] or [setAsset].
@ -261,6 +267,16 @@ class AudioPlayer {
await _invokeMethod('setSpeed', [speed]);
}
/// Sets automaticallyWaitsToMinimizeStalling for AVPlayer in iOS 10.0 or later, defaults to true.
/// Has no effect on Android clients
Future<void> setAutomaticallyWaitsToMinimizeStalling(
final bool automaticallyWaitsToMinimizeStalling) async {
_automaticallyWaitsToMinimizeStalling =
automaticallyWaitsToMinimizeStalling;
await _invokeMethod('setAutomaticallyWaitsToMinimizeStalling',
[automaticallyWaitsToMinimizeStalling]);
}
/// Seeks to a particular position. It is legal to invoke this method from
/// any state except for [AudioPlaybackState.none] and
/// [AudioPlaybackState.connecting].