i use this package :
youtube_explode_dart
and i found this method to download video
Future<void> downloadVideo(id) async {
var permisson = await Permission.storage.request();
if (permisson.isGranted) {
//download video
if (_urlTextFieldController.text != '') {
setState(() => _downloading = true);
//download video
setState(() => progress = 0);
var _youtubeExplode = YoutubeExplode();
//get video metadata
var video = await _youtubeExplode.videos.get(id);
var manifest =
await _youtubeExplode.videos.streamsClient.getManifest(id);
var streams = manifest.muxed.withHighestBitrate();
var audio = streams;
var audioStream = _youtubeExplode.videos.streamsClient.get(audio);
//create a directory
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
var file = File('$appDocPath/${video.id}');
//delete file if exists
if (file.existsSync()) {
file.deleteSync();
}
var output = file.openWrite(mode: FileMode.writeOnlyAppend);
var size = audio.size.totalBytes;
var count = 0;
await for (final data in audioStream) {
// Keep track of the current downloaded data.
count = data.length;
// Calculate the current progress.
double val = ((count / size));
var msg = '${video.title} Downloaded to $appDocPath/${video.id}';
for (val; val == 1.0; val ) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(msg)));
}
setState(() => progress = val);
// Write to file.
output.add(data);
}
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('add youtube video url first!')));
setState(() => _downloading = false);
}
} else {
await Permission.storage.request();
}
}
when the method was done Scaffold Messenger send message video wad downloaded but i can't found the video in gallery or in local storage
i use real device Android and i was add all permission to Android manifest .
anyone can help me :(
CodePudding user response:
The reason might be that you are using the path_provider function "getApplicationDocumentsDirectory()" to store the files. By replacing above Function with "getExternalStorageDirectory()" you would able to store the file in the external directory to appear in gallery. If that doesn’t work you can also use this plugin: https://pub.dev/packages/gallery_saver