我有一个Flutter项目,我想从url下载视频,然后在iOS的应用程序中播放。
以下用于下载和获取路径的代码,但未在VideoPlayer中播放。
class Download extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'File Download',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'File Download With Progress'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool downloading = false;
String progress = '0';
bool isDownloaded = false;
String uri = 'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4';
String filename = 'video.mp4';
Future<void> downloadFile(uri, fileName) async {
setState(() {
downloading = true;
});
String savePath = await getFilePath(fileName);
Dio dio = Dio();
dio.download(
uri,
savePath,
onReceiveProgress: (rcv, total) {
print('received: ${rcv.toStringAsFixed(0)} out of total: ${total.toStringAsFixed(0)}');
setState(() {
progress = ((rcv / total) * 100).toStringAsFixed(0);
});
if (progress == '100') {
setState(() {
isDownloaded = true;
});
} else if (double.parse(progress) < 100) {}
},
deleteOnError: true,
).then((_) {
setState(() {
if (progress == '100') {
isDownloaded = true;
}
downloading = false;
});
});
}
Future<String> getFilePath(uniqueFileName) async {
String path = '';
Directory dir = await getApplicationDocumentsDirectory();
path = '${dir.path}/$uniqueFileName';
print(path);
return path;
}
@override
Widget build(BuildContext context) {
print('build running');
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('$progress%'),
isDownloaded ? Text('File Downloaded! You can see your file in the application\'s directory') : Text('Click the FloatingActionButton to start Downloading!'),
],
),
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.play_arrow),
onPressed: () async {
downloadFile(uri, filename);
}),
);
}
}
下载路径:/var/mobile/Containers/Data/Application/95AE9721-87D7-4FA8-9343-1B6D73B08FEE/Documents/video.mp4
我正在尝试搜索,但是找不到解决我问题的好方法。 谢谢