openharmony 鸿蒙 arkts-apis-avsession-AVSession

2026-08-25 浏览 (1)

Interface (AVSession)

NOTE

  • The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The initial APIs of this interface are supported since API version 10.

An AVSession object is created by calling avSession.createAVSession. The object enables you to obtain the session ID and set the metadata and playback state.

Modules to Import

import { avSession } from '@kit.AVSessionKit';

Properties

System capability: SystemCapability.Multimedia.AVSession.Core

NameTypeRead-OnlyOptionalDescription
sessionId10+stringYesNoUnique session ID of the AVSession object.
Atomic service API: This API can be used in atomic services since API version 12.
sessionType10+AVSessionTypeYesNoAVSession type.
Atomic service API: This API can be used in atomic services since API version 12.
sessionTag22+stringYesNoCustom tag information of the AVSession.
Atomic service API: This API can be used in atomic services since API version 22.

Example

let sessionId: string = currentAVSession.sessionId;
let sessionType: avSession.AVSessionType = currentAVSession.sessionType;

setAVMetadata10+

setAVMetadata(data: AVMetadata): Promise<void>

Sets session metadata. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
dataAVMetadataYesSession metadata.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let metadata: avSession.AVMetadata = {
  assetId: "121278",
  title: "lose yourself",
  artist: "Eminem",
  author: "ST",
  album: "Slim shady",
  writer: "ST",
  composer: "ST",
  duration: 2222,
  mediaImage: "https://www.example.com/example.jpg",
  subtitle: "8 Mile",
  description: "Rap",
  // The LRC contains two types of elements: time tag + lyrics, and ID tag.
  // Example: [00:25.44]xxx\r\n[00:26.44]xxx\r\n
  lyric: "Lyrics in LRC format",
  // The singleLyricText field stores a single line of lyric text without timestamps.
  // Example: "Content of a single lyric line"
  singleLyricText: "Content of a single lyric line",
  previousAssetId: "121277",
  nextAssetId: "121279"
};
currentAVSession.setAVMetadata(metadata).then(() => {
  console.info('SetAVMetadata successfully');
}).catch((err: BusinessError) => {
  console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`);
});

setAVMetadata10+

setAVMetadata(data: AVMetadata, callback: AsyncCallback<void>): void

Sets session metadata. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
dataAVMetadataYesSession metadata.
callbackAsyncCallback<void>YesCallback used to return the result. If the setting is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let metadata: avSession.AVMetadata = {
  assetId: "121278",
  title: "lose yourself",
  artist: "Eminem",
  author: "ST",
  album: "Slim shady",
  writer: "ST",
  composer: "ST",
  duration: 2222,
  mediaImage: "https://www.example.com/example.jpg",
  subtitle: "8 Mile",
  description: "Rap",
  // The LRC contains two types of elements: time tag + lyrics, and ID tag.
  // Example: [00:25.44]xxx\r\n[00:26.44]xxx\r\n
  lyric: "Lyrics in LRC format",
  // The singleLyricText field stores a single line of lyric text without timestamps.
  // Example: "Content of a single lyric line"
  singleLyricText: "Content of a single lyric line",
  previousAssetId: "121277",
  nextAssetId: "121279"
};
currentAVSession.setAVMetadata(metadata, (err: BusinessError) => {
  if (err) {
    console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`);
  } else {
    console.info('SetAVMetadata successfully');
  }
});

setCallMetadata11+

setCallMetadata(data: CallMetadata): Promise<void>

Sets call metadata. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
dataCallMetadataYesCall metadata.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { image } from '@kit.ImageKit';
import { resourceManager } from '@kit.LocalizationKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { avSession } from '@kit.AVSessionKit';

@Entry
@Component
struct Index {
  build() {
    Column() {
      Text('Hello World')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
    }
    .width('100%')
    .height('100%')
  }
}

class CallManager {
  private currentAVSession: avSession.AVSession|null = null;

  async setCallMetadata() {
    try {
      let value = await resourceManager.getSysResourceManager().getRawFileContent('IMAGE_URI');
      let imageSource = await image.createImageSource(value.buffer);
      let imagePixel = await imageSource.createPixelMap({ desiredSize: { width: 150, height: 150 } });
      let calldata: avSession.CallMetadata = {
        name: "xiaoming",
        phoneNumber: "111xxxxxxxx",
        avatar: imagePixel
      };
      await this.currentAVSession?.setCallMetadata(calldata);
      console.info('setCallMetadata successfully');
    } catch (err) {
      if (err) {
        console.error('setCallMetadata BusinessError: code: ${err.code}, message: ${err.message}');
      } else {
        console.error('setCallMetadata Error: ${err}')
      }
    }
  }
}

setCallMetadata11+

setCallMetadata(data: CallMetadata, callback: AsyncCallback<void>): void

Sets call metadata. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
dataCallMetadataYesCall metadata.
callbackAsyncCallback<void>YesCallback used to return the result. If the setting is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { image } from '@kit.ImageKit';
import { resourceManager } from '@kit.LocalizationKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { avSession } from '@kit.AVSessionKit';

@Entry
@Component
struct Index {
  build() {
    Column() {
      Text('Hello World')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
    }
    .width('100%')
    .height('100%')
  }
}

class CallManager {
  private currentAVSession: avSession.AVSession|null = null;

  async setCallMetadata() {
    try {
      let value = await resourceManager.getSysResourceManager().getRawFileContent('IMAGE_URI');
      let imageSource = await image.createImageSource(value.buffer);
      let imagePixel = await imageSource.createPixelMap({ desiredSize: { width: 150, height: 150 } });
      let calldata: avSession.CallMetadata = {
        name: "xiaoming",
        phoneNumber: "111xxxxxxxx",
        avatar: imagePixel
      };
      this.currentAVSession?.setCallMetadata(calldata, (err: BusinessError) => {
        if (err) {
          console.error('setCallMetadata BusinessError: code: ${err.code}, message: ${err.message}');
        } else {
          console.info("setCallMetadata successfully");
        }
      });
    }catch (syncErr) {
      console.error('Syncronous operation failed: ${syncErr}');
    }
  }
}

setAVCallState11+

setAVCallState(state: AVCallState): Promise<void>

Sets the call state. This API uses a promise to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
stateAVCallStateYesCall state.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let calldata: avSession.AVCallState = {
  state: avSession.CallState.CALL_STATE_ACTIVE,
  muted: false
};
currentAVSession.setAVCallState(calldata).then(() => {
  console.info('setAVCallState successfully');
}).catch((err: BusinessError) => {
  console.error(`setAVCallState BusinessError: code: ${err.code}, message: ${err.message}`);
});

setAVCallState11+

setAVCallState(state: AVCallState, callback: AsyncCallback<void>): void

Sets the call state. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
stateAVCallStateYesCall state.
callbackAsyncCallback<void>YesCallback used to return the result. If the setting is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let avcalldata: avSession.AVCallState = {
  state: avSession.CallState.CALL_STATE_ACTIVE,
  muted: false
};
currentAVSession.setAVCallState(avcalldata, (err: BusinessError) => {
  if (err) {
    console.error(`setAVCallState BusinessError: code: ${err.code}, message: ${err.message}`);
  } else {
    console.info('setAVCallState successfully');
  }
});

setAVPlaybackState10+

setAVPlaybackState(state: AVPlaybackState): Promise<void>

Sets information related to the session playback state. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
stateAVPlaybackStateYesInformation related to the session playback state.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let playbackState: avSession.AVPlaybackState = {
  state:avSession.PlaybackState.PLAYBACK_STATE_PLAY,
  speed: 1.0,
  position:{elapsedTime:10, updateTime:(new Date()).getTime()},
  bufferedTime:1000,
  loopMode:avSession.LoopMode.LOOP_MODE_SINGLE,
  isFavorite:true
};
currentAVSession.setAVPlaybackState(playbackState).then(() => {
  console.info('SetAVPlaybackState successfully');
}).catch((err: BusinessError) => {
  console.error(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`);
});

setAVPlaybackState10+

setAVPlaybackState(state: AVPlaybackState, callback: AsyncCallback<void>): void

Sets information related to the session playback state. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
stateAVPlaybackStateYesInformation related to the session playback state.
callbackAsyncCallback<void>YesCallback used to return the result. If the setting is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let PlaybackState: avSession.AVPlaybackState = {
  state:avSession.PlaybackState.PLAYBACK_STATE_PLAY,
  speed: 1.0,
  position:{elapsedTime:10, updateTime:(new Date()).getTime()},
  bufferedTime:1000,
  loopMode:avSession.LoopMode.LOOP_MODE_SINGLE,
  isFavorite:true
};
currentAVSession.setAVPlaybackState(PlaybackState, (err: BusinessError) => {
  if (err) {
    console.error(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`);
  } else {
    console.info('SetAVPlaybackState successfully');
  }
});

setLaunchAbility10+

setLaunchAbility(ability: WantAgent): Promise<void>

Sets a launcher ability. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
abilityWantAgentYesApplication properties, such as the bundle name, ability name, and deviceID.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { wantAgent } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

// WantAgentInfo object.
let wantAgentInfo: wantAgent.WantAgentInfo = {
  wants: [
    {
      deviceId: "deviceId",
      bundleName: "com.example.myapplication",
      abilityName: "EntryAbility",
      action: "action1",
      entities: ["entity1"],
      type: "MIMETYPE",
      uri: "key = {true,true,false}",
      parameters:
        {
          mykey0: 2222,
          mykey1: [1, 2, 3],
          mykey2: "[1, 2, 3]",
          mykey3: "ssssssssssssssssssssssssss",
          mykey4: [false, true, false],
          mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"],
          mykey6: true
        }
    }
  ],
  operationType: wantAgent.OperationType.START_ABILITIES,
  requestCode: 0,
  wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
}

wantAgent.getWantAgent(wantAgentInfo).then((agent) => {
  currentAVSession.setLaunchAbility(agent).then(() => {
    console.info('SetLaunchAbility successfully');
  }).catch((err: BusinessError) => {
    console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`);
  });
});

setLaunchAbility10+

setLaunchAbility(ability: WantAgent, callback: AsyncCallback<void>): void

Sets a launcher ability. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
abilityWantAgentYesApplication properties, such as the bundle name, ability name, and deviceID.
callbackAsyncCallback<void>YesCallback used to return the result. If the setting is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { wantAgent } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

// WantAgentInfo object.
let wantAgentInfo: wantAgent.WantAgentInfo = {
  wants: [
    {
      deviceId: "deviceId",
      bundleName: "com.example.myapplication",
      abilityName: "EntryAbility",
      action: "action1",
      entities: ["entity1"],
      type: "MIMETYPE",
      uri: "key = {true,true,false}",
      parameters:
        {
          mykey0: 2222,
          mykey1: [1, 2, 3],
          mykey2: "[1, 2, 3]",
          mykey3: "ssssssssssssssssssssssssss",
          mykey4: [false, true, false],
          mykey5: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"],
          mykey6: true
        }
    }
  ],
  operationType: wantAgent.OperationType.START_ABILITIES,
  requestCode: 0,
  wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
}

wantAgent.getWantAgent(wantAgentInfo).then((agent) => {
  currentAVSession.setLaunchAbility(agent, (err: BusinessError) => {
    if (err) {
      console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`);
    } else {
      console.info('SetLaunchAbility successfully');
    }
  });
});

dispatchSessionEvent10+

dispatchSessionEvent(event: string, args: Record<string, Object>): Promise<void>

Dispatches a custom event in the session, including the event name and event content in key-value pair format. This API uses a promise to return the result. It is called by the provider.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
eventstringYesName of the session event.
argsRecord<string, Object>YesContent of the session event.
Starting from API version 20, a compatibility change occurred. In API version 19 and earlier, the parameter type is {[key: string]: Object}.

NOTE The args parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see @ohos.app.ability.Want(Want).

Return value

TypeDescription
Promise<void>Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { avSession } from '@kit.AVSessionKit';

@Entry
@Component
struct Index {
  @State message: string = 'hello world';

  build() { 
    Column() {
        Text(this.message)
          .onClick(()=>{
            let currentAVSession: avSession.AVSession|undefined = undefined;
            let tag = "createNewSession";
            let context: Context = this.getUIContext().getHostContext() as Context;

            avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => {
              if (err) {
                console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`);
              } else {
                currentAVSession = data;
                let eventName = "dynamic_lyric";
                if (currentAVSession !== undefined) {
                  (currentAVSession as avSession.AVSession).dispatchSessionEvent(eventName, {lyric : "This is lyric"}).then(() => {
                    console.info('dispatchSessionEvent successfully');
                  }).catch((err: BusinessError) => {
                    console.error(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`);
                  })
                }
              }
            });
          })
      }
    .width('100%')
    .height('100%')
  }
}

dispatchSessionEvent10+

dispatchSessionEvent(event: string, args: Record<string, Object>, callback: AsyncCallback<void>): void

Dispatches a custom event in the session, including the event name and event content in key-value pair format. This API uses an asynchronous callback to return the result. It is called by the provider.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
eventstringYesName of the session event.
argsRecord<string, Object>YesContent of the session event.
Starting from API version 20, a compatibility change occurred. In API version 19 and earlier, the parameter type is {[key: string]: Object}.
callbackAsyncCallback<void>YesCallback used to return the result. If the setting is successful, err is undefined; otherwise, err is an error object.

NOTE

The args parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see @ohos.app.ability.Want(Want).

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { avSession } from '@kit.AVSessionKit';
@Entry
@Component
struct Index {
  @State message: string = 'hello world';

  build() { 
    Column() {
        Text(this.message)
          .onClick(()=>{
            let currentAVSession: avSession.AVSession|undefined = undefined;
            let tag = "createNewSession";
            let context: Context = this.getUIContext().getHostContext() as Context;

            avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => {
              if (err) {
                console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`);
              } else {
                currentAVSession = data;
                let eventName: string = "dynamic_lyric";
                if (currentAVSession !== undefined) {
                  (currentAVSession as avSession.AVSession).dispatchSessionEvent(eventName, {lyric : "This is lyric"}, (err: BusinessError) => {
                    if (err) {
                      console.error(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`);
                    }
                  })
                }
              }
            });
          })
      }
    .width('100%')
    .height('100%')
  }
}

setAVQueueItems10+

setAVQueueItems(items: Array<AVQueueItem>): Promise<void>

Sets a playlist. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
itemsArray<AVQueueItem>YesPlaylist to set.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { image } from '@kit.ImageKit';
import { resourceManager } from '@kit.LocalizationKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { avSession } from '@kit.AVSessionKit';
interface ExtrasType {
  extras: string;
}

@Entry
@Component
struct Index {
  build() {
    Column() {
    }
  }
}

let currentAVSession: avSession.AVSession;

async function setAVQueueItems() {
  try {
    let value = await resourceManager.getSysResourceManager().getRawFileContent('IMAGE_URI');
    let imageSource = await image.createImageSource(value.buffer);
    let imagePixel = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}});
    let queueItemDescription_1: avSession.AVMediaDescription = {
      assetId: '001',
      title: 'music_name',
      subtitle: 'music_sub_name',
      description: 'music_description',
      mediaImage : imagePixel,
      extras: {extras:'any'}
    };
    let queueItem_1: avSession.AVQueueItem = {
      itemId: 1,
      description: queueItemDescription_1
    } as avSession.AVQueueItem;
    let queueItemDescription_2: avSession.AVMediaDescription = {
      assetId: '002',
      title: 'music_name',
      subtitle: 'music_sub_name',
      description: 'music_description',
      mediaImage: imagePixel,
      extras: {extras:'any'}
    };
    let queueItem_2: avSession.AVQueueItem = {
      itemId: 2,
      description: queueItemDescription_2
    } as avSession.AVQueueItem;
    let queueItemsArray: avSession.AVQueueItem[] = [queueItem_1, queueItem_2];
    currentAVSession.setAVQueueItems(queueItemsArray).then(() => {
      console.info('SetAVQueueItems successfully');
    }).catch((err: BusinessError) => {
      console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`);
    });
  } catch (err) {
    if (err) {
      console.error(`ImageSource or PixelMap create Error: code: ${err.code}, message: ${err.message}`);
    }
  }
}

setAVQueueItems10+

setAVQueueItems(items: Array<AVQueueItem>, callback: AsyncCallback<void>): void

Sets a playlist. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
itemsArray<AVQueueItem>YesPlaylist to set.
callbackAsyncCallback<void>YesCallback used to return the result. If the setting is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { image } from '@kit.ImageKit';
import { resourceManager } from '@kit.LocalizationKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { avSession } from '@kit.AVSessionKit'

interface ExtrasType {
  extras: string;
}

@Entry
@Component
struct Index {
  build() {
    Column() {
    }
  }
}

let currentAVSession: avSession.AVSession;

async function setAVQueueItems() {
  try {
    let value = await resourceManager.getSysResourceManager().getRawFileContent('IMAGE_URI');
    let imageSource = await image.createImageSource(value.buffer);
    let imagePixel = await imageSource.createPixelMap({ desiredSize: { width: 150, height: 150 } });
    let queueItemDescription_1: avSession.AVMediaDescription = {
      assetId: '001',
      title: 'music_name',
      subtitle: 'music_sub_name',
      description: 'music_description',
      mediaImage: imagePixel,
      extras: { extras: 'any' }
    };
    let queueItem_1: avSession.AVQueueItem = {
      itemId: 1,
      description: queueItemDescription_1
    };
    let queueItemDescription_2: avSession.AVMediaDescription = {
      assetId: '002',
      title: 'music_name',
      subtitle: 'music_sub_name',
      description: 'music_description',
      mediaImage: imagePixel,
      extras: { extras: 'any' }
    };
    let queueItem_2: avSession.AVQueueItem = {
      itemId: 2,
      description: queueItemDescription_2
    };
    let queueItemsArray: avSession.AVQueueItem[] = [queueItem_1, queueItem_2];
    currentAVSession.setAVQueueItems(queueItemsArray, (err: BusinessError) => {
      if (err) {
        console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`);
      } else {
        console.info('SetAVQueueItems successfully');
      }
    });
  } catch (err) {
    if (err) {
      console.error(`ImageSource or PixelMap create Error: code: ${err.code}, message: ${err.message}`);
    }
  }
}

setAVQueueTitle10+

setAVQueueTitle(title: string): Promise<void>

Sets a name for the playlist. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
titlestringYesName of the playlist.

Return value

TypeDescription
Promise<void>Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let queueTitle = 'QUEUE_TITLE';
currentAVSession.setAVQueueTitle(queueTitle).then(() => {
  console.info('SetAVQueueTitle successfully');
}).catch((err: BusinessError) => {
  console.error(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`);
});

setAVQueueTitle10+

setAVQueueTitle(title: string, callback: AsyncCallback<void>): void

Sets a name for the playlist. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
titlestringYesName of the playlist.
callbackAsyncCallback<void>YesCallback used to return the result. If the setting is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let queueTitle = 'QUEUE_TITLE';
currentAVSession.setAVQueueTitle(queueTitle, (err: BusinessError) => {
  if (err) {
    console.error(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`);
  } else {
    console.info('SetAVQueueTitle successfully');
  }
});

setExtras10+

setExtras(extras: Record<string, Object>): Promise<void>

Sets a custom media packet in the form of key-value pairs. This API uses a promise to return the result. It is called by the provider.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
extrasRecord<string, Object>YesKey-value pairs of the custom media packet.
Starting from API version 20, a compatibility change occurred. In API version 19 and earlier, the parameter type is {[key: string]: Object}.

NOTE

The extras parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see @ohos.app.ability.Want(Want).

Return value

TypeDescription
Promise<void>Promise used to return the result. If the setting is successful, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { avSession } from '@kit.AVSessionKit';
@Entry
@Component
struct Index {
  @State message: string = 'hello world';

  build() { 
    Column() {
        Text(this.message)
          .onClick(() => {
            let currentAVSession: avSession.AVSession|undefined = undefined;
            let tag = "createNewSession";
            let context: Context = this.getUIContext().getHostContext() as Context;

            avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => {
              if (err) {
                console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`);
              } else {
                currentAVSession = data;
                if (currentAVSession !== undefined) {
                  (currentAVSession as avSession.AVSession).setExtras({extras : "This is custom media packet"}).then(() => {
                      console.info('setExtras successfully');
                  }).catch((err: BusinessError) => {
                      console.error(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`);
                  })
                }
              }
            });
          })
      }
    .width('100%')
    .height('100%')
  }
}

setExtras10+

setExtras(extras: {[key: string]: Object}, callback: AsyncCallback<void>): void

Sets a custom media packet in the form of key-value pairs. This API uses an asynchronous callback to return the result. It is called by the provider.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
extras{[key: string]: Object}YesKey-value pairs of the custom media packet.
callbackAsyncCallback<void>YesCallback used to return the result. If the setting is successful, err is undefined; otherwise, err is an error object.

NOTE

The extras parameter supports the following data types: string, number, Boolean, object, array, and file descriptor. For details, see @ohos.app.ability.Want(Want).

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Parameter verification failed.
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { avSession } from '@kit.AVSessionKit';
@Entry
@Component
struct Index {
  @State message: string = 'hello world';

  build() { 
    Column() {
        Text(this.message)
          .onClick(()=>{
            let currentAVSession: avSession.AVSession|undefined = undefined;
            let tag = "createNewSession";
            let context: Context = this.getUIContext().getHostContext() as Context;

            avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => {
              if (err) {
                console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`);
              } else {
                currentAVSession = data;
                if (currentAVSession !== undefined) {
                  (currentAVSession as avSession.AVSession).setExtras({extras : "This is custom media packet"}, (err: BusinessError) => {
                    if (err) {
                      console.error(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`);
                    }
                  })
                }
              }
            });
          })
      }
    .width('100%')
    .height('100%')
  }
}

sendCustomData20+

sendCustomData(data: Record<string, Object>): Promise<void>

Sends custom data to the remote device. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 20.

System capability: SystemCapability.Multimedia.AVSession.AVCast

Parameters

NameTypeMandatoryDescription
dataRecord<string, Object>YesCustom data filled by the application. Only objects with the key 'customData' and of the type string are parsed on the server.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.You are advised to:1.Scheduled retry.2.Destroy the current session or session controller and re-create it.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { avSession } from '@kit.AVSessionKit';
@Entry
@Component
struct Index {
  @State message: string = 'hello world';

  build() { 
    Column() {
        Text(this.message)
          .onClick(()=>{
            let currentAVSession: avSession.AVSession|undefined = undefined;
            let tag = "createNewSession";
            let context: Context = this.getUIContext().getHostContext() as Context;

            avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => {
            if (err) {
                console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`);
            } else {
                currentAVSession = data;
            }
            });
            if (currentAVSession !== undefined) {
            (currentAVSession as avSession.AVSession).sendCustomData({customData : "This is custom data"}).then(() => {
                console.info('sendCustomData successfully');
            }).catch((err: BusinessError) => {
                console.error(`sendCustomData BusinessError: code: ${err.code}, message: ${err.message}`);
            })
            }
          })
      }
    .width('100%')
    .height('100%')
  }
}

getController10+

getController(): Promise<AVSessionController>

Obtains the controller corresponding to this session. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Return value

TypeDescription
Promise<AVSessionController>Promise used to return the session controller.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { avSession } from '@kit.AVSessionKit';

@Entry
@Component
struct Index {
  @State message: string = 'hello world';
  build() {
    Column() {
      Text(this.message)
        .onClick(async ()=>{
          try {
            let context: Context = this.getUIContext().getHostContext() as Context;
            let currentAVSession: avSession.AVSession = await avSession.createAVSession(context, 'SESSION_NAME', 'audio');
            let avSessionController: avSession.AVSessionController;
            currentAVSession.getController().then((avController: avSession.AVSessionController) => {
              avSessionController = avController;
              console.info(`GetController : SUCCESS : sessionid : ${avSessionController.sessionId}`);
            }).catch((err: BusinessError) => {
              console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`);
            });
          } catch (err) {
            if (err) {
              console.error(`AVSession create Error: ${JSON.stringify(err)}`);
            }
          }
        })
    }
    .width('100%')
    .height('100%')
  }
}

getController10+

getController(callback: AsyncCallback<AVSessionController>): void

Obtains the controller corresponding to this session. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<AVSessionController>YesCallback used to return the session controller.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { avSession } from '@kit.AVSessionKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
  @State message: string = 'hello world';

  build() {
    Column() {
      Text(this.message)
        .onClick(async () => {
          try {
            let context: Context = this.getUIContext().getHostContext() as Context;
            let currentAVSession: avSession.AVSession = await avSession.createAVSession(context, 'SESSION_NAME', 'audio');
            let avsessionController: avSession.AVSessionController;
            currentAVSession.getController((err: BusinessError, avcontroller: avSession.AVSessionController) => {
              if (err) {
                console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`);
              } else {
                avsessionController = avcontroller;
                console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`);
              }
            });
          } catch (err) {
            if (err) {
              console.error(`AVSession create Error: code: ${err.code}, message: ${err.message}`);
            }
          }
        })
    }
    .width('100%')
    .height('100%')
  }
}

getAVCastController10+

getAVCastController(): Promise<AVCastController>

Obtains the cast controller when a casting connection is set up. This API uses a promise to return the result. If the session is not in the cast state, the controller returns null.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.AVCast

Return value

TypeDescription
Promise<AVCastController>Promise used to return the cast controller.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600102The session does not exist.
6600109The remote connection is not established.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let aVCastController: avSession.AVCastController;
currentAVSession.getAVCastController().then((avcontroller: avSession.AVCastController) => {
  aVCastController = avcontroller;
  console.info('getAVCastController : SUCCESS');
}).catch((err: BusinessError) => {
  console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`);
});

getAVCastController10+

getAVCastController(callback: AsyncCallback<AVCastController>): void

Obtains the cast controller when a casting connection is set up. This API uses an asynchronous callback to return the result. If the session is not in the cast state, the controller returns null.

System capability: SystemCapability.Multimedia.AVSession.AVCast

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<AVCastController>YesCallback used to return the cast controller.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600102The session does not exist.
6600109The remote connection is not established.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let aVCastController: avSession.AVCastController;
currentAVSession.getAVCastController((err: BusinessError, avcontroller: avSession.AVCastController) => {
  if (err) {
    console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`);
  } else {
    aVCastController = avcontroller;
    console.info('getAVCastController : SUCCESS');
  }
});

getOutputDevice10+

getOutputDevice(): Promise<OutputDeviceInfo>

Obtains information about the output device for this session. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Return value

TypeDescription
Promise<OutputDeviceInfo>Promise used to return the output device information.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

currentAVSession.getOutputDevice().then((outputDeviceInfo: avSession.OutputDeviceInfo) => {
  console.info(`GetOutputDevice : SUCCESS : devices length : ${outputDeviceInfo.devices.length}`);
}).catch((err: BusinessError) => {
  console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`);
})

getOutputDevice10+

getOutputDevice(callback: AsyncCallback<OutputDeviceInfo>): void

Obtains information about the output device for this session. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<OutputDeviceInfo>YesCallback used to return the information obtained.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

currentAVSession.getOutputDevice((err: BusinessError, outputDeviceInfo: avSession.OutputDeviceInfo) => {
  if (err) {
    console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`);
  } else {
    console.info(`GetOutputDevice : SUCCESS : devices length : ${outputDeviceInfo.devices.length}`);
  }
});

activate10+

activate(): Promise<void>

Activates this session. A session can be used only after being activated. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the session is activated, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

currentAVSession.activate().then(() => {
  console.info('Activate : SUCCESS ');
}).catch((err: BusinessError) => {
  console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`);
});

activate10+

activate(callback: AsyncCallback<void>): void

Activates this session. A session can be used only after being activated. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the session is activated, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

currentAVSession.activate((err: BusinessError) => {
  if (err) {
    console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`);
  } else {
    console.info('Activate : SUCCESS ');
  }
});

deactivate10+

deactivate(): Promise<void>

Deactivates this session. You can use activate to activate the session again. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the session is deactivated, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

currentAVSession.deactivate().then(() => {
  console.info('Deactivate : SUCCESS ');
}).catch((err: BusinessError) => {
  console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`);
});

deactivate10+

deactivate(callback: AsyncCallback<void>): void

Deactivates this session. This API uses an asynchronous callback to return the result.

Deactivates this session. You can use activate to activate the session again.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the session is deactivated, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

currentAVSession.deactivate((err: BusinessError) => {
  if (err) {
    console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`);
  } else {
    console.info('Deactivate : SUCCESS ');
  }
});

destroy10+

destroy(): Promise<void>

Destroys this session. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Return value

TypeDescription
Promise<void>Promise used to return the result. If the session is destroyed, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

currentAVSession.destroy().then(() => {
  console.info('Destroy : SUCCESS ');
}).catch((err: BusinessError) => {
  console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`);
});

destroy10+

destroy(callback: AsyncCallback<void>): void

Destroys this session. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the session is destroyed, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

currentAVSession.destroy((err: BusinessError) => {
  if (err) {
    console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`);
  } else {
    console.info('Destroy : SUCCESS ');
  }
});

on('play')10+

on(type: 'play', callback: () => void): void

Subscribes to play command events. The subscription means that the application supports the play command.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'play' is triggered when the command for starting playback is sent to the session.
callback() => voidYesCallback used to return the result.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('play', () => {
  console.info('on play entry');
});

onPlay22+

onPlay(callback: Callback<CommandInfo>): void

Subscribes to play command events. This API uses an asynchronous callback to return the result.

The application receives CommandInfo sent by the controller through the callback.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackCallback<CommandInfo>YesCallback used for subscription. If the subscription is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.onPlay((info: CommandInfo) => {
  console.info('on play entry');
});

on('pause')10+

on(type: 'pause', callback: () => void): void

Subscribes to pause command events. The subscription means that the application supports the pause command.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'pause' is triggered when the command for pausing the playback is sent to the session.
callback() => voidYesCallback used for subscription. If the subscription is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('pause', () => {
  console.info('on pause entry');
});

on('stop')10+

on(type:'stop', callback: () => void): void

Subscribes to stop command events. The subscription means that the application supports the stop command.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'stop' is triggered when the command for stopping the playback is sent to the session.
callback() => voidYesCallback used for subscription. If the subscription is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('stop', () => {
  console.info('on stop entry');
});

on('playNext')10+

on(type:'playNext', callback: () => void): void

Subscribes to playNext command events. The subscription means that the application supports the playNext command.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'playNext' is triggered when the command for playing the next item is sent to the session.
callback() => voidYesCallback used for subscription. If the subscription is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('playNext', () => {
  console.info('on playNext entry');
});

onPlayNext22+

onPlayNext(callback: Callback<CommandInfo>): void

Subscribes to playNext command events. This API uses an asynchronous callback to return the result.

The application receives CommandInfo sent by the controller through the callback.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackCallback<CommandInfo>YesCallback used for subscription. If the subscription is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.onPlayNext((info: CommandInfo) => {
  console.info('on playNext entry');
});

on('playPrevious')10+

on(type:'playPrevious', callback: () => void): void

Subscribes to playPrevious command events. The subscription means that the application supports the playPrevious command.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'playPrevious' is triggered when the command for playing the previous item sent to the session.
callback() => voidYesCallback used for subscription. If the subscription is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('playPrevious', () => {
  console.info('on playPrevious entry');
});

onPlayPrevious22+

onPlayPrevious(callback: Callback<CommandInfo>): void

Subscribes to playPrevious command events. This API uses an asynchronous callback to return the result.

The application receives CommandInfo sent by the controller through the callback.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackCallback<CommandInfo>YesCallback used for subscription. If the subscription is successful, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.onPlayPrevious((info: CommandInfo) => {
  console.info('on playPrevious entry');
});

on('fastForward')10+

on(type: 'fastForward', callback: (time?: number) => void): void

Subscribes to fastForward command events. The subscription means that the application supports the fastForward command.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'fastForward' is triggered when the command for fast forwarding is sent to the session.
callback(time?: number) => voidYesCallback used for subscription. The time parameter in the callback indicates the time to seek to, in seconds.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('fastForward', (time?: number) => {
  console.info('on fastForward entry');
});

onFastForward22+

onFastForward(callback: TwoParamCallback<number, CommandInfo>): void

Subscribes to fastForward command events. This API uses an asynchronous callback to return the result.

The application receives the fast-forward time parameter and CommandInfo sent by the controller through the callback.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackTwoParamCallback<number, CommandInfo>YesCallback used to return the result. It is used to process the fastForward operation.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.onFastForward((time: number, info: CommandInfo) => {
  console.info('on fastForward entry');
});

on('rewind')10+

on(type:'rewind', callback: (time?: number) => void): void

Subscribes to rewind command events.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'rewind' is triggered when the command for rewinding is sent to the session.
callback(time?: number) => voidYesCallback used for subscription. The time parameter in the callback indicates the time to seek to, in seconds.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('rewind', (time?: number) => {
  console.info('on rewind entry');
});

onRewind22+

onRewind(callback: TwoParamCallback<number, CommandInfo>): void

Subscribes to rewind command events. This API uses an asynchronous callback to return the result.

The application receives the rewind time parameter and CommandInfo sent by the controller through the callback.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackTwoParamCallback<number, CommandInfo>YesCallback used to return the result. It is used to process the rewind operation.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.onRewind((time: number, info: CommandInfo) => {
  console.info('on rewind entry');
});

on('playWithAssetId')20+

on(type:'playWithAssetId', callback: Callback<string>): void

Subscribes to playback events with a given media asset ID.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 20.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'playWithAssetId' is triggered when the media asset ID is played.
callbackCallback<string>YesCallback The assetId parameter in the callback indicates the media asset ID.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

let playWithAssetIdCallback = (assetId: string) => {
  console.info(`on playWithAssetId entry,  assetId = ${assetId}`);
}
currentAVSession.on('playWithAssetId', playWithAssetIdCallback);

off('playWithAssetId')20+

off(type: 'playWithAssetId', callback?: Callback<string>): void

Unsubscribes from playback events with a given media asset ID. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 20.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'playWithAssetId' in this case.
callbackCallback<string>NoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. The assetId parameter in the callback indicates the media asset ID.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('playWithAssetId');

on('seek')10+

on(type: 'seek', callback: (time: number) => void): void

Subscribes to seek command events.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'seek' is triggered when the seek command is sent to the session.
callback(time: number) => voidYesCallback used for subscription. The time parameter in the callback indicates the time to seek to, in milliseconds.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('seek', (time: number) => {
  console.info(`on seek entry time : ${time}`);
});

on('setSpeed')10+

on(type: 'setSpeed', callback: (speed: number) => void): void

Subscribes to setSpeed command events.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'setSpeed' is triggered when the command for setting the playback speed is sent to the session.
callback(speed: number) => voidYesCallback used for subscription. The speed parameter in the callback indicates the playback speed.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('setSpeed', (speed: number) => {
  console.info(`on setSpeed speed : ${speed}`);
});

on('setLoopMode')10+

on(type: 'setLoopMode', callback: (mode: LoopMode) => void): void

Subscribes to setLoopMode command events.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'setLoopMode' is triggered when the command for setting the loop mode is sent to the session.
callback(mode: LoopMode) => voidYesCallback used for subscription. The mode parameter in the callback indicates the loop mode.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('setLoopMode', (mode: avSession.LoopMode) => {
  console.info(`on setLoopMode mode : ${mode}`);
});

on('setTargetLoopMode')18+

on(type: 'setTargetLoopMode', callback: Callback<LoopMode>): void

Subscribes to setTargetLoopMode command events.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 18.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'setTargetLoopMode'
is triggered when the command for setting the target loop mode is sent to the session.
callbackCallback<LoopMode>YesCallback used for subscription. The LoopMode parameter in the callback indicates the target loop mode.

Error codes

For details about the error codes, see Universal Error Codes and AVSession Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('setTargetLoopMode', (mode: avSession.LoopMode) => {
  console.info(`on setTargetLoopMode mode : ${mode}`);
});

on('toggleFavorite')10+

on(type: 'toggleFavorite', callback: (assetId: string) => void): void

Subscribes to toggleFavorite command events.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'toggleFavorite' is triggered when the command for favoriting the media asset is sent to the session.
callback(assetId: string) => voidYesCallback used for subscription. The assetId parameter in the callback indicates the media asset ID.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('toggleFavorite', (assetId: string) => {
  console.info(`on toggleFavorite mode : ${assetId}`);
});

on('skipToQueueItem')10+

on(type: 'skipToQueueItem', callback: (itemId: number) => void): void

Subscribes to the event that indicates an item in the playlist is selected. The session can play the selected item.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'skipToQueueItem' is triggered when an item in the playlist is selected.
callback(itemId: number) => voidYesCallback used for subscription. The itemId parameter in the callback indicates the ID of the selected item.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('skipToQueueItem', (itemId: number) => {
  console.info(`on skipToQueueItem id : ${itemId}`);
});

on('handleKeyEvent')10+

on(type: 'handleKeyEvent', callback: (event: KeyEvent) => void): void

Subscribes to key events of external devices such as Bluetooth and wired devices to listen for the play, pause, previous, next, fast-forward, and rewind commands in the key events.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'handleKeyEvent' is triggered when a key event is sent to the session.
callback(event: KeyEvent) => voidYesCallback used for subscription. The event parameter in the callback indicates the key event.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

import { KeyEvent } from '@kit.InputKit';

currentAVSession.on('handleKeyEvent', (event: KeyEvent) => {
  console.info(`on handleKeyEvent event : ${event}`);
});

on('outputDeviceChange')10+

on(type: 'outputDeviceChange', callback: (state: ConnectionState, device: OutputDeviceInfo) => void): void

Subscribes to output device change events. After the application integrates the AVCastPicker component, the application receives the device change callback when the user switches the device through the component.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'outputDeviceChange' is triggered when the output device changes.
callback(state: ConnectionState, device: OutputDeviceInfo) => voidYesCallback function, where the device parameter specifies the output device information.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('outputDeviceChange', (state: avSession.ConnectionState, device: avSession.OutputDeviceInfo) => {
  console.info(`on outputDeviceChange device : ${device}`);
});

on('commonCommand')10+

on(type: 'commonCommand', callback: (command: string, args: Record<string, Object>) => void): void

Subscribes to custom control command change events.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'commonCommand' is triggered when a custom control command changes.
callback(command: string, args: Record<string, Object>) => voidYesCallback used for subscription. The command parameter in the callback indicates the name of the changed custom control command, and args indicates the parameters carried in the command. The parameters must be the same as those set in sendCommonCommand.
Starting from API version 20, a compatibility change occurred. In API version 19 and earlier, the parameter type is (command :string, args:{[key: string]: Object}) => void.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { avSession } from '@kit.AVSessionKit';
@Entry
@Component
struct Index {
  @State message: string = 'hello world';

  build() { 
    Column() {
        Text(this.message)
          .onClick(()=>{
            let currentAVSession: avSession.AVSession|undefined = undefined;
            let tag = "createNewSession";
            let context: Context = this.getUIContext().getHostContext() as Context;

            avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => {
              if (err) {
                console.error(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`);
              } else {
                currentAVSession = data;
                if (currentAVSession !== undefined) {
                  (currentAVSession as avSession.AVSession).on('commonCommand', (commonCommand, args) => {
                      console.info(`OnCommonCommand, the command is ${commonCommand}, args: ${JSON.stringify(args)}`);
                  });
                }
              }
            });
          })
      }
    .width('100%')
    .height('100%')
  }
}

off('play')10+

off(type: 'play', callback?: () => void): void

Unsubscribes from play command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'play' in this case.
callback() => voidNoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('play');

offPlay22+

offPlay(callback?: Callback<CommandInfo>): void

Unsubscribes from play command events. This API uses an asynchronous callback to return the result.

If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackCallback<CommandInfo>NoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.offPlay();

off('pause')10+

off(type: 'pause', callback?: () => void): void

Unsubscribes from pause command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'pause' in this case.
callback() => voidNoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('pause');

off('stop')10+

off(type: 'stop', callback?: () => void): void

Unsubscribes from stop command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'stop' in this case.
callback() => voidNoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('stop');

off('playNext')10+

off(type: 'playNext', callback?: () => void): void

Unsubscribes from playNext command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'playNext' in this case.
callback() => voidNoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('playNext');

offPlayNext22+

offPlayNext(callback?: Callback<CommandInfo>): void

Unsubscribes from playNext command events. This API uses an asynchronous callback to return the result.

If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackCallback<CommandInfo>NoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.offPlayNext();

off('playPrevious')10+

off(type: 'playPrevious', callback?: () => void): void

Unsubscribes from playPrevious command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'playPrevious' in this case.
callback() => voidNoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('playPrevious');

offPlayPrevious22+

offPlayPrevious(callback?: Callback<CommandInfo>): void

Unsubscribes from playPrevious command events. This API uses an asynchronous callback to return the result.

If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackCallback<CommandInfo>NoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.offPlayPrevious();

off('fastForward')10+

off(type: 'fastForward', callback?: () => void): void

Unsubscribes from fastForward command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'fastForward' in this case.
callback() => voidNoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('fastForward');

offFastForward22+

offFastForward(callback?: TwoParamCallback<number, CommandInfo>): void

Unsubscribes from fastForward command events. This API uses an asynchronous callback to return the result.

If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackTwoParamCallback<number, CommandInfo>NoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.offFastForward();

off('rewind')10+

off(type: 'rewind', callback?: () => void): void

Unsubscribes from rewind command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'rewind' in this case.
callback() => voidNoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('rewind');

offRewind22+

offRewind(callback?: TwoParamCallback<number, CommandInfo>): void

Unsubscribes from rewind command events. This API uses an asynchronous callback to return the result.

If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
callbackTwoParamCallback<number, CommandInfo>NoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.offRewind();

off('seek')10+

off(type: 'seek', callback?: (time: number) => void): void

Unsubscribes from seek command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'seek' in this case.
callback(time: number) => voidNoCallback used for unsubscription. The time parameter in the callback indicates the time to seek to, in milliseconds.
If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('seek');

off('setSpeed')10+

off(type: 'setSpeed', callback?: (speed: number) => void): void

Unsubscribes from setSpeed command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'setSpeed' in this case.
callback(speed: number) => voidNoCallback used for unsubscription. The speed parameter in the callback indicates the playback speed.
If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('setSpeed');

off('setLoopMode')10+

off(type: 'setLoopMode', callback?: (mode: LoopMode) => void): void

Unsubscribes from setLoopMode command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'setLoopMode' in this case.
callback(mode: LoopMode) => voidNoCallback used for unsubscription. The mode parameter in the callback indicates the loop mode.
- If the unsubscription is successful, err is undefined; otherwise, err is an error object.
- The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('setLoopMode');

off('setTargetLoopMode')18+

off(type: 'setTargetLoopMode', callback?: Callback<LoopMode>): void

Unsubscribes from setTargetLoopMode command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 18.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'setTargetLoopMode' in this case.
callbackCallback<LoopMode>NoCallback used for unsubscription. The LoopMode parameter in the callback indicates the target loop mode.
- If the unsubscription is successful, err is undefined; otherwise, err is an error object.
- The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see Universal Error Codes and AVSession Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('setTargetLoopMode');

off('toggleFavorite')10+

off(type: 'toggleFavorite', callback?: (assetId: string) => void): void

Unsubscribes from toggleFavorite command events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'toggleFavorite' in this case.
callback(assetId: string) => voidNoCallback used for unsubscription. The assetId parameter in the callback indicates the media asset ID.
If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('toggleFavorite');

off('skipToQueueItem')10+

off(type: 'skipToQueueItem', callback?: (itemId: number) => void): void

Unsubscribes from the event that indicates an item in the playlist is selected. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'skipToQueueItem' in this case.
callback(itemId: number) => voidNoCallback used for unsubscription. The itemId parameter in the callback indicates the ID of the item.
If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('skipToQueueItem');

off('handleKeyEvent')10+

off(type: 'handleKeyEvent', callback?: (event: KeyEvent) => void): void

Unsubscribes from key events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'handleKeyEvent' in this case.
callback(event: KeyEvent) => voidNoCallback used for unsubscription. The event parameter in the callback indicates the key event.
If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('handleKeyEvent');

off('outputDeviceChange')10+

off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: OutputDeviceInfo) => void): void

Unsubscribes from playback device change events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'outputDeviceChange' in this case.
callback(state: ConnectionState, device: OutputDeviceInfo) => voidNoCallback function, where the device parameter specifies the output device information.
If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('outputDeviceChange');

off('commonCommand')10+

off(type: 'commonCommand', callback?: (command: string, args: Record<string, Object>) => void): void

Unsubscribes from custom control command change events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'commonCommand' in this case.
callback(command: string, args: Record<string, Object>) => voidNoCallback used for unsubscription. The command parameter in the callback indicates the name of the changed custom control command, and args indicates the parameters carried in the command.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.
Starting from API version 20, a compatibility change occurred. In API version 19 and earlier, the parameter type is (command: string, args:{[key: string]: Object}) => void.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('commonCommand');

on('answer')11+

on(type: 'answer', callback: Callback<void>): void

Subscribes to call answer events.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'answer' is triggered when a call is answered.
callbackCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('answer', () => {
  console.info('on call answer');
});

off('answer')11+

off(type: 'answer', callback?: Callback<void>): void

Unsubscribes from call answer events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'answer' in this case.
callbackCallback<void>NoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('answer');

on('hangUp')11+

on(type: 'hangUp', callback: Callback<void>): void

Subscribes to call hangup events.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'hangUp' is triggered when a call is hung up.
callbackCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('hangUp', () => {
  console.info('on call hangUp');
});

off('hangUp')11+

off(type: 'hangUp', callback?: Callback<void>): void

Unsubscribes from call answer events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'hangUp' in this case.
callbackCallback<void>NoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('hangUp');

on('toggleCallMute')11+

on(type: 'toggleCallMute', callback: Callback<void>): void

Subscribes to call mute events.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'toggleCallMute' is triggered when a call is muted or unmuted.
callbackCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('toggleCallMute', () => {
  console.info('on call toggleCallMute');
});

off('toggleCallMute')11+

off(type: 'toggleCallMute', callback?: Callback<void>): void

Unsubscribes from call mute events. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'toggleCallMute' in this case.
callbackCallback<void>NoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('toggleCallMute');

on('castDisplayChange')12+

on(type: 'castDisplayChange', callback: Callback<CastDisplayInfo>): void

Subscribes to cast display change events in the case of extended screens.

Multiple callbacks can be registered for this event. To ensure only the latest callback executes, unregister previous listeners first. Otherwise, all registered callbacks will fire on state changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.ExtendedDisplayCast

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'castDisplayChange' is triggered when the cast display in the case of extended screens changes.
callbackCallback<CastDisplayInfo>YesCallback used to return the information about the cast display.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

let castDisplay: avSession.CastDisplayInfo;
currentAVSession.on('castDisplayChange', (display: avSession.CastDisplayInfo) => {
    if (display.state === avSession.CastDisplayState.STATE_ON) {
        castDisplay = display;
        console.info(`Succeeded in castDisplayChange display : ${display.id} ON`);
    } else if (display.state === avSession.CastDisplayState.STATE_OFF){
        console.info(`Succeeded in castDisplayChange display : ${display.id} OFF`);
    }
});

off('castDisplayChange')12+

off(type: 'castDisplayChange', callback?: Callback<CastDisplayInfo>): void

Unsubscribes from cast display change events in the case of extended screens. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.ExtendedDisplayCast

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'castDisplayChange' in this case.
callbackCallback<CastDisplayInfo>NoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object. The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('castDisplayChange');

stopCasting10+

stopCasting(callback: AsyncCallback<void>): void

Stops castings. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Multimedia.AVSession.AVCast

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the command is sent, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600109The remote connection is not established.

Example

import { BusinessError } from '@kit.BasicServicesKit';

currentAVSession.stopCasting((err: BusinessError) => {
  if (err) {
    console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`);
  } else {
    console.info('stopCasting successfully');
  }
});

stopCasting10+

stopCasting(): Promise<void>

Stops castings. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.AVCast

Return value

TypeDescription
Promise<void>Promise used to return the result. If casting stops, no value is returned; otherwise, an error object is returned.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600109The remote connection is not established.

Example

import { BusinessError } from '@kit.BasicServicesKit';

currentAVSession.stopCasting().then(() => {
  console.info('stopCasting successfully');
}).catch((err: BusinessError) => {
  console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`);
});

getOutputDeviceSync10+

getOutputDeviceSync(): OutputDeviceInfo

Obtains the output device information. This API returns the result synchronously.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Return value

TypeDescription
OutputDeviceInfoInformation about the output device.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

try {
  let currentOutputDevice: avSession.OutputDeviceInfo = currentAVSession.getOutputDeviceSync();
} catch (err) {
  let error = err as BusinessError;
  console.error(`getOutputDeviceSync error, error code: ${error.code}, error message: ${error.message}`);
}

getAllCastDisplays12+

getAllCastDisplays(): Promise<Array<CastDisplayInfo>>

Obtains all displays that support extended screen projection in the current system. This API uses a promise to return the result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.ExtendedDisplayCast

Return value

TypeDescription
Promise<Array<CastDisplayInfo>>Promise used to return the information about all the cast displays.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.
6600102The session does not exist.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let castDisplay: avSession.CastDisplayInfo;
currentAVSession.getAllCastDisplays().then((data: Array< avSession.CastDisplayInfo >) => {
    if (data.length >= 1) {
       castDisplay = data[0];
     }
   }).catch((err: BusinessError) => {
     console.error(`Failed to getAllCastDisplay. Code: ${err.code}, message: ${err.message}`);
   });

on('playFromAssetId')(deprecated)

on(type:'playFromAssetId', callback: (assetId: number) => void): void

Subscribes to playback events with a given media asset ID.

NOTE

This API is supported since API version 11 and deprecated since API version 20. You are advised to use on('playWithAssetId') instead.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'playFromAssetId' is triggered when the media asset ID is played.
callback(assetId: number) => voidYesCallback The assetId parameter in the callback indicates the media asset ID.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.on('playFromAssetId', (assetId: number) => {
  console.info('on playFromAssetId entry');
});

off('playFromAssetId')(deprecated)

off(type: 'playFromAssetId', callback?: (assetId: number) => void): void

Unsubscribes from playback events with a given media asset ID. If a callback is specified, the corresponding listener is unregistered. If no callback is specified, all listeners for the specified event are unregistered.

NOTE

This API is supported since API version 11 and deprecated since API version 20. You are advised to use off('playWithAssetId') instead.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Multimedia.AVSession.Core

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'playFromAssetId' in this case.
callback(assetId: number) => voidNoCallback used for unsubscription. If the unsubscription is successful, err is undefined; otherwise, err is an error object.
The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session. The assetId parameter in the callback indicates the media asset ID.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
401parameter check failed. 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
6600101Session service exception.
6600102The session does not exist.

Example

currentAVSession.off('playFromAssetId');

on('customDataChange')20+

on(type: 'customDataChange', callback: Callback<Record<string, Object>>): void

Subscribes to events indicating that custom data is sent to a remote device.

Atomic service API: This API can be used in atomic services since API version 20.

System capability: SystemCapability.Multimedia.AVSession.AVCast

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The event 'customDataChange' is triggered when the provider sends custom data.
callbackCallback<Record<string, Object>>YesCallback used to receive the custom data.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.You are advised to:1.Scheduled retry.2.Destroy the current session or session controller and re-create it.
6600102The session does not exist.

Example

currentAVSession.on('customDataChange', (callback) => {
    console.info(`Caught customDataChange event,the new callback is: ${JSON.stringify(callback)}`);
});

off('customDataChange')20+

off(type: 'customDataChange', callback?: Callback<Record<string, Object>>): void

Unsubscribes from events indicating that custom data is sent to a remote device.

Atomic service API: This API can be used in atomic services since API version 20.

System capability: SystemCapability.Multimedia.AVSession.AVCast

Parameters

NameTypeMandatoryDescription
typestringYesEvent type, which is 'customDataChange' in this case.
callbackCallback<Record<string, Object>>NoCallback used for unsubscription. The callback parameter is optional. If it is not specified, all the subscriptions to the specified event are canceled for this session.

Error codes

For details about the error codes, see AVSession Management Error Codes.

IDError Message
6600101Session service exception.You are advised to:1.Scheduled retry.2.Destroy the current session or session controller and re-create it.
6600102The session does not exist.

Example

currentAVSession.off('customDataChange');

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 arkts-apis-avsession

openharmony 鸿蒙 arkts-apis-avsession-e

openharmony 鸿蒙 capi-native-avsession-h

openharmony 鸿蒙 errorcode-avsession

openharmony 鸿蒙 js-apis-inner-application-MediaControlExtensionContext-sys

openharmony 鸿蒙 ohos-multimedia-avcastpicker

openharmony 鸿蒙 arkts-apis-avsession-AVCastController

openharmony 鸿蒙 arkts-apis-avsession-AVCastPickerHelper

openharmony 鸿蒙 capi-native-avmetadata-h

openharmony 鸿蒙 arkts-apis-avsession-t

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/ybuqdnGy