load method
Future<void>
load(
- FlexPlayerSource source, {
- bool autoPlay = false,
- bool loop = false,
- bool mute = false,
- double volume = 1.0,
- double playbackSpeed = 1.0,
- Duration? position,
- VoidCallback? onInitialized,
Load the video player with the given source
.
Implementation
Future<void> load(
FlexPlayerSource source, {
bool autoPlay = false,
bool loop = false,
bool mute = false,
double volume = 1.0,
double playbackSpeed = 1.0,
Duration? position,
VoidCallback? onInitialized,
}) async {
_channel.setupChannels(this);
await _completer.future;
configuration = configuration.copyWith(
autoPlay: autoPlay,
loop: loop,
volume: volume,
playbackSpeed: playbackSpeed,
position: position,
isPlaying: autoPlay,
);
this.playbackSpeed = playbackSpeed;
this.volume = volume;
_initializationstream.add(InitializationEvent.initializing);
playerStateSink.add(PlayerState.ended);
try {
_source = source;
if (source is AssetFlexPlayerSource) {
throw Exception("Asset source is currently not supported");
} else if (source is NetworkFlexPlayerSource) {
type = FileType.url;
if (source.url.endsWith('.m3u8')) {
final response = await get(Uri.parse(source.url));
String m3u8Content = response.body;
// Extract stream qualities
List<Map<String, String>> data = parseM3U8Content(m3u8Content);
for (var element in data) {
videosList.add(
VideoData(
url: (element['url'] ?? "").toFullUrl(source.url),
quality: element['resolution'].toString().split("x").last,
),
);
}
if (data.isEmpty) {
videosList.add(VideoData(url: source.url, quality: 'Auto'));
}
} else {
videosList.add(VideoData(url: source.url, quality: 'Auto'));
}
} else if (source is FileFlexPlayerSource) {
type = FileType.file;
videosList.add(
VideoData(
url: source.file.path,
quality: 'Auto',
),
);
} else if (source is YouTubeFlexPlayerSource) {
final videoId = source.videoId;
final flexYoutubecontroller = FlexYoutubeController.instance;
final isNotLive =
await FlexYoutubeController.instance.isNotLive(source.videoId);
try {
if (isNotLive) {
await flexYoutubecontroller
.getVideoDetails(source.videoId)
.then((value) {
qualities.value = flexYoutubecontroller.videosList
.map((e) => e.quality)
.toSet()
.toList();
videosList.addAll(flexYoutubecontroller.videosList.value);
});
type = FileType.youtube;
} else {
await flexYoutubecontroller
.getVideoDetails(videoId, isLive: true)
.then(
(value) {
qualities.value = flexYoutubecontroller.videosList
.map((e) => e.quality)
.toSet()
.toList();
videosList.addAll(flexYoutubecontroller.videosList.value);
},
);
type = FileType.url;
}
} catch (e) {
_initializationstream.add(InitializationEvent.uninitialized);
rethrow;
}
}
final ids = videosList.map((e) => e.quality).toSet();
videosList.retainWhere((x) => ids.remove(x.quality));
videosList.sort((a, b) {
return int.parse(a.quality.split("p").first) -
int.parse(b.quality.split("p").first);
});
qualities.value = videosList.map((e) => e.quality).toSet().toList();
startListner();
final video =
videosList.firstWhereOrNull((e) => e.quality.contains("360")) ??
videosList.first;
selectedQuality = video.quality;
_currentPlayingSink.add(video);
getCurrentPlaying = video;
await _channel.load(
videoData: videosList,
index: videosList.indexWhere((e) => e.quality == video.quality),
autoPlay: autoPlay,
loop: loop,
mute: mute,
volume: volume,
playbackSpeed: playbackSpeed,
type: type,
);
} on Exception catch (e) {
_initializationstream.add(InitializationEvent.uninitialized);
log("Error in Loading: $e");
throw PlayerError(e.toString());
}
}