initialize method
Implementation
Future<void> initialize() async {
if (this.playerConfig.supportBackground == false) {
_lifeCycleObserver = _VideoAppLifeCycleObserver(this);
_lifeCycleObserver!.initialize();
}
_creatingCompleter = Completer<void>();
Map<dynamic, dynamic> dataSourceDescription;
switch (dataSourceType) {
case DataSourceType.asset:
dataSourceDescription = <String, dynamic>{'asset': dataSource};
break;
case DataSourceType.network:
case DataSourceType.file:
dataSourceDescription = <String, dynamic>{'uri': dataSource};
break;
}
value = value.copyWith(isPlaying: playerConfig.autoPlay);
dataSourceDescription.addAll(playerConfig.toJson());
final Map<String, dynamic>? response =
await channel.invokeMapMethod<String, dynamic>(
'create',
dataSourceDescription,
);
_textureId = response!['textureId'];
_creatingCompleter!.complete(null);
final Completer<void> initializingCompleter = Completer<void>();
void eventListener(dynamic event) {
if (_isDisposed) {
return;
}
final Map<dynamic, dynamic> map = event;
int? curCode = map['eventCode'];
switch (map['event']) {
case 'initialized':
value = value.copyWith(
duration: Duration(milliseconds: map['duration']),
size: Size(map['width']?.toDouble() ?? 0.0,
map['height']?.toDouble() ?? 0.0),
degree: map['degree'] ?? 0,
eventCode: curCode,
);
initializingCompleter.complete(null);
break;
case 'progress':
if (!value.isPlaying) return;
Duration newProgress = Duration(milliseconds: map['progress']);
Duration newPlayable = Duration(milliseconds: map['playable']);
if (value.position == newProgress && value.playable == newPlayable)
return;
value = value.copyWith(
position: newProgress,
duration: Duration(milliseconds: map['duration']),
playable: newPlayable,
eventCode: curCode,
);
break;
case 'loading':
value = value.copyWith(
isLoading: true,
eventCode: curCode,
);
break;
case 'loadingend':
value = value.copyWith(
isLoading: false,
eventCode: curCode,
);
break;
case 'playend':
value = value.copyWith(
isPlaying: false,
position: value.duration,
eventCode: curCode,
);
break;
case 'netStatus':
int fps = map['fps'].toInt();
// 忽略小于3的帧率浮动
if (value.netSpeed == map['netSpeed'] && (value.fps! - fps).abs() < 3) return;
value = value.copyWith(
netSpeed: map['netSpeed'],
fps: fps,
eventCode: curCode,
);
break;
case 'error':
value = value.copyWith(
errorDescription: map['errorInfo'],
eventCode: curCode,
);
break;
case 'orientation':
value = value.copyWith(
orientation: map['orientation'],
eventCode: curCode,
);
break;
default:
value = value.copyWith(
eventCode: curCode,
);
break;
}
}
_eventSubscription = _eventChannelFor(_textureId!)
.receiveBroadcastStream()
.listen(eventListener);
return initializingCompleter.future;
}