gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:03:46 29 4
gpt4 key购买 nike

我的应用程序将下载视频,在第一个屏幕上可以下载视频,我正在使用 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 - Around the World.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): Init 5dd991d [ExoPlayerLib/2.9.6] [m253, LG-K430, LGE, 23] I/ExoPlayerImpl(20059): Init 8355563 [ExoPlayerLib/2.9.6] [m253, LG-K430, LGE, 23] E/ExoPlayerImplInternal(20059): Source error. E/ExoPlayerImplInternal(20059): com.google.android.exoplayer2.upstream.AssetDataSource$AssetDataSourceException: java.io.FileNotFoundException: flutter_assets//data/user/0/APP_NAME/app_flutter/ATW - Around the World.mp4 E/ExoPlayerImplInternal(20059): at com.google.android.exoplayer2.upstream.AssetDataSource.open(AssetDataSource.java:98) E/ExoPlayerImplInternal(20059): at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:250) E/ExoPlayerImplInternal(20059): at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83) E/ExoPlayerImplInternal(20059): at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractingLoadable.load(ExtractorMediaPeriod.java:885) E/ExoPlayerImplInternal(20059): at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:381) E/ExoPlayerImplInternal(20059): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) E/ExoPlayerImplInternal(20059): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) E/ExoPlayerImplInternal(20059): at java.lang.Thread.run(Thread.java:818) E/ExoPlayerImplInternal(20059): Caused by: java.io.FileNotFoundException: flutter_assets//data/user/0/APP_NAME/app_flutter/ATW - Around the World.mp4 E/ExoPlayerImplInternal(20059): at android.content.res.AssetManager.openAsset(Native Method) E/ExoPlayerImplInternal(20059): at android.content.res.AssetManager.open(AssetManager.java:313) E/ExoPlayerImplInternal(20059): at com.google.android.exoplayer2.upstream.AssetDataSource.open(AssetDataSource.java:79) E/ExoPlayerImplInternal(20059): ... 7 more

最佳答案

video_player 有一个文件函数:VideoPlayerController.file(File)

我的 Android Studio 没有显示该功能,所以我认为它没有实现。

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

关于dart - 使用 path_provider 将下载的文件路径获取到 video_player 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55563689/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com