audio_waveforms
audio_waveforms copied to clipboard
fix: 🐛 Finalize iOS recording before returning path; fix file:// URL parsing
Description
On iOS, stopRecording() could return the file path before AVAudioRecorder finished writing the file to disk. stop() finalizes the audio container asynchronously, so reading the file or releasing the recorder immediately produced a truncated/invalid container. The recorded file then failed to load in the player with kAudioFileInvalidFileError ("Failed to prepare player").
This PR makes the following changes:
1. AudioRecorder.swift (iOS) — defer the result until the file is finalized
- Hold the pending
FlutterResult(s) in astopResultsqueue instead of returning immediately. - Implement the
audioRecorderDidFinishRecording(_:successfully:)delegate callback, which fires once the recorder has flushed the file to disk. The result (duration + path) is returned from there via a newfinalizeRecording(_:url:path:)helper. - Detach the recorder (
audioRecorder = nil) before callingstop()so a subsequentstartRecordingcan't redirect the URL/path that this stop is finalizing. The finishing recorder's own URL is used when finalizing. - A 2s watchdog (guarded by a
stopGenerationcounter) force-finalizes if the delegate never fires, so Dart'sstop()can't hang forever. A stale timer / the loser of delegate-vs-watchdog becomes a no-op. - Re-entrant
stop()calls now queue and all resolve with the same finalized result — a secondstop()no longer returns an empty map / null path. - Guard against an
Int(NaN)crash when the container duration is invalid/unfinished (only convert when finite). - If the recorder isn't actively recording when
stopRecordingis called, finalize synchronously (no callback to wait for).
2. AudioPlayer.swift (iOS + macOS) — harden file URL handling
URL(string:)mis-parses raw filesystem paths that have no scheme, and truncatesfile://URLs at#/?. Nowfile://paths have the scheme stripped (and ahostsegment dropped when present), are percent-decoded, and resolved viaURL(fileURLWithPath:); all other paths go straight toURL(fileURLWithPath:). This prevents anilURL / load failure for plain file paths and for filenames containing#/?.- The same fix is applied to the macOS player, which carried the identical
URL(string:)bug introduced with macOS support.
Checklist
- [x] The title of my PR starts with a Conventional Commit prefix (
fix:,feat:,docs:etc). - [x] I have followed the Contributor Guide when preparing my PR.
- [ ] I have updated/added tests for ALL new/updated/fixed functionality.
- [ ] I have updated/added relevant documentation in
docsand added dartdoc comments with///. - [ ] I have updated/added relevant examples in
examplesordocs.
Breaking Change?
- [ ] Yes, this PR is a breaking change.
- [x] No, this PR is not a breaking change.
Related Issues
Closes #432