startPlayer method
Starts the player with a given url
and optional autoPlay
flag.
This method initializes the player and begins streaming the content from the specified URL. It listens for various events such as player state changes, quality updates, and errors, and broadcasts them via the respective streams.
Implementation
void startPlayer(String url, {bool autoPlay = true}) {
_controller.startPlayer(
url,
autoPlay: autoPlay,
onData: (data) async {
// Parse incoming data and add relevant information to the appropriate streams.
final Map<String, dynamic> parsedData = Map<String, dynamic>.from(data);
if (parsedData.containsKey(AppStrings.state)) {
final value = parsedData[AppStrings.state];
playeStateStream.add(PlayerState.values[value]);
} else if (parsedData.containsKey(AppStrings.quality)) {
final value = parsedData[AppStrings.quality];
qualityStream.add(value);
isAutoQualityStream.add(await isAutoQuality());
} else if (parsedData.containsKey(AppStrings.duration)) {
final value = parsedData[AppStrings.duration];
final duration = double.tryParse(value.toString());
durationStream.add(
Duration(
seconds: (duration?.isFinite ?? false) ? duration!.toInt() : 0,
),
);
getQualities();
} else if (parsedData.containsKey(AppStrings.syncTime)) {
final value = parsedData[AppStrings.syncTime];
syncTimeStream
.add(Duration(seconds: double.parse(value.toString()).toInt()));
} else if (parsedData.containsKey(AppStrings.error)) {
final value = parsedData[AppStrings.error];
errorStream.add(value);
}
},
onError: (error) {},
);
// Periodically update the player's position.
_positionStreamSubs?.cancel();
_positionStreamSubs = Stream.periodic(
const Duration(milliseconds: 100),
).listen(
(event) async {
positionStream.add(await _controller.getPosition());
},
);
}