A lightweight audio recording library that records in .wav format
Kotlin: build.gradle.kts
dependencies {
implementation("com.github.karya-inc:rawaudiorecorder:<latest_release>")
}
Groovy: build.gradle
dependencies {
implementation 'com.github.karya-inc:rawaudiorecorder:<latest_release>'
}
Create an instance of RecorderEventListener
private val listener = object : RecorderEventListener {
override fun onPause() {}
override fun onResume() {}
override fun onPrepared() {
}
// maxAmplitude: Maximum amplitude in current chunk, duration: Time elapses in seconds
override fun onProgressUpdate(maxAmplitude: Int, duration: Long) {
}
override fun onStart() {
}
// durationMs: duration of the recorded file in milliseconds
override fun onStop(durationMs: Long) {
}
}
Create an instance of RawAudioRecorder. RawAudioRecoder needs a CoroutineScope, which can be lifecyclsScope if you're creating it inside Activity or preferably viewModelScope when using in an MVVM project.
val recorder = RawAudioRecorder(listener, viewModelScope)
Prepare the recorder
recorder.prepare(filePath = audioFilepath, config = RecorderConfig(), suppressNoise = true)
Start the recording
recorder.startRecording()
// Pause
recorder.pauseRecording()
// Resume
recorder.resumeRecording()
// Stop
recorder.stopRecording()
data class RecorderConfig(
var sampleRate: SampleRate = SampleRate._16_K,
var channels: Int = AudioFormat.CHANNEL_IN_MONO,
var audioEncoding: AudioEncoding = AudioEncoding.PCM_16BIT
)
Configuration | Available values |
---|---|
sampleRate | SampleRate._8_K (8Khz) |
SampleRate._16_K (16Khz) | |
SampleRate._44_K (44.1Khz) | |
audioEncoding | AudioEncoding.PCM_8BIT |
AudioEncoding.PCM_16BIT | |
AudioEncoding.PCM_32BIT, required 'sdk >= Build.VERSION_CODES.S' | |
channels | AudioFormat.CHANNEL_IN_MONO, other configurations from android.media.AudioFormat |
Checkout the sample App for reference