1# Responding to Audio Output Device Changes
2
3To ensure a seamless user experience when there are changes in the audio output device, it is crucial to listen for such changes and adapt accordingly.
4
5You can use [outputDeviceChangeWithInfo](../../reference/apis-audio-kit/js-apis-audio.md#onoutputdevicechangewithinfo11) of the AudioRenderer to listen for audio output device changes and obtain the change reason. When the audio output device is changed due to the online/offline status change, forcible user selection, device preemption, or device selection policy change, the system uses this API to notify the application of the change, including the audio output device information and change reason.
6
7## Audio Output Device Information
8
9[outputDeviceChangeWithInfo](../../reference/apis-audio-kit/js-apis-audio.md#onoutputdevicechangewithinfo11) contains the information about the new audio output device, in the form of an array. Generally, the array contains information about only one device. For details, see [AudioDeviceDescriptors](../../reference/apis-audio-kit/js-apis-audio.md#audiodevicedescriptors).
10
11## Device Change Reason
12
13The system sends [AudioStreamDeviceChangeReason](../../reference/apis-audio-kit/js-apis-audio.md#audiostreamdevicechangereason11) to the application in any of the following cases:
14
15- **REASON_NEW_DEVICE_AVAILABLE**: A new device is available.
16
17  **Trigger conditions**:
18
19  Typical Bluetooth devices (such as headsets, smart glasses, speakers, and telematics devices) are connected; Bluetooth devices that support wear detection (such as headsets and smart glasses) are put on; wired devices (such as 3.5mm headsets, Type-C headsets, USB headsets, and USB speakers) are plugged in; distributed devices are brought online.
20
21- **REASON_OLD_DEVICE_UNAVAILABLE**: The old device is unavailable.
22
23  When this reason is reported, the application should consider pausing audio playback.
24
25  **Trigger conditions**:
26
27  Typical Bluetooth devices (such as headsets, smart glasses, speakers, and telematics devices) are disconnected; Bluetooth devices that support wear detection (such as headsets and smart glasses) are taken off; wired devices (such as 3.5mm headsets, Type-C headsets, USB headsets, and speakers) are unplugged; distributed devices are brought offline.
28
29  The handling suggestions for typical service scenarios are as follows:
30
31  - Gaming scenario: Do not pause audio playback.
32  - Audiobook scenario: Pause audio playback.
33  - Music scenario: Pause audio playback.
34  - Video scenario: Pause audio playback.
35
36- **REASON_OVERRODE**: The user forcibly selects a device.
37
38  **Trigger conditions**:
39
40  The user selects another audio output device from the UI, and selects to answer a cellular or VoIP call from the peripheral.
41
42- **REASON_UNKNOWN**: Unknown reason.
43
44## Example
45
46  ```ts
47  import router from '@ohos.router';
48  import { audio } from '@kit.AudioKit';
49  import { BusinessError } from '@kit.BasicServicesKit';
50
51  let audioRenderer: audio.AudioRenderer | undefined = undefined;
52  let audioStreamInfo: audio.AudioStreamInfo = {
53    samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_48000, // Sampling rate.
54    channels: audio.AudioChannel.CHANNEL_2, // Channel.
55    sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // Sampling format.
56    encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // Encoding format.
57  };
58  let audioRendererInfo: audio.AudioRendererInfo = {
59    usage: audio.StreamUsage.STREAM_USAGE_MUSIC, // Audio stream usage type.
60    rendererFlags: 0 // AudioRenderer flag.
61  };
62  let audioRendererOptions: audio.AudioRendererOptions = {
63    streamInfo: audioStreamInfo,
64    rendererInfo: audioRendererInfo
65  };
66
67  // Create an AudioRenderer instance.
68  audio.createAudioRenderer(audioRendererOptions).then((data) => {
69    audioRenderer = data;
70    console.info('AudioFrameworkRenderLog: AudioRenderer Created : Success : Stream Type: SUCCESS');
71  }).catch((err: BusinessError) => {
72    console.error(`AudioFrameworkRenderLog: AudioRenderer Created : ERROR : ${err}`);
73  });
74
75  if (audioRenderer) {
76    // Subscribe to audio output device changes, carrying the change reason.
77    (audioRenderer as audio.AudioRenderer).on('outputDeviceChangeWithInfo', async (deviceChangeInfo: audio.AudioStreamDeviceChangeInfo) => {
78      switch (deviceChangeInfo.changeReason) {
79        case audio.AudioStreamDeviceChangeReason.REASON_OLD_DEVICE_UNAVAILABLE:
80          // Respond to the device unavailability event. If the application is playing content, pause the playback and update the UX.
81          // await audioRenderer.pause();
82          break;
83        case audio.AudioStreamDeviceChangeReason.REASON_NEW_DEVICE_AVAILABLE:
84          // The application responds to the device availability event based on the service status.
85          break;
86        case audio.AudioStreamDeviceChangeReason.REASON_OVERRODE:
87          // The application responds to the forcible device selection event based on the service status.
88          break;
89        case audio.AudioStreamDeviceChangeReason.REASON_UNKNOWN:
90          // The application responds to the unknown reason event based on the service status.
91          break;
92      }
93    });
94  }
95  ```
96