Flutter-使用path_provider将下载的文件路径获取到video_player时出错

时间:2019-04-07 21:08:27

标签: dart flutter

我的应用程序将下载视频,在第一个屏幕中将可以下载视频,我正在使用dio并使用path_provider获取应用程序文件夹,在第二个屏幕中,我要显示并播放下载的视频。

下载正常,已存储在正确的路径中,但是我无法检索到video_player的相同路径,得到的是“找不到文件”。

文件下载路径:/ data / user / 0 / APP_NAME / app_flutter / ATW-全世界。mp4

错误显示的路径:flutter_assets // data / user / 0 / APP_NAME / app_flutter / ATW-环游世界。mp4

看起来好像不一样。

Dio dio = Dio();

      try {

        var dir = await getApplicationDocumentsDirectory();

        for (int i = 0; i < urls.length; i++){

          path = "${dir.path}/${tricks[i]}.mp4";

          await dio.download(urls[i], path, onReceiveProgress: (rec, total){

            setState(() {

              progress = ((rec / total) * 100).toStringAsFixed(0) + "%";

            });

          });

          print(path);

        }


      } catch (e){
        print(e);
      }

===============


import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:video_player/video_player.dart';

class VideoScreen extends StatefulWidget {

  String trickName;


  VideoScreen(this.trickName);

  @override
  _VideoScreenState createState() => _VideoScreenState(trickName);
}

class _VideoScreenState extends State<VideoScreen> {

  String trickName;
  VideoPlayerController _controller;
  dynamic videoPath;

  _VideoScreenState(this.trickName);

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _getVideoPath();

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Trick"),
         centerTitle: true,
      ),
      body: FutureBuilder(
        future: _getVideoPath(),
          builder: (context, snapshot){
          if(snapshot.connectionState == ConnectionState.done){
            return Center(
              child: _controller.value.initialized ? AspectRatio(
                aspectRatio: _controller.value.aspectRatio,
                child: VideoPlayer(_controller),
              )
              : Center(
                child: CircularProgressIndicator(),
              ),
            );
            }

          }
      ),


    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller.dispose();
  }

  Future<void> _getVideoPath() async{

    try{

      var dir = await getApplicationDocumentsDirectory();

      videoPath = "${dir.path}/$trickName.mp4";

      print(videoPath);


      _controller = VideoPlayerController.asset(videoPath)..initialize()
          .then((_){
        setState(() {

        });
      });

    }catch (e){
      print(e);
    }

  }


}

I / ExoPlayerImpl(20059):初始化5dd991d [ExoPlayerLib / 2.9.6] [m253,LG-K430,LGE,23]     I / ExoPlayerImpl(20059):初始化8355563 [ExoPlayerLib / 2.9.6] [m253,LG-K430,LGE,23]     E / ExoPlayerImplInternal(20059):源错误。     E / ExoPlayerImplInternal(20059):com.google.android.exoplayer2.upstream.AssetDataSource $ AssetDataSourceException:java.io.FileNotFoundException:flutter_assets // data / user / 0 / APP_NAME / app_flutter / ATW-世界各地。mp4     E / ExoPlayerImplInternal(20059):位于com.google.android.exoplayer2.upstream.AssetDataSource.open(AssetDataSource.java:98)     E / ExoPlayerImplInternal(20059):位于com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:250)     E / ExoPlayerImplInternal(20059):位于com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)     E / ExoPlayerImplInternal(20059):位于com.google.android.exoplayer2.source.ExtractorMediaPeriod $ ExtractingLoadable.load(ExtractorMediaPeriod.java:885)     E / ExoPlayerImplInternal(20059):位于com.google.android.exoplayer2.upstream.Loader $ LoadTask.run(Loader.java:381)     E / ExoPlayerImplInternal(20059):位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)     E / ExoPlayerImplInternal(20059):位于java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588)     E / ExoPlayerImplInternal(20059):位于java.lang.Thread.run(Thread.java:818)     E / ExoPlayerImplInternal(20059):由以下原因引起:java.io.FileNotFoundException:flutter_assets // data / user / 0 / APP_NAME / app_flutter / ATW-遍布世界。mp4     E / ExoPlayerImplInternal(20059):位于android.content.res.AssetManager.openAsset(本机方法)     E / ExoPlayerImplInternal(20059):位于android.content.res.AssetManager.open(AssetManager.java:313)     E / ExoPlayerImplInternal(20059):位于com.google.android.exoplayer2.upstream.AssetDataSource.open(AssetDataSource.java:79)     E / ExoPlayerImplInternal(20059):...还有7个

1 个答案:

答案 0 :(得分:0)

video_player具有文件功能:VideoPlayerController.file(File)

我的Android Studio没有显示该功能,因此我认为该功能尚未实现。

现在,我可以从应用程序文件夹中加载视频了。