ARTICLE AD BOX
I'm learning to build a simple audio recorder app in Jetpack Compose using the native MediaRecorder. When testing it in the Android emulator, it sometimes produces a low-frequency sound throughout the entire file, but other times it works just fine. This issue doesn't appear on a real device, and there are no suspicious logs even when the low-frequency sound occurs.
The low-frequency sound: https://s3.eliaschen.dev/public/any/record_practice.mp3
here's my implementation for MediaRecoder
class AudioRecord(private val context: Context) { private var mediaRecorder: MediaRecorder? = null var isRecording by mutableStateOf(false) private set fun start(outputFile: File) { if (isRecording) return mediaRecorder = MediaRecorder(context).apply { setAudioSource(MediaRecorder.AudioSource.MIC) setOutputFormat(MediaRecorder.OutputFormat.MPEG_4) setAudioEncoder(MediaRecorder.AudioEncoder.AAC) setAudioSamplingRate(44100) setAudioEncodingBitRate(128000) setOutputFile(outputFile) prepare() start() isRecording = true } } fun stop() { if (!isRecording) return mediaRecorder?.stop() mediaRecorder?.release() mediaRecorder = null isRecording = false } }