1e41f4b71Sopenharmony_ci# Using Distributed AVSession (for System Applications Only) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Basic Concepts 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci- Remote AVSession: an AVSession automatically created on the remote device by the AVSession service for synchronization with an AVSession on the local device. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci- Remote AVSessionController: AVSessionController automatically created on the remote device after projection. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Available APIs 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciThe table below describes the key APIs used for remote projection with the distributed AVSession. The APIs use either a callback or promise to return the result. The APIs listed below use a callback. They provide the same functions as their counterparts that use a promise. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciFor details, see [AVSession Management](../../reference/apis-avsession-kit/js-apis-avsession.md). 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci| API | Description | 16e41f4b71Sopenharmony_ci| -------- | -------- | 17e41f4b71Sopenharmony_ci| castAudio(session: SessionToken \| 'all', audioDevices: Array<audio.AudioDeviceDescriptor>, callback: AsyncCallback<void>): void | Casts a session to a list of devices. | 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci## How to Develop 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciTo enable a system application that accesses the AVSession service as the controller to use the distributed AVSession for projection, proceed as follows: 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci1. Import the modules. Before projection, you must obtain the AudioDeviceDescriptor from the audio module. Therefore, import the audio module and AVSessionManager module. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci ```ts 26e41f4b71Sopenharmony_ci import { avSession as AVSessionManager } from '@kit.AVSessionKit'; 27e41f4b71Sopenharmony_ci import { audio } from '@kit.AudioKit'; 28e41f4b71Sopenharmony_ci ``` 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci2. Use **castAudio** in the **AVSessionManager** class to project all sessions of the local device to another device. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci ```ts 33e41f4b71Sopenharmony_ci import { avSession as AVSessionManager } from '@kit.AVSessionKit'; 34e41f4b71Sopenharmony_ci import { audio } from '@kit.AudioKit'; 35e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci async function castAudio() { 38e41f4b71Sopenharmony_ci // Cast the sessions to another device. 39e41f4b71Sopenharmony_ci let audioManager = audio.getAudioManager(); 40e41f4b71Sopenharmony_ci let audioRoutingManager = audioManager.getRoutingManager(); 41e41f4b71Sopenharmony_ci let audioDevices: audio.AudioDeviceDescriptors | undefined = undefined; 42e41f4b71Sopenharmony_ci audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => { 43e41f4b71Sopenharmony_ci audioDevices = data; 44e41f4b71Sopenharmony_ci console.info(`Promise returned to indicate that the device list is obtained.`); 45e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 46e41f4b71Sopenharmony_ci console.error(`Failed to get devices. Code: ${err.code}, message: ${err.message}`); 47e41f4b71Sopenharmony_ci }); 48e41f4b71Sopenharmony_ci if (audioDevices !== undefined) { 49e41f4b71Sopenharmony_ci AVSessionManager.castAudio('all', audioDevices as audio.AudioDeviceDescriptors).then(() => { 50e41f4b71Sopenharmony_ci console.info(`createController : SUCCESS`); 51e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 52e41f4b71Sopenharmony_ci console.error(`Failed to cast audio. Code: ${err.code}, message: ${err.message}`); 53e41f4b71Sopenharmony_ci }); 54e41f4b71Sopenharmony_ci } 55e41f4b71Sopenharmony_ci } 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci ``` 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ciAfter the system application on the local service initiates projection to a remote device, the AVSession framework instructs the AVSession service of the remote device to create a remote AVSession. When the AVSession on the local device changes (for example, the media information or playback state changes), the AVSession framework automatically synchronizes the change to the remote device. 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ciThe AVSession processing mechanism on the remote device is consistent with that on the local device. That is, the controller (for example, the Media Controller) on the remote device listens for the AVSession creation event, and creates a remote **AVSessionController** object to manage the remote AVSession. In addition, the control commands are automatically synchronized by the AVSession framework to the local device. 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ciThe provider (for example, an audio and video application) on the local device listens for control command events, so as to respond to the commands from the remote device in time. 64