initialize method
Future<int>
initialize({
- required HaloVideoPlayerDataSource dataSource,
- bool autoPlay = false,
- bool looping = false,
- double volume = 1.0,
override
Initialize a video player with the given data source
Implementation
@override
Future<int> initialize({
required HaloVideoPlayerDataSource dataSource,
bool autoPlay = false,
bool looping = false,
double volume = 1.0,
}) async {
final playerId = _nextPlayerId++;
VideoPlayerController controller;
try {
if (dataSource.isNetwork) {
controller = VideoPlayerController.networkUrl(
Uri.parse(dataSource.source),
httpHeaders: dataSource.httpHeaders ?? {},
);
} else if (dataSource.isAsset) {
controller = VideoPlayerController.asset(dataSource.source);
} else {
// File path
if (kIsWeb) {
throw UnsupportedError(
'File-based video playback is not supported on web',
);
}
controller = VideoPlayerController.file(File(dataSource.source));
}
await controller.initialize();
controller.setLooping(looping);
controller.setVolume(volume);
if (autoPlay) {
await controller.play();
}
_controllers[playerId] = controller;
_playerIdToController[playerId] = controller;
_valueControllers[playerId] =
StreamController<HaloVideoPlayerValue>.broadcast();
// Listen to controller updates
controller.addListener(() {
final valueController = _valueControllers[playerId];
if (valueController != null && !valueController.isClosed) {
valueController.add(_convertVideoPlayerValue(controller.value));
}
});
return playerId;
} catch (e) {
// Clean up on error
_controllers.remove(playerId);
_playerIdToController.remove(playerId);
_valueControllers.remove(playerId)?.close();
rethrow;
}
}