add broadcasting the recording status too

This commit is contained in:
tibbi
2020-03-30 15:29:17 +02:00
parent 9cea12a1e2
commit 0bac22fd11
5 changed files with 29 additions and 10 deletions

View File

@ -12,7 +12,7 @@ import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.voicerecorder.BuildConfig
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.helpers.GET_DURATION
import com.simplemobiletools.voicerecorder.helpers.GET_RECORDER_INFO
import com.simplemobiletools.voicerecorder.models.Events
import com.simplemobiletools.voicerecorder.services.RecorderService
import kotlinx.android.synthetic.main.activity_main.*
@ -93,7 +93,7 @@ class MainActivity : SimpleActivity() {
}
Intent(this@MainActivity, RecorderService::class.java).apply {
action = GET_DURATION
action = GET_RECORDER_INFO
startService(this)
}
}
@ -130,6 +130,12 @@ class MainActivity : SimpleActivity() {
updateRecordingDuration(event.duration)
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun gotStatusEvent(event: Events.RecordingStatus) {
isRecording = event.isRecording
toggle_recording_button.setImageDrawable(getToggleButtonIcon())
}
private fun getToggleButtonIcon(): Drawable {
val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_mic_vector
return resources.getColoredDrawableWithColor(drawable, getFABIconColor())

View File

@ -3,4 +3,4 @@ package com.simplemobiletools.voicerecorder.helpers
const val RECORDER_RUNNING_NOTIF_ID = 10000
private const val PATH = "com.simplemobiletools.voicerecorder.action."
const val GET_DURATION = PATH + "GET_DURATION"
const val GET_RECORDER_INFO = PATH + "GET_RECORDER_INFO"

View File

@ -2,4 +2,5 @@ package com.simplemobiletools.voicerecorder.models
class Events {
class RecordingDuration internal constructor(val duration: Int)
class RecordingStatus internal constructor(val isRecording: Boolean)
}

View File

@ -19,7 +19,7 @@ import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SplashActivity
import com.simplemobiletools.voicerecorder.helpers.GET_DURATION
import com.simplemobiletools.voicerecorder.helpers.GET_RECORDER_INFO
import com.simplemobiletools.voicerecorder.helpers.RECORDER_RUNNING_NOTIF_ID
import com.simplemobiletools.voicerecorder.models.Events
import org.greenrobot.eventbus.EventBus
@ -30,6 +30,7 @@ import java.util.*
class RecorderService : Service() {
private var currFilePath = ""
private var duration = 0
private var isRecording = false
private var timer = Timer()
private var recorder: MediaRecorder? = null
@ -39,7 +40,7 @@ class RecorderService : Service() {
super.onStartCommand(intent, flags, startId)
when (intent.action) {
GET_DURATION -> broadcastDuration()
GET_RECORDER_INFO -> broadcastRecorderInfo()
else -> startRecording()
}
@ -55,10 +56,6 @@ class RecorderService : Service() {
// mp4 output format with aac encoding should produce good enough mp3 files according to https://stackoverflow.com/a/33054794/1967672
private fun startRecording() {
startForeground(RECORDER_RUNNING_NOTIF_ID, showNotification())
duration = 0
broadcastDuration()
val baseFolder = if (isQPlus()) {
cacheDir
} else {
@ -80,16 +77,22 @@ class RecorderService : Service() {
prepare()
start()
duration = 0
isRecording = true
broadcastRecorderInfo()
startForeground(RECORDER_RUNNING_NOTIF_ID, showNotification())
timer = Timer()
timer.scheduleAtFixedRate(getTimerTask(), 1000, 1000)
} catch (e: IOException) {
showErrorToast(e)
stopRecording()
}
}
}
private fun stopRecording() {
timer.cancel()
isRecording = false
recorder?.apply {
stop()
@ -106,6 +109,11 @@ class RecorderService : Service() {
recorder = null
}
private fun broadcastRecorderInfo() {
broadcastDuration()
broadcastStatus()
}
@SuppressLint("InlinedApi")
private fun addFileInNewMediaStore() {
val audioCollection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
@ -186,4 +194,8 @@ class RecorderService : Service() {
private fun broadcastDuration() {
EventBus.getDefault().post(Events.RecordingDuration(duration))
}
private fun broadcastStatus() {
EventBus.getDefault().post(Events.RecordingStatus(isRecording))
}
}