adding some actual audio recording handling

This commit is contained in:
tibbi
2020-03-28 20:06:05 +01:00
parent c924474958
commit 7e2bed11c0

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.voicerecorder.activities
import android.content.Intent import android.content.Intent
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.media.MediaRecorder
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
@ -11,9 +12,11 @@ import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.voicerecorder.BuildConfig import com.simplemobiletools.voicerecorder.BuildConfig
import com.simplemobiletools.voicerecorder.R import com.simplemobiletools.voicerecorder.R
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import java.io.IOException
class MainActivity : SimpleActivity() { class MainActivity : SimpleActivity() {
private var isRecording = false private var isRecording = false
private var recorder: MediaRecorder? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -41,6 +44,12 @@ class MainActivity : SimpleActivity() {
} }
} }
override fun onStop() {
super.onStop()
recorder?.release()
recorder = null
}
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu) menuInflater.inflate(R.menu.menu, menu)
return true return true
@ -56,7 +65,6 @@ class MainActivity : SimpleActivity() {
} }
private fun initVoiceRecorder() { private fun initVoiceRecorder() {
val filename = "${getCurrentFormattedDateTime()}.mp3"
toggle_recording_button.setOnClickListener { toggle_recording_button.setOnClickListener {
toggleRecording() toggleRecording()
} }
@ -74,11 +82,29 @@ class MainActivity : SimpleActivity() {
} }
private fun startRecording() { private fun startRecording() {
val filename = "${getCurrentFormattedDateTime()}.3gp"
recorder = MediaRecorder().apply {
setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
setOutputFile(filename)
setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
try {
prepare()
} catch (e: IOException) {
showErrorToast(e)
}
start()
}
} }
private fun stopRecording() { private fun stopRecording() {
recorder?.apply {
stop()
release()
}
recorder = null
} }
private fun getToggleButtonIcon(): Drawable { private fun getToggleButtonIcon(): Drawable {