harmony 鸿蒙Using Distributed AVSession

  • 2023-06-24
  • 浏览 (604)

Using Distributed AVSession

Basic Concepts

  • Remote AVSession: an AVSession automatically created on the remote device by the AVSession service for synchronization with an AVSession on the local device.

  • Remote AVSessionController: AVSessionController automatically created on the remote device after projection.

Available APIs

The 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.

For details, see AVSession Management.

API Description
castAudio(session: SessionToken |‘all’, audioDevices: Array<audio.AudioDeviceDescriptor>, callback: AsyncCallback<void>): void Casts a session to a list of devices.

How to Develop

To enable a system application that accesses the AVSession service as the controller to use the distributed AVSession for projection, proceed as follows:

  1. Import the modules. Before projection, you must obtain the AudioDeviceDescriptor from the audio module. Therefore, import the audio module and AVSessionManager module.
   import AVSessionManager from '@ohos.multimedia.avsession';
   import audio from '@ohos.multimedia.audio';
  1. Use castAudio in the AVSessionManager class to project all sessions of the local device to another device.
   import AVSessionManager from '@ohos.multimedia.avsession';
   import audio from '@ohos.multimedia.audio';
   import { BusinessError } from '@ohos.base';
   
   async function castAudio() {
     // Cast the sessions to another device.
     let audioManager = audio.getAudioManager();
     let audioRoutingManager = audioManager.getRoutingManager();
     let audioDevices: audio.AudioDeviceDescriptors|undefined = undefined;
     audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => {
       audioDevices = data;
       console.info(`Promise returned to indicate that the device list is obtained.`);
     }).catch((err: BusinessError) => {
       console.error(`Failed to get devices. Code: ${err.code}, message: ${err.message}`);
     });
     if (audioDevices !== undefined) {
       AVSessionManager.castAudio('all', audioDevices as audio.AudioDeviceDescriptors).then(() => {
         console.info(`createController : SUCCESS`);
       }).catch((err: BusinessError) => {
         console.error(`Failed to cast audio. Code: ${err.code}, message: ${err.message}`);
       });
     }
   }
   

After 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.

The 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.

The 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.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Media

harmony 鸿蒙Developing Audio Call

harmony 鸿蒙Audio Call Development

harmony 鸿蒙Audio Decoding

harmony 鸿蒙Audio Effect Management

harmony 鸿蒙Audio Encoding

harmony 鸿蒙Audio Input Device Management

harmony 鸿蒙Audio Output Device Management

harmony 鸿蒙Audio Playback Concurrency Policy

harmony 鸿蒙Audio Playback Development

0  赞