Cannot record audio on iOS
Describe the bug
Audio does not record on iOS. Works fine on Android emulator and physical device. Microphone permissions are included in Info.plist.
Plugin version is 1.0.5.
Audio appears to record, but:
- No waveforms are shown in
AudioWaveformswidget during recording - Recorded file size is always the same
- Waveforms are all near 0.0
- No error during recording and a path is returned after recording using
final path = await controller.stop(), but file will not play
Recording code:
await controller
.record(
path: '$tempDir/myfile.opus',
sampleRate: 48000,
bitRate: 10000,
androidEncoder: AndroidEncoder.opus,
iosEncoder: IosEncoder.kAudioFormatOpus,
);
After recording a file, the path is returned and the file size of the returned file path is always 16384 bytes. Recording with the path parameter empty does not change anything.
When attempting to play back a file from the returned path after recording using the AudioFileWaveforms widget:
flutter: PlatformException(AudioWaveforms, Couldn't read into buffer. Error Domain=com.apple.coreaudio.avfaudio Code=-50 "(null)" UserInfo={false condition=buffer.frameCapacity != 0}, null, null)
flutter:
#0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
<asynchronous suspension>
#2 AudioWaveformsInterface.extractWaveformData (package:audio_waveforms/src/base/audio_waveforms_interface.dart:182:9)
<asynchronous suspension>
#3 PlayerController.extractWaveformData (package:audio_waveforms/src/controllers/player_controller.dart:169:20)
<asynchronous suspension>
flutter: ----------------------------------------------------
Expected behavior
- Audio waveforms should show while recording on both platforms
- File should record properly
- If file cannot record properly, error should be thrown
Smartphone (please complete the following information):
- Device: iPhone 11 physical device
- OS: iOS 17.4.1
@jt274 Based of this stack answer can you please lowering the bitrate?
@Ujas-Majithiya Oh, the sample rate? If I change the sample rate to 24000 or 16000, I get No such file or directory when trying to access the file path returned by controller.stop() (and no waveforms are shown while recording). If I set the sample rate to 44100 or 48000, I get the original error posted above.
I should also note that changing the recording format to something else, such as kAudioFormatMPEG4AAC produces the same error as above.
@jt274 To record with the opus encoder, your file format(extension) has to be .caf as Apple only supports their proprietary format for opus. After doing this, waves were produced, and I was able to play the audio fine after it.
But one thing to note here is that while trying to extract waves from the .caf file it was only able to read half of the file's buffer and then it threw an exception. So in UI, you will see out of 100 there are only 50 waves and the other are 0 height waves.
@Ujas-Majithiya Unfortunately changing the file extension to .caf does not fix the issue. As I mentioned previously, the issue still occurs even if the recording format is set to AAC and the file extension to .m4a.
If you are using AAC with m4a then the issue is with the bitrate. Please check if the encoder supports the bitrate you are setting. Try some higher value like 320k or make it null, recorder will take the default value.
@Ujas-Majithiya Ok, an update:
- If I do not include the
bitRateat all, the audio records and waveforms show. I can record in opus or aac (even typed the wrong file extension on accident and it worked). On playback, the waveforms show and sound works but I still get theCouldn't read into buffererror for some reason.
So it seems that manually setting bitRate on iOS for multiple formats does not work.
@jt274 thanks!