fetchAudio method
- {required AppModel appModel,
- required BuildContext context,
- required String term,
- required String reading}
override
Given a term and a reading, fetch an audio file that is playable for the play audio action.
Implementation
@override
Future<File?> fetchAudio({
required AppModel appModel,
required BuildContext context,
required String term,
required String reading,
}) async {
Directory appDirDoc = await getApplicationSupportDirectory();
String jpdAudioPath = '${appDirDoc.path}/japanesePod101';
Directory jpdAudioDir = Directory(jpdAudioPath);
if (jpdAudioDir.existsSync()) {
jpdAudioDir.deleteSync(recursive: true);
}
jpdAudioDir.createSync();
late String audioUrl;
if (_kanaKit.isKana(term)) {
audioUrl =
'http://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kana=$term';
} else {
audioUrl =
'http://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=$term&kana=$reading';
}
File file = File('$jpdAudioPath/$term-$reading.mp3');
try {
File networkFile = await DefaultCacheManager().getSingleFile(audioUrl);
if (networkFile.readAsBytesSync().lengthInBytes == 52288) {
return null;
}
networkFile.copySync(file.path);
} catch (e) {
return null;
}
return file;
}