You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Android Wave Recorder is a lightweight library written in Kotlin to record audio files with WAVE (WAV due to its filename extension) format in Android. It's very memory efficient and easy to use library with recording customizations.
10
+
Android Wave Recorder is a lightweight library written in Kotlin to record audio files in WAVE (WAV) format on Android. It’s memory efficient and easy to use, with customizable recording options like Silence Detection and high-quality audio encoding (Float and 32-bit)
11
11
12
12
### Download
13
-
Step 1. Add this in your root (Project) build.gradle at the end of repositories:
13
+
Step 1. Add this to your root (Project) `build.gradle` at the end of repositories:
14
14
```gradle
15
15
allprojects {
16
16
repositories {
@@ -19,39 +19,59 @@ allprojects {
19
19
}
20
20
}
21
21
```
22
-
Step 2. Add the dependency
22
+
Step 2. Add the following dependency to your module `build.gradle`:
Add these permissions into your `AndroidManifest.xml` and [request for them in Android 6.0+](https://developer.android.com/training/permissions/requesting.html)
29
+
Add these permissions to your `AndroidManifest.xml` and [request them at runtime for Android 6.0+](https://developer.android.com/training/permissions/requesting.html)
If you use [Scoped Storage](https://source.android.com/docs/core/storage/scoped) there is an example in the [Sample](https://github.com/squti/Android-Wave-Recorder/tree/master/sample) project.
35
35
### Usage
36
-
Pass in the path of the output file to `WaveRecorder` class and call `startRecording()` like this:
36
+
Pass the path of the output file to the `WaveRecorder` class and call `startRecording()`:
37
37
```kotlin
38
38
/**
39
-
* This path points to application cache directory.
40
-
* you could change it based on your usage
39
+
* This path points to the file directory in the application's internal storage.
40
+
* you can change it based on your usage
41
41
*/
42
-
val filePath:String=externalCacheDir?.absolutePath +"/audioFile.wav"
42
+
val filePath:String=filesDir.absolutePath +"/audioFile.wav"
43
43
44
44
val waveRecorder =WaveRecorder(filePath)
45
45
waveRecorder.startRecording()
46
46
47
47
```
48
-
To stop recording call `stopRecording()` function:
48
+
You can also pass a `URI` for the file path if you are dealing with [Scoped Storage](https://source.android.com/docs/core/storage/scoped):
49
+
```kotlin
50
+
val audioUri =MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
* This URI points to audioFile.wav in the Android-Wave-Recorder directory in Android's default Music folder.
58
+
* you can change it based on your usage
59
+
*/
60
+
val uri = contentResolver.insert(audioUri, contentValues)
61
+
62
+
val waveRecorder =WaveRecorder(uri, context =this)
63
+
waveRecorder.startRecording()
64
+
65
+
```
66
+
_[Here](https://github.com/squti/Android-Wave-Recorder/blob/master/sample/src/main/java/com/github/squti/androidwaverecordersample/MainActivity.kt) is an example of how to get URI before and after Android 10._
67
+
68
+
To stop recording, call the `stopRecording()` function:
49
69
```kotlin
50
70
waveRecorder.stopRecording()
51
71
52
72
```
53
73
54
-
To pause and resume recording you could use `pauseRecording()` and `resumeRecording()` functions:
74
+
To pause and resume recording, use the`pauseRecording()` and `resumeRecording()` functions:
To activate `Noise Suppressor` you could set `noiseSuppressorActive`to true:
84
+
To listen to audio amplitude during recording, register a listener to `onAmplitudeListener`:
65
85
```kotlin
66
-
waveRecorder.noiseSuppressorActive =true
67
-
86
+
waveRecorder.onAmplitudeListener = {
87
+
Log.i(TAG, "Amplitude : $it")
88
+
}
68
89
```
90
+
### Silence Detection
91
+
To activate the *Silence Detection*, set `silenceDetection` to `true`:
92
+
```kotlin
93
+
waveRecorder.silenceDetection =true
69
94
70
-
To listen to audio amplitude during recording you need to register a listener to `onAmplitudeListener`:
95
+
```
96
+
You can adjust the silence amplitude level based on your needs. The default threshold is 1500, meaning amplitudes below 1500 are considered silence. The recorder will pause until the amplitude goes above 1500 again. By default, the recorder buffers the last 2 seconds of silence and adds it to the file when recording resumes. Silence detection waits for 2 seconds after detecting silence; if silence continues, recording pauses. The recorder will resume when it detects sound again. You can adjust the **buffer time, silence waiting time, and amplitude threshold** using `configureSilenceDetection`:
71
97
```kotlin
72
-
waveRecorder.onAmplitudeListener = {
73
-
Log.i(TAG, "Amplitude : $it")
98
+
waveRecorder.configureSilenceDetection {
99
+
minAmplitudeThreshold =2000
100
+
bufferDurationInMillis =1500
101
+
preSilenceDurationInMillis =1500
74
102
}
103
+
75
104
```
105
+
_Note 1: Buffer and Silence Waiting Time may have slight inaccuracies due to the conversion from milliseconds to bytes in the background. Adjust these settings to achieve more accurate results._
76
106
77
-
To listen to recording state changes **(RECORDING, STOP and PAUSE)** you need to register a listener to `onStateChangeListener`:
107
+
_Note 2: Big buffer size can reduce the performance_
108
+
109
+
### Noise Suppression
110
+
To activate the `Noise Suppressor`, set `noiseSuppressorActive` to `true`:
111
+
```kotlin
112
+
waveRecorder.noiseSuppressorActive =true
113
+
114
+
```
115
+
_Note: If the device does not support Noise Suppressor it will not affect the output_
116
+
117
+
### Recording States
118
+
To listen to recording state changes **(RECORDING, STOP, PAUSE and SKIPPING SILENCE)**, register a listener to `onStateChangeListener`:
78
119
```kotlin
79
120
waveRecorder.onStateChangeListener = {
80
121
when (it) {
81
122
RecorderState.RECORDING->TODO()
82
123
RecorderState.STOP->TODO()
83
124
RecorderState.PAUSE->TODO()
125
+
RecorderState.SKIPPING_SILENCE->TODO()
84
126
}
85
127
}
86
128
```
87
129
### Configuration
88
-
The default configuration for recording audio is like so:
130
+
Android Wave Recorder supports **Float, 32-bit, 16-bit and 8-bit** encoding.
0 commit comments