fix: Bugfixes for recording behavior

This commit is contained in:
Myzel394 2023-10-21 22:14:53 +02:00
parent 14abd1aee0
commit e4e8ae0158
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
5 changed files with 32 additions and 29 deletions

View File

@ -154,7 +154,7 @@ data class AudioRecorderSettings(
else MediaRecorder.OutputFormat.THREE_GPP
}
return when(encoder) {
return when (encoder) {
MediaRecorder.AudioEncoder.AAC -> MediaRecorder.OutputFormat.AAC_ADTS
MediaRecorder.AudioEncoder.AAC_ELD -> MediaRecorder.OutputFormat.AAC_ADTS
MediaRecorder.AudioEncoder.AMR_NB -> MediaRecorder.OutputFormat.AMR_NB
@ -167,6 +167,7 @@ data class AudioRecorderSettings(
MediaRecorder.OutputFormat.AAC_ADTS
}
}
MediaRecorder.AudioEncoder.OPUS -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
MediaRecorder.OutputFormat.OGG
@ -174,11 +175,12 @@ data class AudioRecorderSettings(
MediaRecorder.OutputFormat.AAC_ADTS
}
}
else -> MediaRecorder.OutputFormat.DEFAULT
}
}
fun getMimeType(): String = when(getOutputFormat()) {
fun getMimeType(): String = when (getOutputFormat()) {
MediaRecorder.OutputFormat.AAC_ADTS -> "audio/aac"
MediaRecorder.OutputFormat.THREE_GPP -> "audio/3gpp"
MediaRecorder.OutputFormat.MPEG_4 -> "audio/mp4"
@ -190,7 +192,7 @@ data class AudioRecorderSettings(
else -> "audio/3gpp"
}
fun getSamplingRate(): Int = samplingRate ?: when(getOutputFormat()) {
fun getSamplingRate(): Int = samplingRate ?: when (getOutputFormat()) {
MediaRecorder.OutputFormat.AAC_ADTS -> 96000
MediaRecorder.OutputFormat.THREE_GPP -> 44100
MediaRecorder.OutputFormat.MPEG_4 -> 44100
@ -202,11 +204,10 @@ data class AudioRecorderSettings(
else -> 48000
}
fun getEncoder(): Int = encoder ?:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
MediaRecorder.AudioEncoder.AAC
else
MediaRecorder.AudioEncoder.AMR_NB
fun getEncoder(): Int = encoder ?: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
MediaRecorder.AudioEncoder.AAC
else
MediaRecorder.AudioEncoder.AMR_NB
fun setIntervalDuration(duration: Long): AudioRecorderSettings {
if (duration < 10 * 1000L || duration > 60 * 60 * 1000L) {
@ -221,7 +222,6 @@ data class AudioRecorderSettings(
}
fun setBitRate(bitRate: Int): AudioRecorderSettings {
println("bitRate: $bitRate")
if (bitRate !in 1000..320000) {
throw Exception("Bit rate must be between 1000 and 320000")
}

View File

@ -30,16 +30,7 @@ class AudioRecorderService : IntervalRecorderService() {
val filePath: String
get() = "$folder/$counter.${settings!!.fileExtension}"
private fun clearAudioDevice() {
val audioManger = getSystemService(AUDIO_SERVICE)!! as AudioManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
audioManger.clearCommunicationDevice()
} else {
audioManger.stopBluetoothSco()
}
}
/// Tell Android to use the correct bluetooth microphone, if any selected
private fun startAudioDevice() {
if (selectedMicrophone == null) {
return
@ -54,6 +45,16 @@ class AudioRecorderService : IntervalRecorderService() {
}
}
private fun clearAudioDevice() {
val audioManger = getSystemService(AUDIO_SERVICE)!! as AudioManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
audioManger.clearCommunicationDevice()
} else {
audioManger.stopBluetoothSco()
}
}
private fun createRecorder(): MediaRecorder {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
MediaRecorder(this)
@ -185,7 +186,6 @@ class AudioRecorderService : IntervalRecorderService() {
}
}
@SuppressLint("NewApi")
private fun registerMicrophoneListener() {
val audioManager = getSystemService(Context.AUDIO_SERVICE)!! as AudioManager

View File

@ -102,7 +102,6 @@ abstract class RecorderService : Service() {
}
RecorderState.IDLE -> {
stop()
onDestroy()
}
}
@ -149,7 +148,6 @@ abstract class RecorderService : Service() {
super.onDestroy()
stop()
changeState(RecorderState.IDLE)
stopForeground(STOP_FOREGROUND_REMOVE)
NotificationManagerCompat.from(this)

View File

@ -151,8 +151,6 @@ fun RecordingStatus(
}
LaunchedEffect(microphoneStatus) {
println(microphoneStatus)
println(previousStatus)
if (microphoneStatus != previousStatus && showMicrophoneStatusDialog == null && previousStatus != null) {
showMicrophoneStatusDialog = microphoneStatus
}

View File

@ -1,9 +1,9 @@
package app.myzel394.alibi.ui.utils
import android.annotation.SuppressLint
import android.content.Context
import android.media.AudioDeviceInfo
import android.media.AudioManager
import android.os.Build
data class MicrophoneInfo(
val deviceInfo: AudioDeviceInfo,
@ -24,12 +24,19 @@ data class MicrophoneInfo(
return MicrophoneInfo(deviceInfo)
}
@SuppressLint("NewApi")
fun fetchDeviceMicrophones(context: Context): List<MicrophoneInfo> {
val audioManager = context.getSystemService(Context.AUDIO_SERVICE)!! as AudioManager
return audioManager.availableCommunicationDevices.let {
it.subList(2, it.size)
}.map(::fromDeviceInfo)
val mics =
audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS).map(::fromDeviceInfo)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
audioManager.availableCommunicationDevices.let {
it.subList(2, it.size)
}.map(::fromDeviceInfo)
} else {
audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS).let {
it.slice(1 until it.size)
}.map(::fromDeviceInfo)
}
}
}