Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: How to get asset path (video) for File class

I saved a video in my asset folder and i am trying to get the video using the File class.

For example:

Final File video = File(String path);

So my question is where can i find the String path?

I already added this file to the pubspec.yaml

assets:
    - assets/mbf.mp4

Thank you

Answer

getVideo() async {
var response = await rootBundle.load('assets/mbf.mp4');
final directory = await getApplicationDocumentsDirectory();
var file = File("${directory.path}/mbf.mp4"); 
file.writeAsBytesSync(response.buffer.asUint8List());

//Use it with VideoPlayer

controller = VideoPlayerController.file(file)
  ..addListener(() => setState(() {}))
  ..setLooping(true)
  ..initialize().then((_) => controller.play());
 }
like image 825
JBKN Avatar asked Nov 01 '25 12:11

JBKN


1 Answers

You need copy file to device and then get path of file.

See exampl code:

void _copyAssetToLocal() async {
try {
  var content = await rootBundle.load("assets/intro.mp4");
  final directory = await getApplicationDocumentsDirectory();
  var file = File("${directory.path}/intro.mp4");
  file.writeAsBytesSync(content.buffer.asUint8List());
  loadIntroVideoMethod(file.path);
} catch (e) {
  
}}
like image 127
Esmaeil Ahmadipour Avatar answered Nov 04 '25 07:11

Esmaeil Ahmadipour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!