Skip to content

Commit 40f9f63

Browse files
jfversluisCopilot
andcommitted
Enable preferred audio input device selection on Android
Add PreferredDevice property to BaseOptions for Android, allowing consumers to select a specific audio input device (e.g. Bluetooth headset, USB microphone) via AudioDeviceInfo. Changes: - Add BaseOptions.android.cs with PreferredDevice property typed as AudioDeviceInfo from Android.Media - Apply SetPreferredDevice on both AudioRecord and MediaRecorder after creation, before starting recording - Guarded with OperatingSystem.IsAndroidVersionAtLeast(23) since the API requires Android Marshmallow (API 23+) Usage: var audioManager = (AudioManager)context.GetSystemService(Context.AudioService); var btDevice = audioManager.GetDevices(GetDevicesTargets.Inputs) .FirstOrDefault(d => d.Type == AudioDeviceType.BluetoothSco); var recorder = audioManager.CreateRecorder(new AudioRecorderOptions { PreferredDevice = btDevice }); Companion to #208 which adds the equivalent iOS/macCatalyst support. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1dd3d29 commit 40f9f63

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/Plugin.Maui.Audio/AudioRecorder/AudioRecorder.android.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ public Task StartAsync(string filePath, AudioRecorderOptions? recordingOptions =
107107
}
108108

109109
audioRecord = new AudioRecord(AudioSource.Mic, sampleRate, channelIn, encoding, bufferSize);
110+
111+
if (OperatingSystem.IsAndroidVersionAtLeast(23) && audioRecorderOptions.PreferredDevice is not null)
112+
{
113+
audioRecord.SetPreferredDevice(audioRecorderOptions.PreferredDevice);
114+
}
115+
110116
audioRecord.StartRecording();
111117
Task.Run(WriteAudioDataToFile);
112118
}
@@ -137,6 +143,12 @@ public Task StartAsync(string filePath, AudioRecorderOptions? recordingOptions =
137143
mediaRecorder.SetAudioEncodingBitRate(bitRate);
138144
mediaRecorder.SetOutputFile(audioFilePath);
139145
mediaRecorder.Prepare();
146+
147+
if (OperatingSystem.IsAndroidVersionAtLeast(23) && audioRecorderOptions.PreferredDevice is not null)
148+
{
149+
mediaRecorder.SetPreferredDevice(audioRecorderOptions.PreferredDevice);
150+
}
151+
140152
mediaRecorder.Start();
141153

142154
// Set MediaRecorder "is recording" flag true
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Android.Media;
2+
3+
namespace Plugin.Maui.Audio;
4+
5+
partial class BaseOptions
6+
{
7+
/// <summary>
8+
/// Gets or sets the preferred audio input device for recording.
9+
/// <para>
10+
/// Set this to an <see cref="AudioDeviceInfo"/> obtained from
11+
/// <see cref="AudioManager.GetDevices(GetDevicesTargets)"/> with
12+
/// <see cref="GetDevicesTargets.Inputs"/> to record from a specific device
13+
/// (e.g. a Bluetooth headset or USB microphone). When <see langword="null"/>,
14+
/// the system default input device is used.
15+
/// </para>
16+
/// <para>
17+
/// Requires Android API 23 (Marshmallow) or higher. On older API levels
18+
/// this property is ignored.
19+
/// </para>
20+
/// </summary>
21+
public AudioDeviceInfo? PreferredDevice { get; set; }
22+
}

0 commit comments

Comments
 (0)