Make recording in-place and fix the WAV header on the Android platform #151
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I notice that all platforms except Android record audio in place. For example, during recording, the audio file should be saved directly to the specified path in the code.
Plugin.Maui.Audio/src/Plugin.Maui.Audio/AudioRecorder/IAudioRecorder.shared.cs
Line 27 in ba47f60
However, on Android, the audio file is initially saved to a temporary path during recording and then copied to the specified path after recording stops. This process may lead to issues such as losing important audio files if the app crashes during recording. But if we save the audio file directly to the specified path during recording, we can simply repair the WAV header at that path to recover it in case the recording does not stop normally.
To fix this issue, I think we can write the WAV header before the audio data to reserve space, and then update it after recording to finalize the audio file.
Additionally, I also found some issues with creating WAV headers on the Android platform.
BlockAlign(Number of bytes per block) shoud be (NumberOfChannels* BitsPerSample / 8). The default channel is mono, so this option is incorrect by default setting.
Plugin.Maui.Audio/src/Plugin.Maui.Audio/AudioRecorder/AudioRecorder.android.cs
Line 244 in ba47f60
Thanks to PR #108, we can adjust the bit depth (number of bits per sample), so it should be variable rather than constant. If you use Pcm8bit on the current version of Plugin.Maui.Audio.Sample, it will produce incorrect results, playing as noise.
Plugin.Maui.Audio/src/Plugin.Maui.Audio/AudioRecorder/AudioRecorder.android.cs
Line 246 in ba47f60