initialize method
Attempts to open the given dataSource and load metadata about the video.
Implementation
Future<void> initialize() async {
_lifeCycleObserver = _VideoAppLifeCycleObserver(this);
_lifeCycleObserver.initialize();
_creatingCompleter = Completer<void>();
VideoQuality quality = youtubeVideoQuality ?? VideoQuality.medium360;
String finalYoutubeUrl = dataSource;
if (_getIdFromUrl(dataSource) != null) {
try {
Map<String, String> videoUrls = Map();
String? _videoId = _getIdFromUrl(dataSource);
String _fetchUrl = "";
var yt = YoutubeExplode();
var manifest = await yt.videos.streamsClient.getManifest(_videoId);
Uri? videoUri;
manifest.muxed.forEach((m) {
if (quality == m.videoQuality) {
videoUri = m.url;
}
});
if (videoUri == null) {
finalYoutubeUrl = manifest.muxed.first.url.toString();
} else {
finalYoutubeUrl = videoUri.toString();
}
} catch (err) {}
}
late DataSource dataSourceDescription;
switch (dataSourceType) {
case DataSourceType.asset:
dataSourceDescription = DataSource(
sourceType: DataSourceType.asset,
asset: dataSource,
package: package,
);
break;
case DataSourceType.network:
dataSourceDescription = DataSource(
sourceType: DataSourceType.network,
uri: finalYoutubeUrl,
formatHint: formatHint,
);
break;
case DataSourceType.file:
dataSourceDescription = DataSource(
sourceType: DataSourceType.file,
uri: dataSource,
);
break;
}
if (videoPlayerOptions?.mixWithOthers != null) {
await _videoPlayerPlatform
.setMixWithOthers(videoPlayerOptions!.mixWithOthers);
}
_textureId = await _videoPlayerPlatform.create(dataSourceDescription);
_creatingCompleter!.complete(null);
final Completer<void> initializingCompleter = Completer<void>();
void eventListener(VideoEvent event) {
if (_isDisposed) {
return;
}
switch (event.eventType) {
case VideoEventType.initialized:
value = value.copyWith(
duration: event.duration,
size: event.size,
);
initializingCompleter.complete(null);
_applyLooping();
_applyVolume();
_applyPlayPause();
break;
case VideoEventType.completed:
value = value.copyWith(isPlaying: false, position: value.duration);
_timer?.cancel();
break;
case VideoEventType.bufferingUpdate:
value = value.copyWith(buffered: event.buffered);
break;
case VideoEventType.bufferingStart:
value = value.copyWith(isBuffering: true);
break;
case VideoEventType.bufferingEnd:
value = value.copyWith(isBuffering: false);
break;
case VideoEventType.unknown:
break;
}
}
if (closedCaptionFile != null) {
if (_closedCaptionFile == null) {
_closedCaptionFile = await closedCaptionFile;
}
value = value.copyWith(caption: _getCaptionAt(value.position));
}
void errorListener(Object obj) {
final PlatformException e = obj as PlatformException;
value = VideoPlayerValue.erroneous(e.message);
_timer?.cancel();
if (!initializingCompleter.isCompleted) {
initializingCompleter.completeError(obj);
}
}
_eventSubscription = _videoPlayerPlatform
.videoEventsFor(_textureId!)
.listen(eventListener, onError: errorListener);
return initializingCompleter.future;
}