Skip to content

Commit

Permalink
fix: Improve microphone selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Oct 21, 2023
1 parent 027e41d commit 5b7ce77
Showing 1 changed file with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import app.myzel394.alibi.services.AudioRecorderService
import app.myzel394.alibi.services.RecorderService
import app.myzel394.alibi.ui.utils.MicrophoneInfo

class AudioRecorderModel: ViewModel() {
class AudioRecorderModel : ViewModel() {
var recorderState by mutableStateOf(RecorderState.IDLE)
private set
var recordingTime by mutableStateOf<Long?>(null)
private set
var amplitudes by mutableStateOf<List<Int>>(emptyList())
private set
var selectedMicrophone by mutableStateOf<MicrophoneInfo?>(null)
private set

var onAmplitudeChange: () -> Unit = {}

Expand All @@ -46,28 +48,35 @@ class AudioRecorderModel: ViewModel() {

private val connection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, service: IBinder) {
recorderService = ((service as RecorderService.RecorderBinder).getService() as AudioRecorderService).also {recorder ->
recorder.onStateChange = { state ->
recorderState = state
}
recorder.onRecordingTimeChange = { time ->
recordingTime = time
}
recorder.onAmplitudeChange = { amps ->
amplitudes = amps
onAmplitudeChange()
recorderService =
((service as RecorderService.RecorderBinder).getService() as AudioRecorderService).also { recorder ->
// Update UI when the service changes
recorder.onStateChange = { state ->
recorderState = state
}
recorder.onRecordingTimeChange = { time ->
recordingTime = time
}
recorder.onAmplitudeChange = { amps ->
amplitudes = amps
onAmplitudeChange()
}
recorder.onError = {
recorderService!!.createLastRecording()
onError()
}
recorder.onSelectedMicrophoneChange = { microphone ->
selectedMicrophone = microphone
}
}.also {
// Init UI from the service
it.startRecording()

recorderState = it.state
recordingTime = it.recordingTime
amplitudes = it.amplitudes
selectedMicrophone = it.selectedMicrophone
}
recorder.onError = {
recorderService!!.createLastRecording()
onError()
}
}.also {
it.startRecording()

recorderState = it.state
recordingTime = it.recordingTime
amplitudes = it.amplitudes
}
}

override fun onServiceDisconnected(arg0: ComponentName) {
Expand All @@ -80,6 +89,7 @@ class AudioRecorderModel: ViewModel() {
recorderState = RecorderState.IDLE
recordingTime = null
amplitudes = emptyList()
selectedMicrophone = null
}

fun startRecording(context: Context) {
Expand Down Expand Up @@ -120,7 +130,7 @@ class AudioRecorderModel: ViewModel() {
}

fun changeMicrophone(microphone: MicrophoneInfo?) {
recorderService!!.selectedDevice = microphone
recorderService!!.changeMicrophone(microphone)
}

fun bindToService(context: Context) {
Expand Down

0 comments on commit 5b7ce77

Please sign in to comment.