Last active
January 9, 2020 21:33
-
-
Save kazemihabib/d9605b74a0e33a74b2c75d4a43e1ac98 to your computer and use it in GitHub Desktop.
Use kotlin, livedata, databinding with AudioRecordView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DON't forget this, otherwise it won't work | |
// In your activity | |
binding.lifecycleOwner = this | |
// create a binding adapter | |
@BindingAdapter("update") | |
fun setAmplitude(audioRecordView: AudioRecordView, data: Int) { | |
if (data == -1) audioRecordView.recreate() | |
else audioRecordView.update(data) | |
} | |
// and in XML: | |
<com.visualizer.amplitude.AudioRecordView | |
. | |
. | |
. | |
app:update="@{viewModel.amplitudeLiveData}" | |
. | |
. | |
. | |
/> | |
// and to set liveData value: | |
private val _amplitudeLiveData = MutableLiveData<Int>(0) | |
val amplitudeLiveData: LiveData<Int> | |
get() = _amplitudeLiveData | |
private var tickerChannel: ReceiveChannel<Unit>? = null | |
private fun emitAmplitude(enable: Boolean) { | |
if (enable) { | |
tickerChannel = ticker(delayMillis = 100, initialDelayMillis = 0) | |
_amplitudeLiveData.value = -1 | |
GlobalScope.launch { | |
for (event in tickerChannel!!) { | |
recorder?.maxAmplitude?.run { _amplitudeLiveData.postValue(this) } | |
} | |
} | |
} else { | |
tickerChannel?.cancel() | |
tickerChannel = null | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment