fetchAudio method

  1. @override
Future<File?> fetchAudio(
  1. {required AppModel appModel,
  2. required BuildContext context,
  3. required String term,
  4. 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;
}