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 {
  List<ForvoResult> results = await getForvoResults(
    appModel: appModel,
    searchTerm: term,
  );

  String temporaryDirectoryPath = (await getTemporaryDirectory()).path;
  String temporaryFileName =
      'jidoujisho-${DateFormat('yyyyMMddTkkmmss').format(DateTime.now())}';

  File file = File('$temporaryDirectoryPath/$temporaryFileName');
  if (results.isEmpty) {
    return null;
  }

  File networkFile =
      await DefaultCacheManager().getSingleFile(results[0].audioUrl);
  networkFile.copySync(file.path);

  return file;
}