getAudioFiles method

Future<List<File>> getAudioFiles()

Implementation

Future<List<File>> getAudioFiles() async {
  final directory = await getApplicationDocumentsDirectory();
  final audioFiles = <File>[];
  final supportedExtensions = ['.mp3', '.wav', '.aac', '.m4a'];

  bool hasSupportedExtension(File file) {
    for (final extension in supportedExtensions) {
      if (file.path.toLowerCase().endsWith(extension)) {
        return true;
      }
    }
    return false;
  }

  try {
    final files = directory.listSync(recursive: false, followLinks: false);

    for (final file in files) {
      if (file is File && hasSupportedExtension(file)) {
        audioFiles.add(file);
      }
    }
  } catch (e) {
    debugPrint('Error while fetching audio files: $e');
  }

  return audioFiles;
}