setDataSource method
Set data source for this player
path must be a valid uri, otherwise this method return ArgumentError
set assets as data source first add assets in app's pubspec.yml assets: - assets/butterfly.mp4
pass "asset:///assets/butterfly.mp4" to path
scheme is asset, :// is scheme's separator, / is path's separator.
headers http header
If set autoPlay true, player will stat to play.
The behavior of setDataSource(url, autoPlay: true) is like
await setDataSource(url);
await setOption(FOption.playerCategory, "start-on-prepared", 1);
await prepareAsync();
If set showCover true, player will display the first video frame and then enter FState.paused state.
The behavior of setDataSource(url, showCover: true) is like
await setDataSource(url);
await setOption(FOption.playerCategory, "cover-after-prepared", 1);
await prepareAsync();
If both autoPlay and showCover are true, showCover will be ignored.
Implementation
Future<void> setDataSource(
String path, {
Map<String, String?>? headers,
bool autoPlay = false,
bool showCover = false,
}) async {
if (path.isEmpty || Uri.tryParse(path) == null) {
FLog.e("$this setDataSource invalid path:$path");
return Future.error(
ArgumentError.value(path, "path must be a valid url"),
);
}
if (autoPlay == true && showCover == true) {
FLog.w(
"call setDataSource with both autoPlay and showCover true, showCover will be ignored",
);
}
await _nativeSetup.future;
if (state == FState.idle || state == FState.initialized) {
try {
_explicitPausePosition = null;
FLog.i("$this invoke setDataSource $path");
_dataSource = path;
await _channel.invokeMethod("setDataSource", <String, dynamic>{
'url': path,
});
} on PlatformException catch (e) {
return _errorListener(e);
}
if (headers != null) {
final headersStr = headers.entries
.map((entry) => '${entry.key}: ${entry.value ?? ''}')
.join('\r\n');
await setOption(FOption.formatCategory, 'headers', headersStr);
}
if (autoPlay == true) {
await start();
} else if (showCover == true) {
await setOption(FOption.playerCategory, "cover-after-prepared", 1);
await prepareAsync();
}
} else {
FLog.e("$this setDataSource invalid state:$state");
return Future.error(StateError("setDataSource on invalid state $state"));
}
}