mirror of
https://github.com/SimpleMobileTools/Simple-Voice-Recorder.git
synced 2025-06-05 21:59:31 +02:00
run the timer during recording
This commit is contained in:
@ -22,11 +22,14 @@ import com.simplemobiletools.voicerecorder.R
|
|||||||
import kotlinx.android.synthetic.main.activity_main.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
class MainActivity : SimpleActivity() {
|
class MainActivity : SimpleActivity() {
|
||||||
private var isRecording = false
|
private var isRecording = false
|
||||||
private var recorder: MediaRecorder? = null
|
private var recorder: MediaRecorder? = null
|
||||||
private var currFilePath = ""
|
private var currFilePath = ""
|
||||||
|
private var duration = 0
|
||||||
|
private val timer = Timer()
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@ -89,7 +92,8 @@ class MainActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initVoiceRecorder() {
|
private fun initVoiceRecorder() {
|
||||||
updateRecordingDuration(0)
|
duration = 0
|
||||||
|
updateRecordingDuration()
|
||||||
toggle_recording_button.setOnClickListener {
|
toggle_recording_button.setOnClickListener {
|
||||||
toggleRecording()
|
toggleRecording()
|
||||||
}
|
}
|
||||||
@ -106,8 +110,8 @@ class MainActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateRecordingDuration(seconds: Int) {
|
private fun updateRecordingDuration() {
|
||||||
recording_duration.text = seconds.getFormattedDuration()
|
recording_duration.text = duration.getFormattedDuration()
|
||||||
}
|
}
|
||||||
|
|
||||||
// mp4 output format with aac encoding should produce good enough mp3 files according to https://stackoverflow.com/a/33054794/1967672
|
// mp4 output format with aac encoding should produce good enough mp3 files according to https://stackoverflow.com/a/33054794/1967672
|
||||||
@ -132,6 +136,7 @@ class MainActivity : SimpleActivity() {
|
|||||||
try {
|
try {
|
||||||
prepare()
|
prepare()
|
||||||
start()
|
start()
|
||||||
|
timer.scheduleAtFixedRate(timerTask, 1000, 1000)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
showErrorToast(e)
|
showErrorToast(e)
|
||||||
}
|
}
|
||||||
@ -139,6 +144,7 @@ class MainActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun stopRecording() {
|
private fun stopRecording() {
|
||||||
|
timer.cancel()
|
||||||
recorder?.apply {
|
recorder?.apply {
|
||||||
stop()
|
stop()
|
||||||
release()
|
release()
|
||||||
@ -191,6 +197,15 @@ class MainActivity : SimpleActivity() {
|
|||||||
toast(msg, Toast.LENGTH_LONG)
|
toast(msg, Toast.LENGTH_LONG)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val timerTask = object : TimerTask() {
|
||||||
|
override fun run() {
|
||||||
|
duration++
|
||||||
|
runOnUiThread {
|
||||||
|
updateRecordingDuration()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun getToggleButtonIcon(): Drawable {
|
private fun getToggleButtonIcon(): Drawable {
|
||||||
val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_mic_vector
|
val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_mic_vector
|
||||||
return resources.getColoredDrawableWithColor(drawable, getFABIconColor())
|
return resources.getColoredDrawableWithColor(drawable, getFABIconColor())
|
||||||
|
Reference in New Issue
Block a user