user479947
user479947

Reputation:

Loading a file from the assets folder in Flutter / Dart

I'm using the video_player plugin for Flutter to play a .mp4 file. In the example code, there's a demo for using the .network() method to play a file, but in the source code, I'm seeing a .file() method for running local content.

I've added my assets directory to the pubspec.yaml file, but on iOS, I'm not seeing an easy way to get the file:// path to my video and pass it into the video player.

It looks like this is accomplished with an assetBundle constructor, but I'm not quite sure how to phrase the code. Intuitively, it feels like it should look something like this:

  void initState() async {
    super.initState();
    final video = File('assets/video.mp4'); 

    _controller = VideoPlayerController.file(video)
      ..initialize().then((_) {
        setState(() {});
      });
  }

Any ideas?

Upvotes: 5

Views: 6817

Answers (1)

Hosar
Hosar

Reputation: 5292

Guessing you have added something like the following in your pubspec.yaml

assets:
    - assets/

You just need to set the file path as String as follow:

_controller = VideoPlayerController.asset(
        'assets/name_of_your_video.mp4')
      ..initialize().then((_) {
        setState(() {});
      });

This will work, hope this help.

Upvotes: 9

Related Questions