From 862de214365a0ba8ad37fd23e026ebb1de43cb4e Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 21 Oct 2023 17:18:57 +0200 Subject: [PATCH] feat: Add PoC for sources for AudioRecorderService --- .../alibi/services/AudioRecorderService.kt | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/app/src/main/java/app/myzel394/alibi/services/AudioRecorderService.kt b/app/src/main/java/app/myzel394/alibi/services/AudioRecorderService.kt index 0d8617a..413fa91 100644 --- a/app/src/main/java/app/myzel394/alibi/services/AudioRecorderService.kt +++ b/app/src/main/java/app/myzel394/alibi/services/AudioRecorderService.kt @@ -1,12 +1,19 @@ package app.myzel394.alibi.services +import android.annotation.SuppressLint +import android.content.Context +import android.content.Intent +import android.media.AudioDeviceInfo +import android.media.AudioManager import android.media.MediaRecorder import android.media.MediaRecorder.OnErrorListener +import android.media.MediaRecorder.getAudioSourceMax import android.os.Build import java.lang.IllegalStateException class AudioRecorderService: IntervalRecorderService() { var amplitudesAmount = 1000 + var selectedDevice: AudioDeviceInfo? = null var recorder: MediaRecorder? = null private set @@ -15,12 +22,39 @@ class AudioRecorderService: IntervalRecorderService() { val filePath: String get() = "$folder/$counter.${settings!!.fileExtension}" + private fun _setAudioDevice() { + val audioManger = getSystemService(AUDIO_SERVICE)!! as AudioManager + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + if (selectedDevice == null) { + audioManger.clearCommunicationDevice() + } else { + audioManger.setCommunicationDevice(selectedDevice!!) + } + } else { + if (selectedDevice == null) { + audioManger.stopBluetoothSco() + } else { + audioManger.startBluetoothSco() + } + } + + } + private fun createRecorder(): MediaRecorder { + _setAudioDevice() + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { MediaRecorder(this) } else { MediaRecorder() }.apply { + // Audio Source is kinda strange, here are my experimental findings using a Pixel 7 Pro + // and Redmi Buds 3 Pro: + // - MIC: Uses the bottom microphone of the phone (17) + // - CAMCORDER: Uses the top microphone of the phone (2) + // - VOICE_COMMUNICATION: Uses the bottom microphone of the phone (17) + // - DEFAULT: Uses the bottom microphone of the phone (17) setAudioSource(MediaRecorder.AudioSource.MIC) setOutputFile(filePath) setOutputFormat(settings!!.outputFormat) @@ -82,4 +116,30 @@ class AudioRecorderService: IntervalRecorderService() { 0 } } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (intent?.action == "changeAudioDevice") { + selectedDevice = intent.getStringExtra("deviceID")!!.let { + if (it == "null") { + null + } else { + val audioManager = getSystemService(AUDIO_SERVICE)!! as AudioManager + audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS).find { device -> + device.id == it.toInt() + } + } + } + } + + return super.onStartCommand(intent, flags, startId) + } + + companion object { + fun changeAudioDevice(deviceID: String?, context: Context) { + val intent = Intent("changeAudioDevice").apply { + putExtra("deviceID", deviceID ?: "null") + } + context.startService(intent) + } + } } \ No newline at end of file