sniffFile function

Future<AudioProbe> sniffFile(
  1. String path
)

Identifies the audio file at path, reading only the bytes the parsers need (usually a few KiB, never more than a few MiB however the file is built).

Throws a FileSystemException when the file cannot be opened: a missing file is an I/O problem, not an unknown format. Once open, the bytes can never cause an exception; unreadable regions are treated as the end of the file.

Implementation

Future<AudioProbe> sniffFile(String path) async {
  final file = await File(path).open();
  try {
    final length = await file.length();
    return await probeAsync((offset, count) async {
      try {
        await file.setPosition(offset);
        final data = await file.read(count);
        return (bytes: data, atEnd: data.length < count);
      } on FileSystemException {
        return (bytes: Uint8List(0), atEnd: true);
      }
    }, length);
  } finally {
    await file.close();
  }
}