Starting from API version 18, you can query the spatial audio capabilities and subscribe to their status changes.
Spatial audio is an advanced audio technology that transforms traditional stereo sound into a three-dimensional experience. It enhances monaural, stereo, and surround sound by adding a sense of height, delivering an all-encompassing audio experience. Spatial audio immerses users in an interactive, spatially aware soundscape, making them feel as though they are truly present in the audio environment.
Audio Vivid is an AI-driven audio codec standard. It is jointly formulated and released by the UHD World Association (UWA) and the Audio and Video Coding Standard Working Group (AVS). This audio format contains audio Pulse-Code Modulation (PCM) data and metadata, offering a more immersive listening experience over traditional stereo sources. Audio Vivid contains metadata information of audio content sources, which can reproduce the true auditory sensations of the physical and perceptual world.
Spatial audio supports playback of multi-channel, stereo, and Audio Vivid formats, and can render binaural spatial audio through headphones. When paired with Audio Vivid format sources, spatial audio rendering can treat vocals and various instruments in music as independent sound objects. It redefines the position, movement trajectory, and volume of these sound objects, creating a fully immersive experience where sound surrounds and envelops the listener from all directions, including above. This provides a more immersive spatial audio experience, akin to being in a cinema or concert hall. When a device supports spatial audio and the spatial audio feature is enabled, playing Audio Vivid format sources can enhance the audio experience. For details about how to play Audio Vivid sources, see Playing Audio Files in Audio Vivid Format.
The following examples are code snippets. For the complete sample, click the link at the bottom right of the example.
For audio playback apps, you can check whether the device supports spatial audio and the current spatial audio toggle state of the device.
Before using any APIs of AudioSpatializationManager, you must call getSpatializationManager to obtain an AudioSpatializationManager instance.
import { audio } from '@kit.AudioKit';
// ...
let audioManager = audio.getAudioManager();
// ...
let audioSpatializationManager = audioManager.getSpatializationManager(); Use the spatializationSupported property in AudioDeviceDescriptor to check whether a specified device supports spatial audio rendering. You need to use other audio APIs to obtain AudioDeviceDescriptor of a connected device or the current audio device. For example, you can use getDevicesSync of the AudioRoutingManager instance created by getRoutingManager to obtain AudioDeviceDescriptor.
import { audio } from '@kit.AudioKit';
// ...
let audioRoutingManager = audioManager.getRoutingManager();
// ...
let deviceDescriptors = audioRoutingManager.getDevicesSync(audio.DeviceFlag.OUTPUT_DEVICES_FLAG);
console.info(`Succeeded in getting devices, AudioDeviceDescriptors: ${JSON.stringify(deviceDescriptors)}.`); Call isSpatializationEnabledForCurrentDevice to check whether spatial audio rendering is enabled for the current device.
import { audio } from '@kit.AudioKit';
// ...
let isSpatializationEnabledForCurrentDevice = audioSpatializationManager.isSpatializationEnabledForCurrentDevice();
console.info(`Succeeded in checking spatialization enabled for current device is: ${isSpatializationEnabledForCurrentDevice}.`); Call on('spatializationEnabledChangeForCurrentDevice') to subscribe to the spatial audio rendering status change event of the current device.
If true is returned, spatial audio rendering is enabled. If false is returned, it is disabled.
import { audio } from '@kit.AudioKit';
// ...
audioSpatializationManager.on('spatializationEnabledChangeForCurrentDevice',
(isSpatializationEnabledForCurrentDevice: boolean) => {
console.info(`Succeeded in using isSpatializationEnabledForCurrentDevice: ${isSpatializationEnabledForCurrentDevice}.`);
// ...
}); Call off('spatializationEnabledChangeForCurrentDevice') to unsubscribe from the spatial audio rendering status change event of the current device.
import { audio } from '@kit.AudioKit';
// ...
audioSpatializationManager.off('spatializationEnabledChangeForCurrentDevice');