Flutter better_player: How to play video from local assets folder

1,573

So first I had to copy the file to the ApplicationDocuments directory

  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());
      _loadIntroVideo(file.path);
    } catch (e) {
      print("crap");
    }
  }

Then I could load it up from the new fullPath just created.

  void _loadIntroVideo(String fullPath) {
    var config = BetterPlayerConfiguration(
      fit: BoxFit.cover,
      autoPlay: true,
      fullScreenByDefault: false,
      // ...
    );

    BetterPlayerDataSource betterPlayerDataSource =
          BetterPlayerDataSource(BetterPlayerDataSourceType.file, fullPath);

    // etc
  }
Share:
1,573
Dave
Author by

Dave

Updated on December 31, 2022

Comments

  • Dave
    Dave over 1 year

    Q) Anyone know how to reference a file locally for both iOS and Android with better_player?

    I cannot find a way to do this with their docs, I know it can be done with the in-house Flutter VideoPlayer plugin BUT I want to use better_player instead.

    This works with the standard video plugin:

    _videoController = VideoPlayerController.asset('assets/intro.mp4');
    

    e.g. load a video in the project root at /assets/intro.mp4

    I've tried this with better_player, but it doesn't find the file:

        BetterPlayerDataSource betterPlayerDataSource = BetterPlayerDataSource(
            BetterPlayerDataSourceType.file, "assets/sample.mp4");
    
  • Dave
    Dave almost 3 years
    This is basically correct, except it omits the first part, which is to copy the file to the ApplicationDocumentsDirectory first, as per the example app in github. Thanks