使用 path_provider 到 video_player 获取下载的文件路径时出错
Posted
技术标签:
【中文标题】使用 path_provider 到 video_player 获取下载的文件路径时出错【英文标题】:Error getting a downloaded file path with path_provider to video_player 【发布时间】:2019-08-29 00:40:22 【问题描述】:我的应用程序将下载视频,在第一个屏幕上可以下载视频,我正在使用 dio 并使用 path_provider 获取应用程序文件夹,在第二个屏幕上我想显示并播放下载的视频。
下载正常,存储在正确的路径中,但我无法检索到 video_player 的相同路径,我收到“找不到文件”。
文件下载路径:/data/user/0/APP_NAME/app_flutter/ATW - Around the World.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(Native Method) 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】:video_player 有一个文件功能:VideoPlayerController.file(File)
我的 Android Studio 没有显示该功能,所以我认为它没有实现。
现在我可以从 app 文件夹加载视频了。
【讨论】:
以上是关于使用 path_provider 到 video_player 获取下载的文件路径时出错的主要内容,如果未能解决你的问题,请参考以下文章
任务 ':path_provider:extractDebugAnnotations' 执行失败
不允许操作(在项目“Pods”的目标“path_provider”中)
MissingPluginException(在通道 plugins.flutter.io/path_provider 上找不到方法 getApplicationDocumentsDirectory