2

Q) 任何人都知道如何使用 Better_player 在本地为 iOS 和 Android 引用文件?

我找不到使用他们的文档的方法,我知道可以使用内部 Flutter VideoPlayer 插件来完成,但我想改用 better_player。

这适用于标准视频插件:

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

例如,在 /assets/intro.mp4 的项目根目录中加载视频

我已经用 better_player 试过了,但它没有找到文件:

    BetterPlayerDataSource betterPlayerDataSource = BetterPlayerDataSource(
        BetterPlayerDataSourceType.file, "assets/sample.mp4");
4

2 回答 2

0

我的例子:

videoPage.dart

import 'package:better_player/better_player.dart';
import 'package:better_player_example/utils.dart';
import 'package:flutter/material.dart';

return Scaffold(
          appBar: AppBar(
            title: Text("better player example"),
          ),
          body: Column(
            children: [
              FutureBuilder<String>(
                future: Utils.getFileUrl("testvideo.mp4"),
                builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
                  if (snapshot.data != null) {
                    return BetterPlayer.file(snapshot.data!);
                  } else {
                    return const SizedBox();
                  }
                },
              )
            ],
          ),
        );

实用工具.dart

import 'package:path_provider/path_provider.dart';

class Utils {
  static Future<String> getFileUrl(String fileName) async {
    final directory = await getApplicationDocumentsDirectory();
    return "${directory.path}/$fileName";
  }
}
于 2021-07-13T13:44:39.827 回答
0

所以首先我必须将文件复制到ApplicationDocuments目录

  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");
    }
  }

fullPath然后我可以从刚刚创建的新文件中加载它。

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

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

    // etc
  }
于 2021-07-14T11:03:49.797 回答