@@ -33,8 +33,10 @@ import android.widget.Toast
33
33
import androidx.appcompat.app.AppCompatActivity
34
34
import androidx.core.app.ActivityCompat
35
35
import androidx.core.content.ContextCompat
36
+ import androidx.lifecycle.lifecycleScope
36
37
import com.github.squti.androidwaverecorder.RecorderState
37
38
import com.github.squti.androidwaverecorder.WaveRecorder
39
+ import com.github.squti.androidwaverecordersample.databinding.ActivityMainBinding
38
40
import kotlinx.android.synthetic.main.activity_main.*
39
41
import kotlinx.coroutines.Dispatchers
40
42
import kotlinx.coroutines.GlobalScope
@@ -49,10 +51,12 @@ class MainActivity : AppCompatActivity() {
49
51
private lateinit var filePath: String
50
52
private var isRecording = false
51
53
private var isPaused = false
54
+ private lateinit var binding: ActivityMainBinding
52
55
53
56
override fun onCreate (savedInstanceState : Bundle ? ) {
54
57
super .onCreate(savedInstanceState)
55
- setContentView(R .layout.activity_main)
58
+ binding = ActivityMainBinding .inflate(layoutInflater)
59
+ setContentView(binding.root)
56
60
57
61
filePath = externalCacheDir?.absolutePath + " /audioFile.wav"
58
62
@@ -67,10 +71,10 @@ class MainActivity : AppCompatActivity() {
67
71
}
68
72
waveRecorder.onTimeElapsed = {
69
73
Log .e(TAG , " onCreate: time elapsed $it " )
70
- timeTextView.text = formatTimeUnit(it * 1000 )
74
+ binding. timeTextView.text = formatTimeUnit(it * 1000 )
71
75
}
72
76
73
- startStopRecordingButton.setOnClickListener {
77
+ binding. startStopRecordingButton.setOnClickListener {
74
78
75
79
if (! isRecording) {
76
80
if (ContextCompat .checkSelfPermission(
@@ -92,30 +96,28 @@ class MainActivity : AppCompatActivity() {
92
96
}
93
97
}
94
98
95
- pauseResumeRecordingButton.setOnClickListener {
99
+ binding. pauseResumeRecordingButton.setOnClickListener {
96
100
if (! isPaused) {
97
101
waveRecorder.pauseRecording()
98
102
} else {
99
103
waveRecorder.resumeRecording()
100
104
}
101
105
}
102
- showAmplitudeSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
106
+ binding. showAmplitudeSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
103
107
if (isChecked) {
104
- amplitudeTextView.text = " Amplitude : 0"
105
- amplitudeTextView.visibility = View .VISIBLE
108
+ binding. amplitudeTextView.text = " Amplitude : 0"
109
+ binding. amplitudeTextView.visibility = View .VISIBLE
106
110
waveRecorder.onAmplitudeListener = {
107
- GlobalScope .launch(Dispatchers .Main ) {
108
- amplitudeTextView.text = " Amplitude : $it "
109
- }
111
+ binding.amplitudeTextView.text = " Amplitude : $it "
110
112
}
111
113
112
114
} else {
113
115
waveRecorder.onAmplitudeListener = null
114
- amplitudeTextView.visibility = View .GONE
116
+ binding. amplitudeTextView.visibility = View .GONE
115
117
}
116
118
}
117
119
118
- noiseSuppressorSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
120
+ binding. noiseSuppressorSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
119
121
waveRecorder.noiseSuppressorActive = isChecked
120
122
if (isChecked)
121
123
Toast .makeText(this , " Noise Suppressor Activated" , Toast .LENGTH_SHORT ).show()
@@ -127,30 +129,30 @@ class MainActivity : AppCompatActivity() {
127
129
Log .d(TAG , waveRecorder.audioSessionId.toString())
128
130
isRecording = true
129
131
isPaused = false
130
- messageTextView.visibility = View .GONE
131
- recordingTextView.text = " Recording..."
132
- recordingTextView.visibility = View .VISIBLE
133
- startStopRecordingButton.text = " STOP"
134
- pauseResumeRecordingButton.text = " PAUSE"
135
- pauseResumeRecordingButton.visibility = View .VISIBLE
136
- noiseSuppressorSwitch.isEnabled = false
132
+ binding. messageTextView.visibility = View .GONE
133
+ binding. recordingTextView.text = " Recording..."
134
+ binding. recordingTextView.visibility = View .VISIBLE
135
+ binding. startStopRecordingButton.text = " STOP"
136
+ binding. pauseResumeRecordingButton.text = " PAUSE"
137
+ binding. pauseResumeRecordingButton.visibility = View .VISIBLE
138
+ binding. noiseSuppressorSwitch.isEnabled = false
137
139
}
138
140
139
141
private fun stopRecording () {
140
142
isRecording = false
141
143
isPaused = false
142
- recordingTextView.visibility = View .GONE
143
- messageTextView.visibility = View .VISIBLE
144
- pauseResumeRecordingButton.visibility = View .GONE
145
- showAmplitudeSwitch.isChecked = false
144
+ binding. recordingTextView.visibility = View .GONE
145
+ binding. messageTextView.visibility = View .VISIBLE
146
+ binding. pauseResumeRecordingButton.visibility = View .GONE
147
+ binding. showAmplitudeSwitch.isChecked = false
146
148
Toast .makeText(this , " File saved at : $filePath " , Toast .LENGTH_LONG ).show()
147
- startStopRecordingButton.text = " START"
148
- noiseSuppressorSwitch.isEnabled = true
149
+ binding. startStopRecordingButton.text = " START"
150
+ binding. noiseSuppressorSwitch.isEnabled = true
149
151
}
150
152
151
153
private fun pauseRecording () {
152
- recordingTextView.text = " PAUSE"
153
- pauseResumeRecordingButton.text = " RESUME"
154
+ binding. recordingTextView.text = " PAUSE"
155
+ binding. pauseResumeRecordingButton.text = " RESUME"
154
156
isPaused = true
155
157
}
156
158
@@ -189,4 +191,4 @@ class MainActivity : AppCompatActivity() {
189
191
" 00:00"
190
192
}
191
193
}
192
- }
194
+ }
0 commit comments