Skip to content
Draft
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions AudioRecorder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Other existing imports
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file.

import android.media.AudioManager;
import android.telecom.Call;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

// Function to detect connected Bluetooth devices
private AudioDevice getAudioSource() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
for (BluetoothDevice device : bluetoothAdapter.getBondedDevices()) {
if (device.getType() == BluetoothDevice.DEVICE_TYPE_AUDIO) {
// return audio source associated with the Bluetooth device
return AudioDevice.BLUETOOTH;
}
}
}
// Fallback to default audio source if no Bluetooth devices are connected
return AudioDevice.DEFAULT;
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This AudioRecorder.kt at the repository root is Java-like pseudo-code (semicolon imports, non-Kotlin function syntax, unknown types like AudioDevice) and will not compile in this Kotlin/Gradle project. It looks like an accidentally committed Copilot-generated snippet—please remove it from the PR (or convert it into valid Kotlin in the correct module/package if it’s actually needed).

Suggested change
import android.media.AudioManager;
import android.telecom.Call;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
// Function to detect connected Bluetooth devices
private AudioDevice getAudioSource() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
for (BluetoothDevice device : bluetoothAdapter.getBondedDevices()) {
if (device.getType() == BluetoothDevice.DEVICE_TYPE_AUDIO) {
// return audio source associated with the Bluetooth device
return AudioDevice.BLUETOOTH;
}
}
}
// Fallback to default audio source if no Bluetooth devices are connected
return AudioDevice.DEFAULT;
import android.media.AudioManager
import android.telecom.Call
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
private enum class AudioDevice {
BLUETOOTH,
DEFAULT,
}
// Function to detect connected Bluetooth devices
private fun getAudioSource(): AudioDevice {
val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
if (bluetoothAdapter?.isEnabled == true) {
val bondedDevices: Set<BluetoothDevice>? = bluetoothAdapter.bondedDevices
if (!bondedDevices.isNullOrEmpty()) {
// Return audio source associated with any bonded Bluetooth device
return AudioDevice.BLUETOOTH
}
}
// Fallback to default audio source if no Bluetooth devices are connected
return AudioDevice.DEFAULT

Copilot uses AI. Check for mistakes.
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class AudioRecorder(private val audioManager: AudioManager?) {
// Docs: only format '[g]uaranteed to be supported by devices'
private const val AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT

private const val AUDIO_SOURCE = AudioSource.MIC
private const val CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO
}

Expand All @@ -49,6 +48,26 @@ class AudioRecorder(private val audioManager: AudioManager?) {

private var focusRequest: AudioFocusRequestCompat? = null
private val focusListener = OnAudioFocusChangeListener { /* Not used */ }
private var scoStarted = false

/**
* Determine the appropriate audio source based on connected devices.
* Returns VOICE_COMMUNICATION when Bluetooth SCO is available off-call,
* otherwise returns MIC.
*/
private fun getAudioSource(): Int {
if (audioManager == null) {
return AudioSource.MIC
}

// Check if Bluetooth SCO is available
return if (audioManager.isBluetoothScoAvailableOffCall()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the property instead of the function

Suggested change
return if (audioManager.isBluetoothScoAvailableOffCall()) {
return if (audioManager.isBluetoothScoAvailableOffCall) {

// Use VOICE_COMMUNICATION which is the standard for Bluetooth audio
AudioSource.VOICE_COMMUNICATION
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not use that all the time, it might be beneficial to other scenario than only Bluetooth.

Microphone audio source tuned for voice communications such as VoIP. It will for instance take advantage of echo cancellation or automatic gain control if available.

} else {
AudioSource.MIC
}
}
Comment on lines +69 to +88
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The KDoc for getAudioSource() doesn't match the actual behavior: the function returns VOICE_COMMUNICATION only when Bluetooth SCO is available, otherwise MIC (there is no separate “fallback to VOICE_COMMUNICATION” path). Please update the documentation (or adjust the logic) so it accurately reflects what the code does.

Copilot uses AI. Check for mistakes.

/**
* Start the recorder. After calling this function, data will be available via [audioBytes].
Expand Down Expand Up @@ -101,8 +120,9 @@ class AudioRecorder(private val audioManager: AudioManager?) {
private fun setupRecorder() {
if (recorder != null) stopRecording()

val audioSource = getAudioSource()
val bufferSize = minBufferSize() * 10
recorder = AudioRecord(AUDIO_SOURCE, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, bufferSize)
recorder = AudioRecord(audioSource, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, bufferSize)
}

private fun releaseRecorder() {
Expand All @@ -114,6 +134,13 @@ class AudioRecorder(private val audioManager: AudioManager?) {

private fun requestFocus() {
if (audioManager == null) return

// Enable Bluetooth SCO if available
if (audioManager.isBluetoothScoAvailableOffCall()) {
audioManager.startBluetoothSco()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is deprecated and the documentation suggest to use setCommunicationDevice instead can you adjust and try again?

scoStarted = true
}

if (focusRequest == null) {
focusRequest = AudioFocusRequestCompat.Builder(AudioManagerCompat.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE).run {
setAudioAttributes(
Expand All @@ -138,7 +165,16 @@ class AudioRecorder(private val audioManager: AudioManager?) {
}

private fun abandonFocus() {
if (audioManager == null || focusRequest == null) return
AudioManagerCompat.abandonAudioFocusRequest(audioManager, focusRequest!!)
if (audioManager == null) return

// Disable Bluetooth SCO only if this instance started it
if (scoStarted) {
audioManager.stopBluetoothSco()
scoStarted = false
}

if (focusRequest != null) {
AudioManagerCompat.abandonAudioFocusRequest(audioManager, focusRequest!!)
}
}
}
Loading