Switching Audio Input Devices

Starting from API version 21, support for switching audio input device routes is available.

When an application performs audio input, the system selects the corresponding input device based on the audio stream type. (If the audio stream type is SOURCE_TYPE_MIC, the built-in microphone is used for recording. If the audio stream type is SOURCE_TYPE_VOICE_COMMUNICATION, the input device follows the current output device.) If the default input device does not meet the application requirements, the application can call setBluetoothAndNearlinkPreferredRecordCategory or selectMediaInputDevice to switch the audio input device.

The examples in each of the following steps are code snippets. You can click the link at the bottom right of the sample code to obtain the complete sample codes.

Applications can use setBluetoothAndNearlinkPreferredRecordCategory of AudioSessionManager to express a preference for Bluetooth or NearLink devices when they become available.

NOTE

In call scenarios, if a Bluetooth or NearLink device is online, the system uses the Bluetooth or NearLink device as the input device by default.

import { audio } from '@kit.AudioKit';  // Import the audio module.
import { BusinessError } from '@kit.BasicServicesKit';

let audioManager = audio.getAudioManager();  // Create an AudioManager instance.

let audioSessionManager = audioManager.getSessionManager();  // Call an API of AudioManager to create an AudioSessionManager instance.

// ...
  audioSessionManager.setBluetoothAndNearlinkPreferredRecordCategory(audio.BluetoothAndNearlinkPreferredRecordCategory
    .PREFERRED_LOW_LATENCY).then(() => {
    console.info('Succeeded in setting bluetooth and nearlink preferred record category.');
    // ...
  }).catch((err: BusinessError) => {
    console.error(`Failed to set bluetooth and nearlink preferred record category. Code: ${err.code},
      message: ${err.message}`);
    // ...
  });

Manually Selecting Input Devices

Applications can use selectMediaInputDevice of AudioSessionManager to select an input device.

NOTE

In call scenarios, the input device follows the current output device, and other concurrent recording streams also follows the call input device.

import { audio } from '@kit.AudioKit';  // Import the audio module.
import { BusinessError } from '@kit.BasicServicesKit';

let audioManager = audio.getAudioManager();  // Create an AudioManager instance.

let audioSessionManager = audioManager.getSessionManager();  // Call an API of AudioManager to create an AudioSessionManager instance.

// ...
// When an input device goes online or offline, a callback notification is received. Listen for changes in the connection status of available audio input devices.
let availableDeviceChangeCallback = (deviceChanged: audio.DeviceChangeAction) => {
  let data: audio.AudioDeviceDescriptors = deviceChanged.deviceDescriptors;
  console.info(`Succeeded in using on or off function, AudioDeviceDescriptors: ${data}.`);
  // ...
};

// Listen for changes in the current input device. The callback is triggered when an input device is selected.
let currentInputDeviceChangedCallback = (currentInputDeviceChangedEvent: audio.CurrentInputDeviceChangedEvent) => {
  console.info(`Succeeded in using on or off function, CurrentInputDeviceChangedEvent:
   ${currentInputDeviceChangedEvent}.`);
  // ...
};

// ...
  audioSessionManager.on('availableDeviceChange', audio.DeviceUsage.MEDIA_INPUT_DEVICES, availableDeviceChangeCallback);
  // ...
  audioSessionManager.on('currentInputDeviceChanged', currentInputDeviceChangedCallback);
  // ...
  // Stop listening for changes in the connection status of available audio input devices.
  audioSessionManager.off('availableDeviceChange', availableDeviceChangeCallback);
  // ...
  // Stop listening for changes in the current input device.
  audioSessionManager.off('currentInputDeviceChanged', currentInputDeviceChangedCallback);
  // ...
  try {
    // Obtain the list of currently available audio input devices.
    let data: audio.AudioDeviceDescriptors =
      audioSessionManager.getAvailableDevices(audio.DeviceUsage.MEDIA_INPUT_DEVICES);
    console.info(`Succeeded in getting available devices, AudioDeviceDescriptors: ${data}.`);

    // ...

    // If the list of currently available audio input devices is not empty, you can make a selection.
    if (data[0]) {
      // Select an input device.
      await audioSessionManager.selectMediaInputDevice(data[0]).then(() => {
        console.info('Succeeded in selecting media input device.');
        // ...
      }).catch((err: BusinessError) => {
        console.error(`Failed to select media input device. Code: ${err.code}, message: ${err.message}`);
        // ...
      });
    }
  } catch (err) {
    let error = err as BusinessError;
    console.error(`Failed to select media input device. Code: ${err.code}, message: ${err.message}`);
    // ...
  }
  // ...
  // Check whether the input device selection is successful.
  try {
    let device: audio.AudioDeviceDescriptor = audioSessionManager.getSelectedMediaInputDevice();
    console.info(`Succeeded in getting selected media input device: ${JSON.stringify(device)}`);

    // ...
  } catch (err) {
    let error = err as BusinessError;
    console.error(`Failed to get selected media input device. Code: ${error.code}, message: ${error.message}`);
    // ...
  }
  // ...
  // Clear the input device selected via selectMediaInputDevice.
  audioSessionManager.clearSelectedMediaInputDevice().then(() => {
    console.info('Succeeded in clearing selected media input device.');
    // ...
  }).catch((err: BusinessError) => {
    console.error(`Failed to clear selected media input device. Code: ${err.code}, message: ${err.message}`);
    // ...
  });