Skip to content

Commit 3dad0ad

Browse files
committed
Implement Builder pattern for WaveRecorder class instantiation
1 parent a59eff8 commit 3dad0ad

File tree

2 files changed

+29
-9
lines changed
  • android-wave-recorder/src/main/java/com/github/squti/androidwaverecorder
  • sample/src/main/java/com/github/squti/androidwaverecordersample

2 files changed

+29
-9
lines changed

android-wave-recorder/src/main/java/com/github/squti/androidwaverecorder/WaveRecorder.kt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ class WaveRecorder(private var filePath: String) {
5353
/**
5454
* Configuration for recording audio file.
5555
*/
56+
@Deprecated(
57+
"Use configureWaveSettings to set recording configuration. Access to this property will not be available in the future."
58+
)
5659
var waveConfig: WaveConfig = WaveConfig()
5760

58-
/**
59-
* Configuration for Silence Detection.
60-
*/
61-
var silenceDetectionConfig: SilenceDetectionConfig = SilenceDetectionConfig(30)
61+
private var silenceDetectionConfig: SilenceDetectionConfig = SilenceDetectionConfig(30)
6262

6363
/**
6464
* Register a callback to be invoked in every recorded chunk of audio data
@@ -106,6 +106,22 @@ class WaveRecorder(private var filePath: String) {
106106
private var silenceDuration = 0L
107107
private var currentState: RecorderState = RecorderState.STOP
108108

109+
/**
110+
* Set configuration for recording audio file.
111+
*/
112+
fun configureWaveSettings(block: WaveConfig.() -> Unit): WaveRecorder {
113+
waveConfig.apply(block)
114+
return this
115+
}
116+
117+
/**
118+
* Set configuration for Silence Detection.
119+
*/
120+
fun configureSilenceDetection(block: SilenceDetectionConfig.() -> Unit): WaveRecorder {
121+
silenceDetectionConfig.apply(block)
122+
return this
123+
}
124+
109125
/**
110126
* Starts audio recording asynchronously and writes recorded data chunks on storage.
111127
*/

sample/src/main/java/com/github/squti/androidwaverecordersample/MainActivity.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import androidx.appcompat.app.AppCompatActivity
3535
import androidx.core.app.ActivityCompat
3636
import androidx.core.content.ContextCompat
3737
import com.github.squti.androidwaverecorder.RecorderState
38-
import com.github.squti.androidwaverecorder.SilenceDetectionConfig
3938
import com.github.squti.androidwaverecorder.WaveRecorder
4039
import com.github.squti.androidwaverecordersample.databinding.ActivityMainBinding
4140
import java.util.Locale
@@ -58,10 +57,15 @@ class MainActivity : AppCompatActivity() {
5857
filePath = filesDir.absolutePath + "/audioFile.wav"
5958

6059
waveRecorder = WaveRecorder(filePath)
61-
waveRecorder.waveConfig.sampleRate = 44100
62-
waveRecorder.waveConfig.channels = AudioFormat.CHANNEL_IN_MONO
63-
waveRecorder.waveConfig.audioEncoding = AudioFormat.ENCODING_PCM_32BIT
64-
waveRecorder.silenceDetectionConfig = SilenceDetectionConfig(minAmplitudeThreshold = 80)
60+
.configureWaveSettings {
61+
sampleRate = 44100
62+
channels = AudioFormat.CHANNEL_IN_STEREO
63+
audioEncoding = AudioFormat.ENCODING_PCM_32BIT
64+
}.configureSilenceDetection {
65+
minAmplitudeThreshold = 80
66+
bufferDurationInMillis = 1500
67+
preSilenceDurationInMillis = 1500
68+
}
6569

6670

6771

0 commit comments

Comments
 (0)