extractAudioAmplitudesFromFile function

Future<List<double>> extractAudioAmplitudesFromFile(
  1. Uri fileUri,
  2. int linesNeeded
)

Factory method to extract amplitudes from any supported audio file.

Implementation

Future<List<double>> extractAudioAmplitudesFromFile(Uri fileUri, int linesNeeded) async {
  late Uint8List bytes;
  if (kIsWeb) {
    final response = await http.get(fileUri);
    if (response.statusCode != 200) throw Exception('Failed to load audio file from network');
    bytes = response.bodyBytes;
  } else if (fileUri.path.startsWith('assets')) {
    bytes = (await rootBundle.load(fileUri.path)).buffer.asUint8List();
  } else {
    bytes = await File(fileUri.toFilePath()).readAsBytes();
  }
  return _extractAmplitudes(bytes, linesNeeded);
}