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

@ -167,6 +167,7 @@ data class AudioRecorderSettings(
MediaRecorder.OutputFormat.AAC_ADTS MediaRecorder.OutputFormat.AAC_ADTS
} }
} }
MediaRecorder.AudioEncoder.OPUS -> { MediaRecorder.AudioEncoder.OPUS -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
MediaRecorder.OutputFormat.OGG MediaRecorder.OutputFormat.OGG
@ -174,6 +175,7 @@ data class AudioRecorderSettings(
MediaRecorder.OutputFormat.AAC_ADTS MediaRecorder.OutputFormat.AAC_ADTS
} }
} }
else -> MediaRecorder.OutputFormat.DEFAULT else -> MediaRecorder.OutputFormat.DEFAULT
} }
} }
@ -202,8 +204,7 @@ data class AudioRecorderSettings(
else -> 48000 else -> 48000
} }
fun getEncoder(): Int = encoder ?: fun getEncoder(): Int = encoder ?: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
MediaRecorder.AudioEncoder.AAC MediaRecorder.AudioEncoder.AAC
else else
MediaRecorder.AudioEncoder.AMR_NB MediaRecorder.AudioEncoder.AMR_NB
@ -221,7 +222,6 @@ data class AudioRecorderSettings(
} }
fun setBitRate(bitRate: Int): AudioRecorderSettings { fun setBitRate(bitRate: Int): AudioRecorderSettings {
println("bitRate: $bitRate")
if (bitRate !in 1000..320000) { if (bitRate !in 1000..320000) {
throw Exception("Bit rate must be between 1000 and 320000") throw Exception("Bit rate must be between 1000 and 320000")
} }

View File

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

View File

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

View File

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

View File

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