Skip to content

Instantly share code, notes, and snippets.

@apptects
Created April 8, 2019 10:50
Show Gist options
  • Select an option

  • Save apptects/e3a2f42fc020c2b1bc13945cdc75362c to your computer and use it in GitHub Desktop.

Select an option

Save apptects/e3a2f42fc020c2b1bc13945cdc75362c to your computer and use it in GitHub Desktop.
Audio clip downloader
class AudioDownloader {
static final AudioDownloader _singleton = new AudioDownloader._internal();
factory AudioDownloader() {
return _singleton;
}
AudioDownloader._internal();
Future<String> downloadUrl(String url) async {
Uint8List rawData;
try {
rawData = await readBytes(url);
} on Exception catch(e) {
print('Failed to download $url ($e)');
return '';
}
final documentsDirectory = await getApplicationDocumentsDirectory();
final temporaryFile = File('${documentsDirectory.path}/${basename(url)}');
await temporaryFile.writeAsBytes(rawData);
return temporaryFile.path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment