diff --git a/zh-cn/application-dev/media/Readme-CN.md b/zh-cn/application-dev/media/Readme-CN.md index 2fac2281c20827d058b7bc483bd7e038da33cfab..17a59c5d2d5df7b6bc1384d1423b01a275796023 100755 --- a/zh-cn/application-dev/media/Readme-CN.md +++ b/zh-cn/application-dev/media/Readme-CN.md @@ -48,6 +48,8 @@ - 分布式媒体会话 - [分布式媒体会话概述](distributed-avsession-overview.md) - [使用分布式媒体会话](using-distributed-avsession.md) + - 音视频投播 + - [使用音视频投播](using-avsession-avcast.md) - 相机 - [相机开发概述](camera-overview.md) - 相机开发指导 diff --git a/zh-cn/application-dev/media/using-avsession-avcast.md b/zh-cn/application-dev/media/using-avsession-avcast.md new file mode 100644 index 0000000000000000000000000000000000000000..2d33da6d87b0296708546d31087649a289457633 --- /dev/null +++ b/zh-cn/application-dev/media/using-avsession-avcast.md @@ -0,0 +1,307 @@ +# 音视频投播开发 + +鸿蒙提供了统一的应用内音视频投播功能设计,通过提供投播组件和标准的接口,使得应用进行跨端投播音视频资源变得简单便捷。同时,系统默认支持Cast+协议,有效提升了投播体验。使用系统提供的投播组件和接口后,应用仅需要负责进行资源信息的设置,投播中部分状态的监听,以及应用主动控制的行为,如播放、暂停、切换资源等,其他动作包括图标切换,设备的发现、连接、认证等,都由系统统一负责完成。 + + +## 基本概念 + +- 媒体会话(AVSession):本地播放时用于进行媒体播放中的信息的更新和对系统播控中心的控制响应,[媒体会话提供方接入参考](using-avsession-developer.md)。 在投播时,AVSession用于在本地播放和投播之间切换的“枢纽”接口,把二者联系起来,即在本地播放时,如果接收到远端设备的连接状态,则应用感知到远端投播场景,进入到远端投播状态。通过AVSession可以进行应用投播能力的设置和查询,并进行投播控制器的创建。 +- 投播组件[AVCastPicker](../reference/apis/js-apis-avcastpicker.md):应用用于创建系统级的投播组件,嵌入在应用内界面,当用户点击后,由系统负责进行设备发现、连接、认证,应用只需要通过接口获取到投播中的状态回调。 +- 投播控制器[AVCastController](../reference/apis/js-apis-avsession.md):在投播后,由应用发起的对远端播放控制的接口,包括播放、暂停、调节音量、设置播放模式、设置播放速度等能力。 +- 后台长时任务:进行投播时应用后台任务的申请,避免应用在投播后被系统后台清理或冻结。后台长时任务[开发参考](../task-management/continuous-task-dev-guide.md)。 + +## 接入说明 + +- 基本原则 + +应用需要接入AVSession才可以使用系统提供的统一投播能力。系统不额外提供音视频设备发现的接口由应用选择和切换,需要通过系统提供的接口进行音视频的投播接入。 + +- 详细说明 + +鸿蒙系统把本地播控和远端投播融合归一到AVSession中,体现了系统的统一设计。使用系统提供的统一投播组件,应用可以减少对设备发现连接的关注,而只需要关注连接设备是否在远端进行。当用户通过投播组件,选择了某个远端设备并连接成功,应用会通过回调接口获取到该设备连接成功的事件,然后可以创建一个“远端遥控器” AVCastController,从而实现对远端设备播放的控制操作。当进入到远端设备投播状态时,应用也可以对应用界面进行重新绘制,比如呈现遥控器状态。一个比较好的体验是,在进入远端投播时,本地的播放器最好停止,以防止本端和投播端设备同时播放的情形。 + +对于本端和远端设备的场景的区分,参考 AVCastCategory 的定义: + +```ts +/** + * The default cast type "local", media can be routed on the same device, + * including internal speakers or audio jacks on the device itself, A2DP devices. + */ +CATEGORY_LOCAL = 0, + +/** + * The remote category indicating the media is presenting on a different device + * the application need get an AVCastController to control remote playback. + */ +CATEGORY_REMOTE = 1, +``` + +本次的投播能力和已有的分布式会话有什么关联和区别? + +1. 分布式会话是基于分布式软总线的能力在双端设备之间进行会话信息的同步,投播能力基于Cast+的能力进行数据传输和信息同步,支持的设备类型更丰富; +2. 分布式会话投播音频投播成功后,远端无对应信息展示,仅有声音会传输到对端设备;而投播能力支持在远端设备进行播放的展示,体验上会更有优势; +2. 投播能力当前支持音频,长期会支持视频,这个是分布式会话不支持的; +3. 分布式会话能力长期会融入到投播能力中,由系统提供统一的接口包装,更容易接入和使用。 + +## 接口说明 + +接口返回值有两种返回形式:callback和promise,下表中为callback形式接口,promise和callback只是返回值方式不一样,功能相同。 + +### 投播组件接口 + +更多API说明请参见[API文档](../reference/apis/js-apis-avcastpicker.md)。 + +| 接口 | 说明 | +| -------- | -------- | +| AVCastPicker |投播组件的定义 | + +### 投播相关的主要接口 + +更多API说明请参见[API文档](../reference/apis/js-apis-avsession.md)。 + +| 接口定义 | 说明 | +| -------- | -------- | +| getAVCastController(callback: AsyncCallback\): void; | 获取远端投播时的控制接口。| +| on(type: 'outputDeviceChange', callback: (state: ConnectionState, device: OutputDeviceInfo) => void): void; | 注册设备变化的回调,同时包含了设备的连接状态。| +| sendControlCommand(command: AVCastControlCommand, callback: AsyncCallback\): void; | 投播会话的控制接口,用于进行投播中的各种播控指令 | +| prepare(item: AVQueueItem, callback: AsyncCallback): void | 准备播放,进行资源加载和缓冲,不会触发真正的播放 | +| start(item: AVQueueItem, callback: AsyncCallback): void | 开始播放媒体资源 | +| on(type: 'playbackStateChange', filter: Array\ \| 'all', callback: (state: AVPlaybackState) => void) | 注册播放状态变化的回调 | +| on(type: 'mediaItemChange', callback: AsyncCallback\): void; | 注册当前播放内容更新的回调,返回当前播放的内容的信息。| + +Note: + +*AVCastController由系统获取并返回,在设备连接成功后获取,在设备断开后不能继续使用,否则会抛出异常* + + +## 应用开发步骤 + +应用接入投播功能,分为如下几步: + +1. 创建AVSession,既可以用于本地,也用于投播的实现 +2. 在需要投播的播放界面绘制投播组件 +3. 向AVSession注册设备投播过程状态的回调,感知投播连接 +4. 通过AVSession获取投播控制器,进行投播中的播放控制 +5. 投播启动后,应用需要进行长时任务的申请 +6. 投播结束后,销毁过程对象 + +### 接入效果展示 + +![Cast Demo](figures/cast-demo.png) + +### 应用接入示意 + +![Cast Interaction Diagram](figures/cast-interaction-diagram.png) + +### 进入投播和远端控制响应 + +1. 创建播放器,并创建AVSession,通过AVSessionManager的方法创建并激活媒体会话。 + +```ts +import AVSessionManager from '@ohos.multimedia.avsession'; //导入AVSession模块 + +// 创建session +async createSession() { + let session: AVSessionManager.AVSession = await AVSessionManager.createAVSession(this.context, 'SESSION_NAME', 'audio'); + session.activate(); + console.info(`session create done : sessionId : ${session.sessionId}`); +} +``` + +2. 在播放页面创建投播组件。 + +```ts +// 创建组件,并设置大小 +build() { + Row() { + Column() { + AVCastPicker() + .width('40vp') + .height('40vp') + .border({ width: 1, color: Color.Red }) + } + } +} +``` + +3. 设置AVSession的信息,注册AVSession的回调: + +```ts +async setListenerFromController() { + let session: AVSessionManager.AVSession = ALLREADY_CREATE_A_SESSION; + + // 对于应用来说,可以在每次更新资源的时候,设置设备过滤器,以通知系统进行设备选择界面的呈现 + // 如果应用不需要,或者不设置,系统会按照默认的设备策略进行展示 + // 设备过滤,一般是由应用根据播放的资源指定能够支持的投播设备 + // 通过extras 的 deviceFilter 设置设备过滤条件 + let extras = { + deviceFilter: AVCastDeviceCategory.DEVICE_CAPABILITY_CAST_STREAM; + } + await session.setExtras(extras).then(() => { + console.info(`Set extras successfully`); + }).catch((err) => { + console.error(`Failed to set extras. Code: ${err.code}, message: ${err.message}`); + }) + + // 监听设备连接状态的变化 + session.on("outputDeviceChange", (state, device) => { + if (state == AVCastState.STATE_CONNECTED) { // 设备连接成功 + global.avcastcontroller = avsession.getAVCastController(); + console.info('get a cast controller successfully'); + console.info(`device connected ` + ${JSON.stringify(device)}); + + // 设置播放参数,开始播放 + var playItem = { + itemId: 0, + description: { + mediaId: '12345', + mediaName: 'song1', + mediaType: 'AUDIO', + mediaUri: 'http://resource1_address', + mediaSize: 12345, + startPosition: 0, + duration: 0, + artist: 'mysong', + albumTitle: 'song1_title', + albumCoverUri: "http://resource1_album_address", + lyricUri: "http://resource1_lyric_address", + iconUri: "http://resource1_icon_address", + appName: 'MyMusic' + } + }; + // 准备播放,这个不会触发真正的播放,会进行加载和缓冲 + global.avcastcontroller.prepare(playItem, () => { + console.info('prepare done'); + }); + // 启动播放 + global.avcastcontroller.start(playItem, () => { + console.info('play done'); + }); + + // 列表里的第一首歌曲投播的时候,本端的Player要停止 + this.localplayer.stop(); + + // 本端投播后的应用界面由应用进行专辑图、歌手等信息的刷新 + + // 远端状态的回调,这个回调需要应用进行界面刷新,而不需要执行控制操作 + // 回调包括播放状态,播放位置,播放速度,播放模式等 + global.avcontroller.on('playbackStateChange', filter: 'all', (state) => { + console.info('playbackStateChange ' + state); + }); + // 用于响应远端进行上一首、下一首 + global.avcontroller.on('playNext', () => { + // 应用根据列表顺序,更新新的资源进行播放 + var nextPlayItem = { + itemId: 2, + description: { + mediaId: '12345', + mediaName: 'song2', + mediaType: 'AUDIO', + mediaUri: 'http://resource2_address', + mediaSize: 12345, + startPosition: 0, + duration: 0, + artist: 'mysong', + albumTitle: 'song2_title', + albumCoverUri: "http://resource2_album_address", + lyricUri: "http://resource2_lyric_address", + iconUri: "http://resource2_icon_address", + appName: 'MyMusic' + } + }; + // 准备播放 + global.avcastcontroller.prepare(nextPlayItem, () => { + console.info('prepare done'); + }); + // 启动播放 + global.avcastcontroller.start(nextPlayItem, () => { + console.info('play done'); + }); + }); + } + } + + // 可以通过getOutputDevice接口主动查询连接的设备 + session.getOutputDevice((device) => { + console.info(`current routing device` + ${JSON.stringify(device)}); + }); +} +``` + +4. 使用AVCastController进行应用内的播放控制: + +```ts +async sendCommandToSessionByController() { + // 记录从avsession获取的远端控制器 + // 下发播放命令 + let avCommand: AVSessionManager.AVCastControlCommand = {command:'play'}; + global.avcontroller.sendControlCommand(avCommand); + + // 下发暂停命令 + let avCommand: AVSessionManager.AVCastControlCommand = {command:'pause'}; + global.avcontroller.sendControlCommand(avCommand); + + // 播放、暂停等的操作,可以通过状态的回调接口获取到,然后进行本端界面的刷新 +} +``` + +5. 申请投播长时任务,避免应用在投播进入后台时被系统冻结,导致无法持续投播 + +```ts +import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager'; +import wantAgent from '@ohos.app.ability.wantAgent'; + +function startContinuousTask() { + let wantAgentInfo = { + // 点击通知后,将要执行的动作列表 + wants: [ + { + bundleName: "com.example.myapplication", + abilityName: "EntryAbility", + } + ], + // 点击通知后,动作类型 + operationType: wantAgent.OperationType.START_ABILITY, + // 使用者自定义的一个私有值 + requestCode: 0, + // 点击通知后,动作执行属性 + wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] + }; + + // 通过wantAgent模块的getWantAgent方法获取WantAgent对象 + try { + wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { + try { + backgroundTaskManager.startBackgroundRunning(mContext, + backgroundTaskManager.BackgroundMode.MULTI_DEVICE_CONNECTION, wantAgentObj).then(() => { + console.info("Operation succeeded"); + }).catch((error) => { + console.error(`Operation failed. code is ${error.code} message is ${error.message}`); + }); + } catch (error) { + console.error(`Operation failed. code is ${error.code} message is ${error.message}`); + } + }); + } catch (error) { + console.error(`Operation getWantAgent failed. code is ${error.code} message is ${error.message}`); + } +} +``` + +6. 音频焦点的处理 + +在应用进入投播后,当前应用需要去注册焦点处理,以免被其他应用的焦点申请而影响。 +**当前投播支持全局切换,后续如果规格变更,再同步调整方案说明** + +7. 结束投播 + +当远端设备断开的时候,应用会收到事件,系统会自动断开连接。 + +应用也可以使用断开投播的接口,主动进行投播连接的断开。 + +```ts +async release() { + // 一般来说,应用退出时,而不希望继续投播,可以主动结束 + await global.avsession.stopCast(); +} +``` \ No newline at end of file diff --git a/zh-cn/application-dev/reference/apis/js-apis-avcastpicker.md b/zh-cn/application-dev/reference/apis/js-apis-avcastpicker.md new file mode 100644 index 0000000000000000000000000000000000000000..8b8402a2c8685da109ba06c1a90e6b4f31b5d1d3 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-avcastpicker.md @@ -0,0 +1,37 @@ +# @ohos.avcastpicker (AVCastPicker) + +本模块提供创建投播组件的功能,提供了设备发现连接的统一入口。 + +> **说明:** +> +> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 +> +> 示例效果请以真机运行为准,当前IDE预览器不支持。 + +## 导入模块 + +```js +import { AVCastPicker} from '@ohos.avcastpicker'; +``` + +**系统能力:** SystemCapability.Multimedia.AVSession + +**示例:** +```ts +import { AVCastPicker } from '@ohos.avcastpicker' + +@Entry +@Component +struct Index { + build() { + Row() { + Column() { + AVCastPicker() + .width('40vp') + .height('40vp') + .border({ width: 1, color: Color.Red }) + }.height('50%') + }.width('50%') + } +} +``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-avsession.md b/zh-cn/application-dev/reference/apis/js-apis-avsession.md index 0ff9dd6a4e03ee9ce3d144b81bfc117e874814ed..8af6aa99c8fa547f76162f172da2a08ee5ab13dd 100755 --- a/zh-cn/application-dev/reference/apis/js-apis-avsession.md +++ b/zh-cn/application-dev/reference/apis/js-apis-avsession.md @@ -6,6 +6,7 @@ - [AVSession](#avsession10) : 会话,可用于设置元数据、播放状态信息等操作。 - [AVSessionController](#avsessioncontroller10): 会话控制器,可用于查看会话ID,完成对会话发送命令及事件,获取会话元数据、播放状态信息等操作。 +- [AVCastController](#avcastcontroller10): 投播控制器,可用于投播场景下,完成播放控制、远端播放状态监听、远端播放状态信息获取等操作。 > **说明:** > @@ -853,43 +854,104 @@ avSession.sendSystemControlCommand(avcommand, function (err) { }); ``` -## AVSession10+ +## avSession.startCastDeviceDiscovery -调用[avSession.createAVSession](#avsessioncreateavsession10)后,返回会话的实例,可以获得会话ID,完成设置元数据,播放状态信息等操作。 +startCastDeviceDiscovery(callback: AsyncCallback): void -### 属性 +开始设备搜索发现。结果通过callback异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 -| 名称 | 类型 | 可读 | 可写 | 说明 | -| :-------- | :----- | :--- | :--- | :---------------------------- | -| sessionId | string | 是 | 否 | AVSession对象唯一的会话标识。 | +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**系统接口:** 该接口为系统接口。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------------- | +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | **示例:** + ```js -let sessionId = session.sessionId; +avSession.startCastDeviceDiscovery(function (err) { + if (err) { + console.info(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`startCastDeviceDiscovery successfully`); + } +}); ``` -### setAVMetadata10+ +## avSession.startCastDeviceDiscovery -setAVMetadata(data: AVMetadata): Promise\ +startCastDeviceDiscovery(filter: number, callback: AsyncCallback): void -设置会话元数据。结果通过Promise异步回调方式返回。 +开始设备搜索发现。结果通过callback异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**系统接口:** 该接口为系统接口。 **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------ | ------------------------- | ---- | ------------ | -| data | [AVMetadata](#avmetadata10) | 是 | 会话元数据。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------------- | +| filter | number | 是 | 进行设备发现的过滤条件,由ProtocolType的组合而成 | +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | -**返回值:** +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | + +**示例:** + +```js +let filter = 2; +avSession.startCastDeviceDiscovery(filter, function (err) { + if (err) { + console.info(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`startCastDeviceDiscovery successfully`); + } +}); +``` + +## avSession.startCastDeviceDiscovery + +startCastDeviceDiscovery(filter?: number): Promise + +开始设备搜索发现。结果通过Promise异步回调方式返回。 + +**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast +**系统接口:** 该接口为系统接口。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------------- | +| filter | number | 否 | 进行设备发现的过滤条件,由ProtocolType的组合而成 | + +**返回值:** | 类型 | 说明 | | -------------- | ----------------------------- | -| Promise\ | Promise对象。当元数据设置成功,无返回结果,否则返回错误对象。 | +| Promise\ | Promise对象。当开始设备搜索成功,无返回结果,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -897,48 +959,34 @@ setAVMetadata(data: AVMetadata): Promise\ | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | **示例:** ```js -let metadata = { - 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", - lyric: "https://www.example.com/example.lrc", - previousAssetId: "121277", - nextAssetId: "121279", -}; -session.setAVMetadata(metadata).then(() => { - console.info(`SetAVMetadata successfully`); +let filter = 2; +avSession.startCastDeviceDiscovery(filter).then(() => { + console.info(`startCastDeviceDiscovery successfully`); }).catch((err) => { - console.info(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### setAVMetadata10+ +## avSession.on('deviceAvailable') -setAVMetadata(data: AVMetadata, callback: AsyncCallback\): void +on(type: 'deviceAvailable', callback: (device: OutputDeviceInfo) => void): void -设置会话元数据。结果通过callback异步回调方式返回。 +设备发现回调监听。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**系统接口:** 该接口为系统接口 **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------- | ---- | ------------------------------------- | -| data | [AVMetadata](#avmetadata10) | 是 | 会话元数据。 | -| callback | AsyncCallback\ | 是 | 回调函数。当元数据设置成功,err为undefined,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------- | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'deviceAvailable'`,有设备更新时触发回调。 | +| callback | (device: OutputDeviceInfo) => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -946,55 +994,30 @@ setAVMetadata(data: AVMetadata, callback: AsyncCallback\): void | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | **示例:** ```js -let metadata = { - 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", - lyric: "https://www.example.com/example.lrc", - previousAssetId: "121277", - nextAssetId: "121279", -}; -session.setAVMetadata(metadata, function (err) { - if (err) { - console.info(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SetAVMetadata successfully`); - } +avSession.on('deviceAvailable', (device) => { + console.info(`on deviceAvailable : ${device} `); }); ``` -### setAVPlaybackState10+ - -setAVPlaybackState(state: AVPlaybackState): Promise\ +## avSession.off('deviceAvailable') -设置会话播放状态。结果通过Promise异步回调方式返回。 +off(type: 'deviceAvailable', callback?: (device: OutputDeviceInfo) => void): void -**系统能力:** SystemCapability.Multimedia.AVSession.Core +取消设备发现回调的监听。 -**参数:** +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast -| 参数名 | 类型 | 必填 | 说明 | -| ------ | ----------------------------------- | ---- | ---------------------------------------------- | -| data | [AVPlaybackState](#avplaybackstate10) | 是 | 会话播放状态,包括状态、倍数、循环模式等信息。 | +**系统接口:** 该接口为系统接口 -**返回值:** +**参数:** -| 类型 | 说明 | -| -------------- | ----------------------------- | -| Promise\ | Promise对象。当播放状态设置成功,无返回结果,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ---------------------- | ---- | ------------------------------------------------------- | +| type | string | 是 | 事件回调类型,支持事件`'deviceAvailable'`:设备发现回调。| **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1002,40 +1025,30 @@ setAVPlaybackState(state: AVPlaybackState): Promise\ | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | **示例:** ```js -let playbackState = { - 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, -}; -session.setAVPlaybackState(playbackState).then(() => { - console.info(`SetAVPlaybackState successfully`); -}).catch((err) => { - console.info(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); -}); +avSession.off('deviceAvailable'); ``` -### setAVPlaybackState10+ +## avSession.stopCastDeviceDiscovery -setAVPlaybackState(state: AVPlaybackState, callback: AsyncCallback\): void +stopCastDeviceDiscovery(callback: AsyncCallback): void -设置会话播放状态。结果通过callback异步回调方式返回。 +结束设备搜索发现。结果通过callback异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**系统接口:** 该接口为系统接口。 **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------------------- | ---- | ---------------------------------------------- | -| data | [AVPlaybackState](#avplaybackstate10) | 是 | 会话播放状态,包括状态、倍数、循环模式等信息。 | -| callback | AsyncCallback\ | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------------- | +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1043,47 +1056,36 @@ setAVPlaybackState(state: AVPlaybackState, callback: AsyncCallback\): void | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | **示例:** ```js -let PlaybackState = { - 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, -}; -session.setAVPlaybackState(PlaybackState, function (err) { +avSession.stopCastDeviceDiscovery(function (err) { if (err) { - console.info(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`stopCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); } else { - console.info(`SetAVPlaybackState successfully`); + console.info(`stopCastDeviceDiscovery successfully`); } }); ``` -### setAVQueueItems10+ +## avSession.stopCastDeviceDiscovery -setAVQueueItems(items: Array\): Promise\ +stopCastDeviceDiscovery(): Promise -设置媒体播放列表。结果通过Promise异步回调方式返回。 +结束设备搜索发现。结果通过Promise异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 -**参数:** +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast -| 参数名 | 类型 | 必填 | 说明 | -| ------ | ------------------------------------ | ---- | ---------------------------------- | -| items | Array<[AVQueueItem](#avqueueitem10)\> | 是 | 播放列表单项的队列,用以表示播放列表。 | +**系统接口:** 该接口为系统接口。 **返回值:** | 类型 | 说明 | | -------------- | ----------------------------- | -| Promise\ | Promise对象。当播放列表设置成功,无返回结果,否则返回错误对象。 | +| Promise\ | Promise对象。当停止搜索成功,无返回结果,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1091,65 +1093,36 @@ setAVQueueItems(items: Array\): Promise\ | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | **示例:** ```js -import image from '@ohos.multimedia.image'; -import resourceManager from '@ohos.resourceManager'; - -let value : Uint8Array = await resourceManager.getRawFile('IMAGE_URI'); -let imageSource : imageImageSource = image.createImageSource(value.buffer); -let imagePixel : image.PixelMap = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); -let queueItemDescription_1 = { - mediaId: '001', - title: 'music_name', - subtitle: 'music_sub_name', - description: 'music_description', - icon : imagePixel, - iconUri: 'http://www.icon.uri.com', - extras: {'extras':'any'} -}; -let queueItem_1 = { - itemId: 1, - description: queueItemDescription_1 -}; -let queueItemDescription_2 = { - mediaId: '002', - title: 'music_name', - subtitle: 'music_sub_name', - description: 'music_description', - icon: imagePixel, - iconUri: 'http://www.xxx.com', - extras: {'extras':'any'} -}; -let queueItem_2 = { - itemId: 2, - description: queueItemDescription_2 -}; -let queueItemsArray = [queueItem_1, queueItem_2]; -session.setAVQueueItems(queueItemsArray).then(() => { - console.info(`SetAVQueueItems successfully`); +avSession.stopCastDeviceDiscovery().then(() => { + console.info(`startCastDeviceDiscovery successfully`); }).catch((err) => { - console.info(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### setAVQueueItems10+ +## avSession.startCasting -setAVQueueItems(items: Array\, callback: AsyncCallback\): void +startCasting(session: SessionToken, device: OutputDeviceInfo, callback: AsyncCallback): void -设置媒体播放列表。结果通过callback异步回调方式返回。 +启动投播。结果通过callback异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**系统接口:** 该接口为系统接口。 **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------ | ---- | ----------------------------------------------------------- | -| items | Array<[AVQueueItem](#avqueueitem10)\> | 是 | 播放列表单项的队列,用以表示播放列表。 | -| callback | AsyncCallback\ | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------------- | +| session | [SessionToken](#sessiontoken) | 'all' | 是 | 会话令牌。SessionToken表示单个token;字符串`'all'`指所有token。 | +| outputDevice | [OutputDeviceInfo](#outputdeviceinfo10) | 是 | 设备相关信息 | +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1157,72 +1130,51 @@ setAVQueueItems(items: Array\, callback: AsyncCallback\): voi | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | **示例:** ```js -import image from '@ohos.multimedia.image'; -import resourceManager from '@ohos.resourceManager'; - -let value : Uint8Array = await resourceManager.getRawFile('IMAGE_URI'); -let imageSource : imageImageSource = image.createImageSource(value.buffer); -let imagePixel : image.PixelMap = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); -let queueItemDescription_1 = { - mediaId: '001', - title: 'music_name', - subtitle: 'music_sub_name', - description: 'music_description', - icon: imagePixel, - iconUri: 'http://www.icon.uri.com', - extras: {'extras':'any'} -}; -let queueItem_1 = { - itemId: 1, - description: queueItemDescription_1 -}; -let queueItemDescription_2 = { - mediaId: '002', - title: 'music_name', - subtitle: 'music_sub_name', - description: 'music_description', - icon: imagePixel, - iconUri: 'http://www.icon.uri.com', - extras: {'extras':'any'} -}; -let queueItem_2 = { - itemId: 2, - description: queueItemDescription_2 -}; -let queueItemsArray = [queueItem_1, queueItem_2]; -session.setAVQueueItems(queueItemsArray, function (err) { +let castDevice; +avSession.on('deviceAvailable', (device) => { + castDevice = device; + console.info(`on deviceAvailable : ${device} `); +}); +let myToken = { + sessionId: avSession.sessionId; +} +avSession.startCasting(myToken, castDevice, function (err) { if (err) { - console.info(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`startCasting BusinessError: code: ${err.code}, message: ${err.message}`); } else { - console.info(`SetAVQueueItems successfully`); + console.info(`startCasting successfully`); } }); ``` -### setAVQueueTitle10+ +## avSession.startCasting -setAVQueueTitle(title: string): Promise\ +startCasting(session: SessionToken, device: OutputDeviceInfo): Promise -设置媒体播放列表名称。结果通过Promise异步回调方式返回。 +启动投播。结果通过Promise异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**系统接口:** 该接口为系统接口。 **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------ | ------ | ---- | -------------- | -| title | string | 是 | 播放列表的名称。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------------- | +| session | [SessionToken](#sessiontoken) | 'all' | 是 | 会话令牌。SessionToken表示单个token;字符串`'all'`指所有token。 | +| outputDevice | [OutputDeviceInfo](#outputdeviceinfo10) | 是 | 设备相关信息 | **返回值:** | 类型 | 说明 | | -------------- | ----------------------------- | -| Promise\ | Promise对象。当播放列表设置成功,无返回结果,否则返回错误对象。 | +| Promise\ | Promise对象。当停止搜索成功,无返回结果,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1230,33 +1182,43 @@ setAVQueueTitle(title: string): Promise\ | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | **示例:** ```js -let queueTitle = 'QUEUE_TITLE'; -session.setAVQueueTitle(queueTitle).then(() => { - console.info(`SetAVQueueTitle successfully`); +let castDevice; +avSession.on('deviceAvailable', (device) => { + castDevice = device; + console.info(`on deviceAvailable : ${device} `); +}); +let myToken = { + sessionId: avSession.sessionId; +} +avSession.startCasting(myToken, castDevice).then(() => { + console.info(`startCasting successfully`); }).catch((err) => { - console.info(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`startCasting BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### setAVQueueTitle10+ +## avSession.stopCasting -setAVQueueTitle(title: string, callback: AsyncCallback\): void +stopCasting(session: SessionToken, callback: AsyncCallback): void -设置媒体播放列表名称。结果通过callback异步回调方式返回。 +结束投播。结果通过callback异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**系统接口:** 该接口为系统接口。 **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | --------------------- | ---- | ----------------------------------------------------------- | -| title | string | 是 | 播放列表名称字段。 | -| callback | AsyncCallback\ | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------------- | +| session | [SessionToken](#sessiontoken) | 'all' | 是 | 会话令牌。SessionToken表示单个token;字符串`'all'`指所有token。 | +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1264,40 +1226,1536 @@ setAVQueueTitle(title: string, callback: AsyncCallback\): void | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | **示例:** ```js -let queueTitle = 'QUEUE_TITLE'; -session.setAVQueueTitle(queueTitle, function (err) { +let myToken = { + sessionId: avSession.sessionId; +} +avSession.stopCasting(myToken, castDevice, function (err) { if (err) { - console.info(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); } else { - console.info(`SetAVQueueTitle successfully`); + console.info(`stopCasting successfully`); } }); ``` -### setLaunchAbility10+ +## avSession.stopCasting -setLaunchAbility(ability: WantAgent): Promise\ +stopCasting(session: SessionToken): Promise -设置一个WantAgent用于拉起会话的Ability。结果通过Promise异步回调方式返回。 +结束投播。结果通过Promise异步回调方式返回。 + +**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**系统接口:** 该接口为系统接口。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------------- | +| session | [SessionToken](#sessiontoken) | 'all' | 是 | 会话令牌。SessionToken表示单个token;字符串`'all'`指所有token。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当停止搜索成功,无返回结果,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | + +**示例:** + +```js +let myToken = { + sessionId: avSession.sessionId; +} +avSession.stopCasting(myToken).then(() => { + console.info(`stopCasting successfully`); +}).catch((err) => { + console.info(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + + +## avSession.setDiscoverable + +setDiscoverable(enable: boolean, callback: AsyncCallback): void + +设置设备是否可被发现,用于投播接收端。结果通过callback异步回调方式返回。 + +**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**系统接口:** 该接口为系统接口。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------------- | +| enable | boolean | 是 | 是否允许本设备被发现. true: 允许被发现, false:不允许被发现 | +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | + +**示例:** + +```js +avSession.setDiscoverable(true, function (err) { + if (err) { + console.info(`setDiscoverable BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`setDiscoverable successfully`); + } +}); +``` + +## avSession.setDiscoverable + +setDiscoverable(enable: boolean): Promise + +设置设备是否可被发现,用于投播接收端。结果通过Promise异步回调方式返回。 + +**需要权限:** ohos.permission.MANAGE_MEDIA_RESOURCES,仅系统应用可用。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**系统接口:** 该接口为系统接口。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------------- | +| enable | boolean | 是 | 是否允许本设备被发现. true: 允许被发现, false:不允许被发现 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当停止搜索成功,无返回结果,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | + +**示例:** + +```js +avSession.setDiscoverable(true).then(() => { + console.info(`setDiscoverable successfully`); +}).catch((err) => { + console.info(`setDiscoverable BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + +## AVSession10+ + +调用[avSession.createAVSession](#avsessioncreateavsession10)后,返回会话的实例,可以获得会话ID,完成设置元数据,播放状态信息等操作。 + +### 属性 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +| 名称 | 类型 | 可读 | 可写 | 说明 | +| :-------- | :----- | :--- | :--- | :---------------------------- | +| sessionId | string | 是 | 否 | AVSession对象唯一的会话标识。 | + + +**示例:** +```js +let sessionId = session.sessionId; +``` + +### setAVMetadata10+ + +setAVMetadata(data: AVMetadata): Promise\ + +设置会话元数据。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | -| ability | [WantAgent](js-apis-app-ability-wantAgent.md) | 是 | 应用的相关属性信息,如bundleName,abilityName,deviceId等。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------------------------- | ---- | ------------ | +| data | [AVMetadata](#avmetadata10) | 是 | 会话元数据。 | **返回值:** | 类型 | 说明 | | -------------- | ----------------------------- | -| Promise\ | Promise对象。当Ability设置成功,无返回结果,否则返回错误对象。 | +| Promise\ | Promise对象。当元数据设置成功,无返回结果,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +let metadata = { + 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", + lyric: "https://www.example.com/example.lrc", + previousAssetId: "121277", + nextAssetId: "121279", +}; +session.setAVMetadata(metadata).then(() => { + console.info(`SetAVMetadata successfully`); +}).catch((err) => { + console.info(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + +### setAVMetadata10+ + +setAVMetadata(data: AVMetadata, callback: AsyncCallback\): void + +设置会话元数据。结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------- | ---- | ------------------------------------- | +| data | [AVMetadata](#avmetadata10) | 是 | 会话元数据。 | +| callback | AsyncCallback\ | 是 | 回调函数。当元数据设置成功,err为undefined,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +let metadata = { + 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", + lyric: "https://www.example.com/example.lrc", + previousAssetId: "121277", + nextAssetId: "121279", +}; +session.setAVMetadata(metadata, function (err) { + if (err) { + console.info(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SetAVMetadata successfully`); + } +}); +``` + +### setAVPlaybackState10+ + +setAVPlaybackState(state: AVPlaybackState): Promise\ + +设置会话播放状态。结果通过Promise异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ----------------------------------- | ---- | ---------------------------------------------- | +| data | [AVPlaybackState](#avplaybackstate10) | 是 | 会话播放状态,包括状态、倍数、循环模式等信息。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当播放状态设置成功,无返回结果,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +let playbackState = { + 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, +}; +session.setAVPlaybackState(playbackState).then(() => { + console.info(`SetAVPlaybackState successfully`); +}).catch((err) => { + console.info(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + +### setAVPlaybackState10+ + +setAVPlaybackState(state: AVPlaybackState, callback: AsyncCallback\): void + +设置会话播放状态。结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------------------- | ---- | ---------------------------------------------- | +| data | [AVPlaybackState](#avplaybackstate10) | 是 | 会话播放状态,包括状态、倍数、循环模式等信息。 | +| callback | AsyncCallback\ | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +let PlaybackState = { + 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, +}; +session.setAVPlaybackState(PlaybackState, function (err) { + if (err) { + console.info(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SetAVPlaybackState successfully`); + } +}); +``` + +### setAVQueueItems10+ + +setAVQueueItems(items: Array\): Promise\ + +设置媒体播放列表。结果通过Promise异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------------------------------------ | ---- | ---------------------------------- | +| items | Array<[AVQueueItem](#avqueueitem10)\> | 是 | 播放列表单项的队列,用以表示播放列表。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当播放列表设置成功,无返回结果,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +import image from '@ohos.multimedia.image'; +import resourceManager from '@ohos.resourceManager'; + +let value : Uint8Array = await resourceManager.getRawFile('IMAGE_URI'); +let imageSource : imageImageSource = image.createImageSource(value.buffer); +let imagePixel : image.PixelMap = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); +let queueItemDescription_1 = { + mediaId: '001', + title: 'music_name', + subtitle: 'music_sub_name', + description: 'music_description', + icon : imagePixel, + iconUri: 'http://www.icon.uri.com', + extras: {'extras':'any'} +}; +let queueItem_1 = { + itemId: 1, + description: queueItemDescription_1 +}; +let queueItemDescription_2 = { + mediaId: '002', + title: 'music_name', + subtitle: 'music_sub_name', + description: 'music_description', + icon: imagePixel, + iconUri: 'http://www.xxx.com', + extras: {'extras':'any'} +}; +let queueItem_2 = { + itemId: 2, + description: queueItemDescription_2 +}; +let queueItemsArray = [queueItem_1, queueItem_2]; +session.setAVQueueItems(queueItemsArray).then(() => { + console.info(`SetAVQueueItems successfully`); +}).catch((err) => { + console.info(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + +### setAVQueueItems10+ + +setAVQueueItems(items: Array\, callback: AsyncCallback\): void + +设置媒体播放列表。结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------ | ---- | ----------------------------------------------------------- | +| items | Array<[AVQueueItem](#avqueueitem10)\> | 是 | 播放列表单项的队列,用以表示播放列表。 | +| callback | AsyncCallback\ | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +import image from '@ohos.multimedia.image'; +import resourceManager from '@ohos.resourceManager'; + +let value : Uint8Array = await resourceManager.getRawFile('IMAGE_URI'); +let imageSource : imageImageSource = image.createImageSource(value.buffer); +let imagePixel : image.PixelMap = await imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}); +let queueItemDescription_1 = { + mediaId: '001', + title: 'music_name', + subtitle: 'music_sub_name', + description: 'music_description', + icon: imagePixel, + iconUri: 'http://www.icon.uri.com', + extras: {'extras':'any'} +}; +let queueItem_1 = { + itemId: 1, + description: queueItemDescription_1 +}; +let queueItemDescription_2 = { + mediaId: '002', + title: 'music_name', + subtitle: 'music_sub_name', + description: 'music_description', + icon: imagePixel, + iconUri: 'http://www.icon.uri.com', + extras: {'extras':'any'} +}; +let queueItem_2 = { + itemId: 2, + description: queueItemDescription_2 +}; +let queueItemsArray = [queueItem_1, queueItem_2]; +session.setAVQueueItems(queueItemsArray, function (err) { + if (err) { + console.info(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SetAVQueueItems successfully`); + } +}); +``` + +### setAVQueueTitle10+ + +setAVQueueTitle(title: string): Promise\ + +设置媒体播放列表名称。结果通过Promise异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------ | ---- | -------------- | +| title | string | 是 | 播放列表的名称。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当播放列表设置成功,无返回结果,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +let queueTitle = 'QUEUE_TITLE'; +session.setAVQueueTitle(queueTitle).then(() => { + console.info(`SetAVQueueTitle successfully`); +}).catch((err) => { + console.info(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + +### setAVQueueTitle10+ + +setAVQueueTitle(title: string, callback: AsyncCallback\): void + +设置媒体播放列表名称。结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------- | ---- | ----------------------------------------------------------- | +| title | string | 是 | 播放列表名称字段。 | +| callback | AsyncCallback\ | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +let queueTitle = 'QUEUE_TITLE'; +session.setAVQueueTitle(queueTitle, function (err) { + if (err) { + console.info(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SetAVQueueTitle successfully`); + } +}); +``` + +### setLaunchAbility10+ + +setLaunchAbility(ability: WantAgent): Promise\ + +设置一个WantAgent用于拉起会话的Ability。结果通过Promise异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | +| ability | [WantAgent](js-apis-app-ability-wantAgent.md) | 是 | 应用的相关属性信息,如bundleName,abilityName,deviceId等。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当Ability设置成功,无返回结果,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +import wantAgent from '@ohos.app.ability.wantAgent'; + +//WantAgentInfo对象 +let 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) => { + session.setLaunchAbility(agent).then(() => { + console.info(`SetLaunchAbility successfully`); + }).catch((err) => { + console.info(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); + }); +}); +``` + +### setLaunchAbility10+ + +setLaunchAbility(ability: WantAgent, callback: AsyncCallback\): void + +设置一个WantAgent用于拉起会话的Ability。结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | +| ability | [WantAgent](js-apis-app-ability-wantAgent.md) | 是 | 应用的相关属性信息,如bundleName,abilityName,deviceId等。 | +| callback | AsyncCallback\ | 是 | 回调函数。当Ability设置成功,err为undefined,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +import wantAgent from '@ohos.app.ability.wantAgent'; + +//WantAgentInfo对象 +let 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) => { + session.setLaunchAbility(agent, function (err) { + if (err) { + console.info(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SetLaunchAbility successfully`); + } + }); +}); +``` + +### dispatchSessionEvent10+ + +dispatchSessionEvent(event: string, args: {[key: string]: Object}): Promise\ + +媒体提供方设置一个会话内自定义事件,包括事件名和键值对形式的事件内容, 结果通过Promise异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | +| event | string | 是 | 需要设置的会话事件的名称 | +| args | {[key: string]: any} | 是 | 需要传递的会话事件键值对 | + +> **说明:** +> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当事件设置成功,无返回结果,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +let eventName = "dynamic_lyric"; +let args = { + lyric : "This is lyric" +} +await session.dispatchSessionEvent(eventName, args).catch((err) => { + console.info(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); +}) +``` + +### dispatchSessionEvent10+ + +dispatchSessionEvent(event: string, args: {[key: string]: Object}, callback: AsyncCallback\): void + +媒体提供方设置一个会话内自定义事件,包括事件名和键值对形式的事件内容, 结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | +| event | string | 是 | 需要设置的会话事件的名称 | +| args | {[key: string]: any} | 是 | 需要传递的会话事件键值对 | +| callback | AsyncCallback\ | 是 | 回调函数。当会话事件设置成功,err为undefined,否则返回错误对象。 | + +> **说明:** +> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +let eventName = "dynamic_lyric"; +let args = { + lyric : "This is lyric" +} +await session.dispatchSessionEvent(eventName, args, (err) => { + if(err) { + console.info(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); + } +}) +``` + +### setExtras10+ + +setExtras(extras: {[key: string]: Object}): Promise\ + +媒体提供方设置键值对形式的自定义媒体数据包, 结果通过Promise异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | +| extras | {[key: string]: Object} | 是 | 需要传递的自定义媒体数据包键值对 | + +> **说明:** +> 参数extras支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当自定义媒体数据包设置成功,无返回结果,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +let extras = { + extras : "This is custom media packet" +} +await session.setExtras(extras).catch((err) => { + console.info(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); +}) +``` + +### setExtras10+ + +setExtras(extras: {[key: string]: Object}, callback: AsyncCallback\): void + +媒体提供方设置键值对形式的自定义媒体数据包, 结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | +| extras | {[key: string]: any} | 是 | 需要传递的自定义媒体数据包键值对 | +| callback | AsyncCallback\ | 是 | 回调函数。当自定义媒体数据包设置成功,err为undefined,否则返回错误对象。 | + +> **说明:** +> 参数extras支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +let extras = { + extras : "This is custom media packet" +} +await session.setExtras(extras, (err) => { + if(err) { + console.info(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); + } +}) +``` + +### getController10+ + +getController(): Promise\ + +获取本会话对应的控制器。结果通过Promise异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**返回值:** + +| 类型 | 说明 | +| ---------------------------------------------------- | ----------------------------- | +| Promise<[AVSessionController](#avsessioncontroller10)> | Promise对象。返回会话控制器。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +let controller; +session.getController().then((avcontroller) => { + controller = avcontroller; + console.info(`GetController : SUCCESS : sessionid : ${controller.sessionId}`); +}).catch((err) => { + console.info(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + +### getController10+ + +getController(callback: AsyncCallback\): void + +获取本会话相应的控制器。结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------------------------------------------- | ---- | -------------------------- | +| callback | AsyncCallback<[AVSessionController](#avsessioncontroller10)\> | 是 | 回调函数。返回会话控制器。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +let controller; +session.getController(function (err, avcontroller) { + if (err) { + console.info(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + controller = avcontroller; + console.info(`GetController : SUCCESS : sessionid : ${controller.sessionId}`); + } +}); +``` + +### getAVCastController10+ + +getAVCastController(callback: AsyncCallback): void; + +设备建立连接后,获取投播控制器。结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | +| callback | AsyncCallback<[AVCastController](#avcastcontroller10)\> | 是 | 回调函数,返回投播控制器实例。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600102 | The session does not exist. | +| 6600110 | The remote connection is not established. | + +**示例:** + +```js +let controller; +session.getAVCastController().then((avcontroller) => { + controller = avcontroller; + console.info(`getAVCastController : SUCCESS : sessionid : ${controller.sessionId}`); +}).catch((err) => { + console.info(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + +### getAVCastController10+ + +getAVCastController(): Promise; + +设备建立连接后,获取投播控制器。结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**返回值:** + +| 类型 | 说明 | +| --------- | ------------------------------------------------------------ | +| Promise<[AVCastController](#avcastcontroller10)\> | Promise对象。返回投播控制器实例。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600102 | The session does not exist. | +| 6600110 | The remote connection is not established. | + +**示例:** + +```js +let controller; +session.getAVCastController(function (err, avcontroller) { + if (err) { + console.info(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + controller = avcontroller; + console.info(`getAVCastController : SUCCESS : sessionid : ${controller.sessionId}`); + } +}); +``` + +### stopCasting10+ + +stopCasting(callback: AsyncCallback): void; + +停止投播。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------------------------- | ---- | ------------ | +| callback | AsyncCallback\ | 是 | 回调函数。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.stopCasting(function (err) { + if (err) { + console.info(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + controller = avcontroller; + console.info(`GetController : SUCCESS : sessionid : ${controller.sessionId}`); + } +}); +``` + +### stopCasting10+ + +stopCasting(): Promise; + +停止投播。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当停止投播成功,无返回结果,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.stopCasting().then(() => { + console.info(`stopCasting successfully`); +}).catch((err) => { + console.info(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + + +### getOutputDevice10+ + +getOutputDevice(): Promise\ + +通过会话获取播放设备信息。结果通过Promise异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**返回值:** + +| 类型 | 说明 | +| ---------------------------------------------- | --------------------------------- | +| Promise<[OutputDeviceInfo](#outputdeviceinfo10)> | Promise对象。返回播放设备信息。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.getOutputDevice().then((outputDeviceInfo) => { + console.info(`GetOutputDevice : SUCCESS : isRemote : ${outputDeviceInfo.isRemote}`); +}).catch((err) => { + console.info(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + +### getOutputDevice10+ + +getOutputDevice(callback: AsyncCallback\): void + +通过会话获取播放设备相关信息。结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------------------------------------- | ---- | ------------------------------ | +| callback | AsyncCallback<[OutputDeviceInfo](#outputdeviceinfo10)\> | 是 | 回调函数,返回播放设备信息。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.getOutputDevice(function (err, outputDeviceInfo) { + if (err) { + console.info(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetOutputDevice : SUCCESS : isRemote : ${outputDeviceInfo.isRemote}`); + } +}); +``` + +### activate10+ + +activate(): Promise\ + +激活会话,激活后可正常使用会话。结果通过Promise异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当会话激活成功,无返回结果,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.activate().then(() => { + console.info(`Activate : SUCCESS `); +}).catch((err) => { + console.info(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + +### activate10+ + +activate(callback: AsyncCallback\): void + +激活会话,激活后可正常使用会话。结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------- | ---- | ---------- | +| callback | AsyncCallback\ | 是 | 回调函数。当会话激活成功,err为undefined,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.activate(function (err) { + if (err) { + console.info(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`Activate : SUCCESS `); + } +}); +``` + +### deactivate10+ + +deactivate(): Promise\ + +禁用当前会话的功能,可通过[activate](#activate10)恢复。结果通过Promise异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当禁用会话成功,无返回结果,否则返回错误对象。| + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.deactivate().then(() => { + console.info(`Deactivate : SUCCESS `); +}).catch((err) => { + console.info(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + +### deactivate10+ + +deactivate(callback: AsyncCallback\): void + +禁用当前会话。结果通过callback异步回调方式返回。 + +禁用当前会话的功能,可通过[activate](#activate10)恢复。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------- | ---- | ---------- | +| callback | AsyncCallback\ | 是 | 回调函数。当禁用会话成功,err为undefined,否则返回错误对象。| + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.deactivate(function (err) { + if (err) { + console.info(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`Deactivate : SUCCESS `); + } +}); +``` + +### destroy10+ + +destroy(): Promise\ + +销毁当前会话,使当前会话完全失效。结果通过Promise异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当会话销毁成功,无返回结果,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.destroy().then(() => { + console.info(`Destroy : SUCCESS `); +}).catch((err) => { + console.info(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); +}); +``` + +### destroy10+ + +destroy(callback: AsyncCallback\): void + +销毁当前会话,使当前会话完全失效。结果通过callback异步回调方式返回。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------- | ---- | ---------- | +| callback | AsyncCallback\ | 是 | 回调函数。当会话销毁成功,err为undefined,否则返回错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.destroy(function (err) { + if (err) { + console.info(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`Destroy : SUCCESS `); + } +}); +``` + +### on('play'|'pause'|'stop'|'playNext'|'playPrevious'|'fastForward'|'rewind')10+ + +on(type: 'play'|'pause'|'stop'|'playNext'|'playPrevious'|'fastForward'|'rewind', callback: () => void): void + +设置播放命令监听事件。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------- | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持的事件包括:`'play'`,`'pause'`,`'stop'`,` 'playNext'`,` 'playPrevious'`, `'fastForward'`,` 'rewind'`。
当对应的播放命令被发送到会话时,触发该事件回调。 | +| callback | callback: () => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.on('play', () => { + console.info(`on play entry`); +}); +session.on('pause', () => { + console.info(`on pause entry`); +}); +session.on('stop', () => { + console.info(`on stop entry`); +}); +session.on('playNext', () => { + console.info(`on playNext entry`); +}); +session.on('playPrevious', () => { + console.info(`on playPrevious entry`); +}); +session.on('fastForward', () => { + console.info(`on fastForward entry`); +}); +session.on('rewind', () => { + console.info(`on rewind entry`); +}); +``` + +### on('seek')10+ + +on(type: 'seek', callback: (time: number) => void): void + +设置跳转节点监听事件。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------------------- | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'seek'`:当跳转节点命令被发送到会话时,触发该事件。 | +| callback | (time: number) => void | 是 | 回调函数。参数time是时间节点,单位为毫秒。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** +The session does not exist +```js +session.on('seek', (time) => { + console.info(`on seek entry time : ${time}`); +}); +``` + +### on('setSpeed')10+ + +on(type: 'setSpeed', callback: (speed: number) => void): void + +设置播放速率的监听事件。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'setSpeed'`:当设置播放速率的命令被发送到会话时,触发该事件。 | +| callback | (speed: number) => void | 是 | 回调函数。参数speed是播放倍速。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.on('setSpeed', (speed) => { + console.info(`on setSpeed speed : ${speed}`); +}); +``` + +### on('setLoopMode')10+ + +on(type: 'setLoopMode', callback: (mode: LoopMode) => void): void + +设置循环模式的监听事件。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ---- | +| type | string | 是 | 事件回调类型,支持事件`'setLoopMode'`:当设置循环模式的命令被发送到会话时,触发该事件。 | +| callback | (mode: [LoopMode](#loopmode10)) => void | 是 | 回调函数。参数mode是循环模式。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.on('setLoopMode', (mode) => { + console.info(`on setLoopMode mode : ${mode}`); +}); +``` + +### on('toggleFavorite')10+ + +on(type: 'toggleFavorite', callback: (assetId: string) => void): void + +设置是否收藏的监听事件 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------- | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'toggleFavorite'`:当是否收藏的命令被发送到会话时,触发该事件。 | +| callback | (assetId: string) => void | 是 | 回调函数。参数assetId是媒体ID。 | + +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 + +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | + +**示例:** + +```js +session.on('toggleFavorite', (assetId) => { + console.info(`on toggleFavorite mode : ${assetId}`); +}); +``` + +### on('skipToQueueItem')10+ + +on(type: 'skipToQueueItem', callback: (itemId: number) => void): void + +设置播放列表其中某项被选中的监听事件,session端可以选择对这个单项歌曲进行播放。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------ | ---- | ---------------------------------------------------------------------------------------- | +| type | string | 是 | 事件回调类型,支持事件`'skipToQueueItem'`:当播放列表选中单项的命令被发送到会话时,触发该事件。 | +| callback | (itemId: number) => void | 是 | 回调函数。参数itemId是选中的播放列表项的ID。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1310,59 +2768,25 @@ setLaunchAbility(ability: WantAgent): Promise\ **示例:** ```js -import wantAgent from '@ohos.app.ability.wantAgent'; - -//WantAgentInfo对象 -let 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) => { - session.setLaunchAbility(agent).then(() => { - console.info(`SetLaunchAbility successfully`); - }).catch((err) => { - console.info(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); - }); +session.on('skipToQueueItem', (itemId) => { + console.info(`on skipToQueueItem id : ${itemId}`); }); ``` -### setLaunchAbility10+ +### on('handleKeyEvent')10+ -setLaunchAbility(ability: WantAgent, callback: AsyncCallback\): void +on(type: 'handleKeyEvent', callback: (event: KeyEvent) => void): void -设置一个WantAgent用于拉起会话的Ability。结果通过callback异步回调方式返回。 +设置按键事件的监听 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | -| ability | [WantAgent](js-apis-app-ability-wantAgent.md) | 是 | 应用的相关属性信息,如bundleName,abilityName,deviceId等。 | -| callback | AsyncCallback\ | 是 | 回调函数。当Ability设置成功,err为undefined,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'handleKeyEvent'`:当按键事件被发送到会话时,触发该事件。 | +| callback | (event: [KeyEvent](js-apis-keyevent.md)) => void | 是 | 回调函数。参数event是按键事件。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1375,70 +2799,25 @@ setLaunchAbility(ability: WantAgent, callback: AsyncCallback\): void **示例:** ```js -import wantAgent from '@ohos.app.ability.wantAgent'; - -//WantAgentInfo对象 -let 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) => { - session.setLaunchAbility(agent, function (err) { - if (err) { - console.info(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SetLaunchAbility successfully`); - } - }); +session.on('handleKeyEvent', (event) => { + console.info(`on handleKeyEvent event : ${event}`); }); ``` -### dispatchSessionEvent10+ +### on('outputDeviceChange')10+ -dispatchSessionEvent(event: string, args: {[key: string]: Object}): Promise\ +on(type: 'outputDeviceChange', callback: (device: OutputDeviceInfo) => void): void -媒体提供方设置一个会话内自定义事件,包括事件名和键值对形式的事件内容, 结果通过Promise异步回调方式返回。 +设置播放设备变化的监听事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | -| event | string | 是 | 需要设置的会话事件的名称 | -| args | {[key: string]: any} | 是 | 需要传递的会话事件键值对 | - -> **说明:** -> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 - -**返回值:** - -| 类型 | 说明 | -| -------------- | ----------------------------- | -| Promise\ | Promise对象。当事件设置成功,无返回结果,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'outputDeviceChange'`:当播放设备变化时,触发该事件。 | +| callback | (device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | 是 | 回调函数。参数device是设备相关信息。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1451,78 +2830,56 @@ dispatchSessionEvent(event: string, args: {[key: string]: Object}): Promise\ { - console.info(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); -}) +session.on('outputDeviceChange', (device) => { + console.info(`on outputDeviceChange device isRemote : ${device.isRemote}`); +}); ``` -### dispatchSessionEvent10+ +### on('commonCommand')10+ -dispatchSessionEvent(event: string, args: {[key: string]: Object}, callback: AsyncCallback\): void +on(type: 'commonCommand', callback: (command: string, args: {[key: string]: Object}) => void): void -媒体提供方设置一个会话内自定义事件,包括事件名和键值对形式的事件内容, 结果通过callback异步回调方式返回。 +设置自定义控制命令变化的监听器。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | -| event | string | 是 | 需要设置的会话事件的名称 | -| args | {[key: string]: any} | 是 | 需要传递的会话事件键值对 | -| callback | AsyncCallback\ | 是 | 回调函数。当会话事件设置成功,err为undefined,否则返回错误对象。 | - -> **说明:** -> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'commonCommand'`:当自定义控制命令变化时,触发该事件。 | +| callback | (commonCommand: string, args: {[key:string]: Object}) => void | 是 | 回调函数,commonCommand为变化的自定义控制命令名,args为自定义控制命令的参数,参数内容与sendCommand方法设置的参数内容完全一致。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ------------------------------ | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | **示例:** ```js -let eventName = "dynamic_lyric"; -let args = { - lyric : "This is lyric" -} -await session.dispatchSessionEvent(eventName, args, (err) => { - if(err) { - console.info(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); - } -}) +session.on('commonCommand', (commonCommand, args) => { + console.info(`OnCommonCommand, the command is ${commonCommand}, args: ${JSON.stringify(args)}`); +}); ``` -### setExtras10+ +### off('play'|'pause'|'stop'|'playNext'|'playPrevious'|'fastForward'|'rewind')10+ -setExtras(extras: {[key: string]: Object}): Promise\ +off(type: 'play' | 'pause' | 'stop' | 'playNext' | 'playPrevious' | 'fastForward' | 'rewind', callback?: () => void): void -媒体提供方设置键值对形式的自定义媒体数据包, 结果通过Promise异步回调方式返回。 +取消会话相关事件监听,关闭后,不再进行相关事件回调。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | -| extras | {[key: string]: Object} | 是 | 需要传递的自定义媒体数据包键值对 | - -> **说明:** -> 参数extras支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 - -**返回值:** - -| 类型 | 说明 | -| -------------- | ----------------------------- | -| Promise\ | Promise对象。当自定义媒体数据包设置成功,无返回结果,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | +| type | string | 是 | 关闭对应的监听事件,支持的事件包括:`'play'`,` 'pause'`,`'stop'`, `'playNext'`,` 'playPrevious'`, ` 'fastForward'`,` 'rewind'`。 | +| callback | callback: () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1535,31 +2892,29 @@ setExtras(extras: {[key: string]: Object}): Promise\ **示例:** ```js -let extras = { - extras : "This is custom media packet" -} -await session.setExtras(extras).catch((err) => { - console.info(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); -}) +session.off('play'); +session.off('pause'); +session.off('stop'); +session.off('playNext'); +session.off('playPrevious'); +session.off('fastForward'); +session.off('rewind'); ``` -### setExtras10+ +### off('seek')10+ -setExtras(extras: {[key: string]: Object}, callback: AsyncCallback\): void +off(type: 'seek', callback?: (time: number) => void): void -媒体提供方设置键值对形式的自定义媒体数据包, 结果通过callback异步回调方式返回。 +取消监听跳转节点事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------- | --------------------------------------------- | ---- | ----------------------------------------------------------- | -| extras | {[key: string]: any} | 是 | 需要传递的自定义媒体数据包键值对 | -| callback | AsyncCallback\ | 是 | 回调函数。当自定义媒体数据包设置成功,err为undefined,否则返回错误对象。 | - -> **说明:** -> 参数extras支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------------------- | ---- | ----------------------------------------- | +| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'seek'`。 | +| callback | (time: number) => void | 否 | 回调函数,参数time是时间节点,单位为毫秒。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1572,29 +2927,23 @@ setExtras(extras: {[key: string]: Object}, callback: AsyncCallback\): void **示例:** ```js -let extras = { - extras : "This is custom media packet" -} -await session.setExtras(extras, (err) => { - if(err) { - console.info(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); - } -}) +session.off('seek'); ``` -### getController10+ +### off('setSpeed')10+ -getController(): Promise\ +off(type: 'setSpeed', callback?: (speed: number) => void): void -获取本会话对应的控制器。结果通过Promise异步回调方式返回。 +取消监听播放速率变化事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**返回值:** +**参数:** -| 类型 | 说明 | -| ---------------------------------------------------- | ----------------------------- | -| Promise<[AVSessionController](#avsessioncontroller10)> | Promise对象。返回会话控制器。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------------| +| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'setSpeed'`。 | +| callback | (speed: number) => void | 否 | 回调函数,参数speed是播放倍速。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1607,28 +2956,23 @@ getController(): Promise\ **示例:** ```js -let controller; -session.getController().then((avcontroller) => { - controller = avcontroller; - console.info(`GetController : SUCCESS : sessionid : ${controller.sessionId}`); -}).catch((err) => { - console.info(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); -}); +session.off('setSpeed'); ``` -### getController10+ +### off('setLoopMode')10+ -getController(callback: AsyncCallback\): void +off(type: 'setLoopMode', callback?: (mode: LoopMode) => void): void -获取本会话相应的控制器。结果通过callback异步回调方式返回。 +取消监听循环模式变化事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------------------------------------------- | ---- | -------------------------- | -| callback | AsyncCallback<[AVSessionController](#avsessioncontroller10)\> | 是 | 回调函数。返回会话控制器。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ----- | +| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'setLoopMode'`。| +| callback | (mode: [LoopMode](#loopmode10)) => void | 否 | 回调函数,参数mode是循环模式。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1641,30 +2985,23 @@ getController(callback: AsyncCallback\): void **示例:** ```js -let controller; -session.getController(function (err, avcontroller) { - if (err) { - console.info(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - controller = avcontroller; - console.info(`GetController : SUCCESS : sessionid : ${controller.sessionId}`); - } -}); +session.off('setLoopMode'); ``` -### getOutputDevice10+ +### off('toggleFavorite')10+ -getOutputDevice(): Promise\ +off(type: 'toggleFavorite', callback?: (assetId: string) => void): void -通过会话获取播放设备信息。结果通过Promise异步回调方式返回。 +取消监听是否收藏的事件 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**返回值:** +**参数:** -| 类型 | 说明 | -| ---------------------------------------------- | --------------------------------- | -| Promise<[OutputDeviceInfo](#outputdeviceinfo10)> | Promise对象。返回播放设备信息。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------- | ---- | -------------------------------------------------------- | +| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'toggleFavorite'`。 | +| callback | (assetId: string) => void | 否 | 回调函数,参数assetId是媒体ID。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1677,26 +3014,23 @@ getOutputDevice(): Promise\ **示例:** ```js -session.getOutputDevice().then((outputDeviceInfo) => { - console.info(`GetOutputDevice : SUCCESS : isRemote : ${outputDeviceInfo.isRemote}`); -}).catch((err) => { - console.info(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); -}); +session.off('toggleFavorite'); ``` -### getOutputDevice10+ +### off('skipToQueueItem')10+ -getOutputDevice(callback: AsyncCallback\): void +off(type: 'skipToQueueItem', callback?: (itemId: number) => void): void -通过会话获取播放设备相关信息。结果通过callback异步回调方式返回。 +取消监听播放列表单项选中的事件 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------------------------------------- | ---- | ------------------------------ | -| callback | AsyncCallback<[OutputDeviceInfo](#outputdeviceinfo10)\> | 是 | 回调函数,返回播放设备信息。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------ | ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | +| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'skipToQueueItem'`。 | +| callback | (itemId: number) => void | 否 | 回调函数,参数itemId是播放列表单项ID。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1709,28 +3043,23 @@ getOutputDevice(callback: AsyncCallback\): void **示例:** ```js -session.getOutputDevice(function (err, outputDeviceInfo) { - if (err) { - console.info(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`GetOutputDevice : SUCCESS : isRemote : ${outputDeviceInfo.isRemote}`); - } -}); +session.off('skipToQueueItem'); ``` -### activate10+ +### off('handleKeyEvent')10+ -activate(): Promise\ +off(type: 'handleKeyEvent', callback?: (event: KeyEvent) => void): void -激活会话,激活后可正常使用会话。结果通过Promise异步回调方式返回。 +取消监听按键事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**返回值:** +**参数:** -| 类型 | 说明 | -| -------------- | ----------------------------- | -| Promise\ | Promise对象。当会话激活成功,无返回结果,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | +| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'handleKeyEvent'`。 | +| callback | (event: [KeyEvent](js-apis-keyevent.md)) => void | 否 | 回调函数,参数event是按键事件。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1743,26 +3072,23 @@ activate(): Promise\ **示例:** ```js -session.activate().then(() => { - console.info(`Activate : SUCCESS `); -}).catch((err) => { - console.info(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); -}); +session.off('handleKeyEvent'); ``` -### activate10+ +### off('outputDeviceChange')10+ -activate(callback: AsyncCallback\): void +off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: OutputDeviceInfo) => void): void -激活会话,激活后可正常使用会话。结果通过callback异步回调方式返回。 +取消监听播放设备变化的事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | -------------------- | ---- | ---------- | -| callback | AsyncCallback\ | 是 | 回调函数。当会话激活成功,err为undefined,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ | +| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'outputDeviceChange'`。 | +| callback | (state: [ConnectionState](#connectionstate10), device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | 否 | 回调函数,参数device是设备相关信息。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1775,96 +3101,77 @@ activate(callback: AsyncCallback\): void **示例:** ```js -session.activate(function (err) { - if (err) { - console.info(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`Activate : SUCCESS `); - } -}); +session.off('outputDeviceChange'); ``` -### deactivate10+ -deactivate(): Promise\ +### off('commonCommand')10+ -禁用当前会话的功能,可通过[activate](#activate10)恢复。结果通过Promise异步回调方式返回。 +off(type: 'commonCommand', callback?: (command: string, args: {[key:string]: Object}) => void): void + +取消监听自定义控制命令的变化。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**返回值:** +**参数:** -| 类型 | 说明 | -| -------------- | ----------------------------- | -| Promise\ | Promise对象。当禁用会话成功,无返回结果,否则返回错误对象。| +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'commonCommand'`。 | +| callback | (command: string, args: {[key:string]: Object}) => void | 否 | 回调函数,参数command是变化的自定义控制命令名,args为自定义控制命令的参数。
该参数为可选参数,若不填写该参数,则认为取消所有对command事件的监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ---------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | **示例:** ```js -session.deactivate().then(() => { - console.info(`Deactivate : SUCCESS `); -}).catch((err) => { - console.info(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); -}); +session.off('commonCommand'); ``` -### deactivate10+ - -deactivate(callback: AsyncCallback\): void -禁用当前会话。结果通过callback异步回调方式返回。 -禁用当前会话的功能,可通过[activate](#activate10)恢复。 +## AVSessionController10+ -**系统能力:** SystemCapability.Multimedia.AVSession.Core +调用[avSession.createController](#avsessioncreatecontroller)后,返回会话控制器实例。控制器可查看会话ID,并可完成对会话发送命令及事件,获取会话元数据,播放状态信息等操作。 -**参数:** +### 属性 -| 参数名 | 类型 | 必填 | 说明 | -| -------- | -------------------- | ---- | ---------- | -| callback | AsyncCallback\ | 是 | 回调函数。当禁用会话成功,err为undefined,否则返回错误对象。| +**系统能力:** SystemCapability.Multimedia.AVSession.Core -**错误码:** -以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 +| 名称 | 类型 | 可读 | 可写 | 说明 | +| :-------- | :----- | :--- | :--- | :-------------------------------------- | +| sessionId | string | 是 | 否 | AVSessionController对象唯一的会话标识。 | -| 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | -| 6600101 | Session service exception. | -| 6600102 | The session does not exist. | **示例:** - ```js -session.deactivate(function (err) { - if (err) { - console.info(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`Deactivate : SUCCESS `); - } +let sessionId; +await avSession.createController(session.sessionId).then((controller) => { + sessionId = controller.sessionId; +}).catch((err) => { + console.info(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### destroy10+ +### getAVPlaybackState10+ -destroy(): Promise\ +getAVPlaybackState(): Promise\ -销毁当前会话,使当前会话完全失效。结果通过Promise异步回调方式返回。 +获取当前会话播放状态相关信息。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **返回值:** -| 类型 | 说明 | -| -------------- | ----------------------------- | -| Promise\ | Promise对象。当会话销毁成功,无返回结果,否则返回错误对象。 | +| 类型 | 说明 | +| --------------------------------------------- | --------------------------- | +| Promise<[AVPlaybackState](#avplaybackstate10)\> | Promise对象。返回播放状态对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1873,30 +3180,30 @@ destroy(): Promise\ | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** - ```js -session.destroy().then(() => { - console.info(`Destroy : SUCCESS `); +controller.getAVPlaybackState().then((playbackState) => { + console.info(`GetAVPlaybackState : SUCCESS : state : ${playbackState.state}`); }).catch((err) => { - console.info(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`GetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### destroy10+ +### getAVPlaybackState10+ -destroy(callback: AsyncCallback\): void +getAVPlaybackState(callback: AsyncCallback\): void -销毁当前会话,使当前会话完全失效。结果通过callback异步回调方式返回。 +获取当前播放状态相关信息。结果通过callback异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | -------------------- | ---- | ---------- | -| callback | AsyncCallback\ | 是 | 回调函数。当会话销毁成功,err为undefined,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------------------------------------- | ---- | ---------------------------- | +| callback | AsyncCallback<[AVPlaybackState](#avplaybackstate10)\> | 是 | 回调函数,返回当前播放状态对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1905,33 +3212,32 @@ destroy(callback: AsyncCallback\): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** - ```js -session.destroy(function (err) { +controller.getAVPlaybackState(function (err, playbackState) { if (err) { - console.info(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`GetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); } else { - console.info(`Destroy : SUCCESS `); + console.info(`GetAVPlaybackState : SUCCESS : state : ${playbackState.state}`); } }); ``` -### on('play'|'pause'|'stop'|'playNext'|'playPrevious'|'fastForward'|'rewind')10+ +### getAVQueueItems10+ -on(type: 'play'|'pause'|'stop'|'playNext'|'playPrevious'|'fastForward'|'rewind', callback: () => void): void +getAVQueueItems(): Promise\> -设置播放命令监听事件。 +获取当前会话播放列表相关信息。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**参数:** +**返回值:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | -------------------- | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持的事件包括:`'play'`,`'pause'`,`'stop'`,` 'playNext'`,` 'playPrevious'`, `'fastForward'`,` 'rewind'`。
当对应的播放命令被发送到会话时,触发该事件回调。 | -| callback | callback: () => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。 | +| 类型 | 说明 | +| --------------------------------------------- | ----------------------------- | +| Promise\> | Promise对象。返回播放列表队列。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1940,47 +3246,30 @@ on(type: 'play'|'pause'|'stop'|'playNext'|'playPrevious'|'fastForward'|'rewind', | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** - ```js -session.on('play', () => { - console.info(`on play entry`); -}); -session.on('pause', () => { - console.info(`on pause entry`); -}); -session.on('stop', () => { - console.info(`on stop entry`); -}); -session.on('playNext', () => { - console.info(`on playNext entry`); -}); -session.on('playPrevious', () => { - console.info(`on playPrevious entry`); -}); -session.on('fastForward', () => { - console.info(`on fastForward entry`); -}); -session.on('rewind', () => { - console.info(`on rewind entry`); +controller.getAVQueueItems().then((items) => { + console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); +}).catch((err) => { + console.info(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### on('seek')10+ +### getAVQueueItems10+ -on(type: 'seek', callback: (time: number) => void): void +getAVQueueItems(callback: AsyncCallback\>): void -设置跳转节点监听事件。 +获取当前播放列表相关信息。结果通过callback异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ---------------------- | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件`'seek'`:当跳转节点命令被发送到会话时,触发该事件。 | -| callback | (time: number) => void | 是 | 回调函数。参数time是时间节点,单位为毫秒。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------------------------------------- | ---- | ------------------------- | +| callback | AsyncCallback\> | 是 | 回调函数,返回播放列表队列。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -1989,29 +3278,32 @@ on(type: 'seek', callback: (time: number) => void): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** -The session does not exist ```js -session.on('seek', (time) => { - console.info(`on seek entry time : ${time}`); +controller.getAVQueueItems(function (err, items) { + if (err) { + console.info(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); + } }); ``` -### on('setSpeed')10+ +### getAVQueueTitle10+ -on(type: 'setSpeed', callback: (speed: number) => void): void +getAVQueueTitle(): Promise\ -设置播放速率的监听事件。 +获取当前会话播放列表的名称。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**参数:** +**返回值:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------- | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件`'setSpeed'`:当设置播放速率的命令被发送到会话时,触发该事件。 | -| callback | (speed: number) => void | 是 | 回调函数。参数speed是播放倍速。 | +| 类型 | 说明 | +| ---------------- | ----------------------------- | +| Promise | Promise对象。返回播放列表名称。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2020,29 +3312,30 @@ on(type: 'setSpeed', callback: (speed: number) => void): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** - ```js -session.on('setSpeed', (speed) => { - console.info(`on setSpeed speed : ${speed}`); +controller.getAVQueueTitle().then((title) => { + console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); +}).catch((err) => { + console.info(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### on('setLoopMode')10+ +### getAVQueueTitle10+ -on(type: 'setLoopMode', callback: (mode: LoopMode) => void): void +getAVQueueTitle(callback: AsyncCallback\): void -设置循环模式的监听事件。 +获取当前播放列表的名称。结果通过callback异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------- | ---- | ---- | -| type | string | 是 | 事件回调类型,支持事件`'setLoopMode'`:当设置循环模式的命令被发送到会话时,触发该事件。 | -| callback | (mode: [LoopMode](#loopmode10)) => void | 是 | 回调函数。参数mode是循环模式。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------------------- | ---- | ------------------------- | +| callback | AsyncCallback | 是 | 回调函数,返回播放列表名称。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2051,29 +3344,38 @@ on(type: 'setLoopMode', callback: (mode: LoopMode) => void): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** - ```js -session.on('setLoopMode', (mode) => { - console.info(`on setLoopMode mode : ${mode}`); +controller.getAVQueueTitle(function (err, title) { + if (err) { + console.info(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); + } }); ``` -### on('toggleFavorite')10+ +### skipToQueueItem10+ -on(type: 'toggleFavorite', callback: (assetId: string) => void): void +skipToQueueItem(itemId: number): Promise\ -设置是否收藏的监听事件 +设置指定播放列表单项的ID,发送给session端处理,session端可以选择对这个单项歌曲进行播放。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------- | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件`'toggleFavorite'`:当是否收藏的命令被发送到会话时,触发该事件。 | -| callback | (assetId: string) => void | 是 | 回调函数。参数assetId是媒体ID。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------- | ---- | ------------------------------------------- | +| itemId | number | 是 | 播放列表单项的ID值,用以表示选中的播放列表单项。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | --------------------------------------------------------------- | +| Promise\ | Promise对象。当播放列表单项ID设置成功,无返回结果,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2082,29 +3384,33 @@ on(type: 'toggleFavorite', callback: (assetId: string) => void): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** ```js -session.on('toggleFavorite', (assetId) => { - console.info(`on toggleFavorite mode : ${assetId}`); +let queueItemId = 0; +controller.skipToQueueItem(queueItemId).then(() => { + console.info(`SkipToQueueItem successfully`); +}).catch((err) => { + console.info(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### on('skipToQueueItem')10+ +### skipToQueueItem10+ -on(type: 'skipToQueueItem', callback: (itemId: number) => void): void +skipToQueueItem(itemId: number, callback: AsyncCallback\): void -设置播放列表其中某项被选中的监听事件,session端可以选择对这个单项歌曲进行播放。 +设置指定播放列表单项的ID,发送给session端处理,session端可以选择对这个单项歌曲进行播放。结果通过callback异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------ | ---- | ---------------------------------------------------------------------------------------- | -| type | string | 是 | 事件回调类型,支持事件`'skipToQueueItem'`:当播放列表选中单项的命令被发送到会话时,触发该事件。 | -| callback | (itemId: number) => void | 是 | 回调函数。参数itemId是选中的播放列表项的ID。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------- | ---- | ----------------------------------------------------------- | +| itemId | number | 是 | 播放列表单项的ID值,用以表示选中的播放列表单项。 | +| callback | AsyncCallback\ | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2113,29 +3419,34 @@ on(type: 'skipToQueueItem', callback: (itemId: number) => void): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** ```js -session.on('skipToQueueItem', (itemId) => { - console.info(`on skipToQueueItem id : ${itemId}`); +let queueItemId = 0; +controller.skipToQueueItem(queueItemId, function (err) { + if (err) { + console.info(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SkipToQueueItem successfully`); + } }); ``` -### on('handleKeyEvent')10+ +### getAVMetadata10+ -on(type: 'handleKeyEvent', callback: (event: KeyEvent) => void): void +getAVMetadata(): Promise\ -设置按键事件的监听 +获取会话元数据。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**参数:** +**返回值:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件`'handleKeyEvent'`:当按键事件被发送到会话时,触发该事件。 | -| callback | (event: [KeyEvent](js-apis-keyevent.md)) => void | 是 | 回调函数。参数event是按键事件。 | +| 类型 | 说明 | +| ----------------------------------- | ----------------------------- | +| Promise<[AVMetadata](#avmetadata10)\> | Promise对象,返回会话元数据。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2144,29 +3455,30 @@ on(type: 'handleKeyEvent', callback: (event: KeyEvent) => void): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** - ```js -session.on('handleKeyEvent', (event) => { - console.info(`on handleKeyEvent event : ${event}`); +controller.getAVMetadata().then((metadata) => { + console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); +}).catch((err) => { + console.info(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### on('outputDeviceChange')10+ +### getAVMetadata10+ -on(type: 'outputDeviceChange', callback: (device: OutputDeviceInfo) => void): void +getAVMetadata(callback: AsyncCallback\): void -设置播放设备变化的监听事件。 +获取会话元数据。结果通过callback异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件`'outputDeviceChange'`:当播放设备变化时,触发该事件。 | -| callback | (device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | 是 | 回调函数。参数device是设备相关信息。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------------------------- | ---- | -------------------------- | +| callback | AsyncCallback<[AVMetadata](#avmetadata10)\> | 是 | 回调函数,返回会话元数据。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2175,60 +3487,63 @@ on(type: 'outputDeviceChange', callback: (device: OutputDeviceInfo) => void): vo | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** - ```js -session.on('outputDeviceChange', (device) => { - console.info(`on outputDeviceChange device isRemote : ${device.isRemote}`); +controller.getAVMetadata(function (err, metadata) { + if (err) { + console.info(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); + } }); ``` -### on('commonCommand')10+ +### getOutputDevice10+ -on(type: 'commonCommand', callback: (command: string, args: {[key: string]: Object}) => void): void +getOutputDevice(): Promise\ -设置自定义控制命令变化的监听器。 +获取播放设备信息。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**参数:** +**返回值:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件`'commonCommand'`:当自定义控制命令变化时,触发该事件。 | -| callback | (commonCommand: string, args: {[key:string]: Object}) => void | 是 | 回调函数,commonCommand为变化的自定义控制命令名,args为自定义控制命令的参数,参数内容与sendCommand方法设置的参数内容完全一致。 | +| 类型 | 说明 | +| ----------------------------------------------- | --------------------------------- | +| Promise<[OutputDeviceInfo](#outputdeviceinfo10)\> | Promise对象,返回播放设备信息。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ------------------------------ | +| -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** - ```js -session.on('commonCommand', (commonCommand, args) => { - console.info(`OnCommonCommand, the command is ${commonCommand}, args: ${JSON.stringify(args)}`); +controller.getOutputDevice().then((deviceInfo) => { + console.info(`GetOutputDevice : SUCCESS : isRemote : ${deviceInfo.isRemote}`); +}).catch((err) => { + console.info(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### off('play'|'pause'|'stop'|'playNext'|'playPrevious'|'fastForward'|'rewind')10+ +### getOutputDevice10+ -off(type: 'play' | 'pause' | 'stop' | 'playNext' | 'playPrevious' | 'fastForward' | 'rewind', callback?: () => void): void +getOutputDevice(callback: AsyncCallback\): void -取消会话相关事件监听,关闭后,不再进行相关事件回调。 +获取播放设备信息。结果通过callback异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | -------------------- | ---- | ---------------------------------------------------------------------------------------------------------------------------- | -| type | string | 是 | 关闭对应的监听事件,支持的事件包括:`'play'`,` 'pause'`,`'stop'`, `'playNext'`,` 'playPrevious'`, ` 'fastForward'`,` 'rewind'`。 | -| callback | callback: () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------------------------------------- | ---- | ------------------------------ | +| callback | AsyncCallback<[OutputDeviceInfo](#outputdeviceinfo10)\> | 是 | 回调函数,返回播放设备信息。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2236,34 +3551,33 @@ off(type: 'play' | 'pause' | 'stop' | 'playNext' | 'playPrevious' | 'fastForward | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** ```js -session.off('play'); -session.off('pause'); -session.off('stop'); -session.off('playNext'); -session.off('playPrevious'); -session.off('fastForward'); -session.off('rewind'); +controller.getOutputDevice(function (err, deviceInfo) { + if (err) { + console.info(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetOutputDevice : SUCCESS : isRemote : ${deviceInfo.isRemote}`); + } +}); ``` -### off('seek')10+ +### getExtras10+ -off(type: 'seek', callback?: (time: number) => void): void +getExtras(): Promise\<{[key: string]: Object}> -取消监听跳转节点事件。 +获取媒体提供方设置的自定义媒体数据包。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**参数:** +**返回值:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ---------------------- | ---- | ----------------------------------------- | -| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'seek'`。 | -| callback | (time: number) => void | 否 | 回调函数,参数time是时间节点,单位为毫秒。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 类型 | 说明 | +| ----------------------------------- | ----------------------------- | +| Promise<{[key: string]: Object}\> | Promise对象,返回媒体提供方设置的自定义媒体数据包,数据包的内容与setExtras设置的内容完全一致。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2272,27 +3586,30 @@ off(type: 'seek', callback?: (time: number) => void): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | +| 6600105 | Invalid session command. | +| 6600107 | Too many commands or events. | **示例:** - ```js -session.off('seek'); +let extras = await controller.getExtras().catch((err) => { + console.info(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); +}); ``` -### off('setSpeed')10+ +### getExtras10+ -off(type: 'setSpeed', callback?: (speed: number) => void): void +getExtras(callback: AsyncCallback\<{[key: string]: Object}>): void -取消监听播放速率变化事件。 +获取媒体提供方设置的自定义媒体数据包,结果通过callback异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------- | ---- | -------------------------------------------| -| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'setSpeed'`。 | -| callback | (speed: number) => void | 否 | 回调函数,参数speed是播放倍速。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------------------------- | ---- | -------------------------- | +| callback | AsyncCallback<{[key: string]: Object}\> | 是 | 回调函数,返回媒体提供方设置的自定义媒体数据包,数据包的内容与setExtras设置的内容完全一致。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2301,27 +3618,50 @@ off(type: 'setSpeed', callback?: (speed: number) => void): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | +| 6600105 | Invalid session command. | +| 6600107 | Too many commands or events. | **示例:** - ```js -session.off('setSpeed'); +let metadata = { + 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", + lyric: "https://www.example.com/example.lrc", + previousAssetId: "121277", + nextAssetId: "121279", +}; +controller.getExtras(function (err, extras) { + if (err) { + console.info(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`getExtras : SUCCESS : assetId : ${metadata.assetId}`); + } +}); ``` -### off('setLoopMode')10+ +### sendAVKeyEvent10+ -off(type: 'setLoopMode', callback?: (mode: LoopMode) => void): void +sendAVKeyEvent(event: KeyEvent): Promise\ -取消监听循环模式变化事件。 +发送按键事件到控制器对应的会话。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------- | ---- | ----- | -| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'setLoopMode'`。| -| callback | (mode: [LoopMode](#loopmode10)) => void | 否 | 回调函数,参数mode是循环模式。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------------------------------------------------------------ | ---- | ---------- | +| event | [KeyEvent](js-apis-keyevent.md) | 是 | 按键事件。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2330,27 +3670,43 @@ off(type: 'setLoopMode', callback?: (mode: LoopMode) => void): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | +| 6600105 | Invalid session command. | +| 6600106 | The session is not activated. | + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当事件发送成功,无返回结果,否则返回错误对象。 | **示例:** ```js -session.off('setLoopMode'); +let keyItem = {code:0x49, pressedTime:2, deviceId:0}; +let event = {action:2, key:keyItem, keys:[keyItem]}; + +controller.sendAVKeyEvent(event).then(() => { + console.info(`SendAVKeyEvent Successfully`); +}).catch((err) => { + console.info(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); +}); ``` -### off('toggleFavorite')10+ +### sendAVKeyEvent10+ -off(type: 'toggleFavorite', callback?: (assetId: string) => void): void +sendAVKeyEvent(event: KeyEvent, callback: AsyncCallback\): void -取消监听是否收藏的事件 +发送按键事件到会话。结果通过callback异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------- | ---- | -------------------------------------------------------- | -| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'toggleFavorite'`。 | -| callback | (assetId: string) => void | 否 | 回调函数,参数assetId是媒体ID。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ---------- | +| event | [KeyEvent](js-apis-keyevent.md) | 是 | 按键事件。 | +| callback | AsyncCallback\ | 是 | 回调函数。当事件发送成功,err为undefined,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2359,27 +3715,38 @@ off(type: 'toggleFavorite', callback?: (assetId: string) => void): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | +| 6600105 | Invalid session command. | +| 6600106 | The session is not activated. | **示例:** ```js -session.off('toggleFavorite'); +let keyItem = {code:0x49, pressedTime:2, deviceId:0}; +let event = {action:2, key:keyItem, keys:[keyItem]}; + +controller.sendAVKeyEvent(event, function (err) { + if (err) { + console.info(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SendAVKeyEvent Successfully`); + } +}); ``` -### off('skipToQueueItem')10+ +### getLaunchAbility10+ -off(type: 'skipToQueueItem', callback?: (itemId: number) => void): void +getLaunchAbility(): Promise\ -取消监听播放列表单项选中的事件 +获取应用在会话中保存的WantAgent对象。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**参数:** +**返回值:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------ | ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | -| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'skipToQueueItem'`。 | -| callback | (itemId: number) => void | 否 | 回调函数,参数itemId是播放列表单项ID。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 类型 | 说明 | +| ------------------------------------------------------- | ------------------------------------------------------------ | +| Promise<[WantAgent](js-apis-app-ability-wantAgent.md)\> | Promise对象,返回在[setLaunchAbility](#setlaunchability10)保存的对象,包括应用的相关属性信息,如bundleName,abilityName,deviceId等。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2388,18 +3755,25 @@ off(type: 'skipToQueueItem', callback?: (itemId: number) => void): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** ```js -session.off('skipToQueueItem'); +import wantAgent from '@ohos.app.ability.wantAgent'; + +controller.getLaunchAbility().then((agent) => { + console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); +}).catch((err) => { + console.info(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); +}); ``` -### off('handleKeyEvent')10+ +### getLaunchAbility10+ -off(type: 'handleKeyEvent', callback?: (event: KeyEvent) => void): void +getLaunchAbility(callback: AsyncCallback\): void -取消监听按键事件。 +获取应用在会话中保存的WantAgent对象。结果通过callback异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core @@ -2407,8 +3781,7 @@ off(type: 'handleKeyEvent', callback?: (event: KeyEvent) => void): void | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | -| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'handleKeyEvent'`。 | -| callback | (event: [KeyEvent](js-apis-keyevent.md)) => void | 否 | 回调函数,参数event是按键事件。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| callback | AsyncCallback<[WantAgent](js-apis-app-ability-wantAgent.md)\> | 是 | 回调函数。返回在[setLaunchAbility](#setlaunchability10)保存的对象,包括应用的相关属性信息,如bundleName,abilityName,deviceId等。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2417,27 +3790,35 @@ off(type: 'handleKeyEvent', callback?: (event: KeyEvent) => void): void | -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** ```js -session.off('handleKeyEvent'); +import wantAgent from '@ohos.app.ability.wantAgent'; + +controller.getLaunchAbility(function (err, agent) { + if (err) { + console.info(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); + } +}); ``` -### off('outputDeviceChange')10+ +### getRealPlaybackPositionSync10+ -off(type: 'outputDeviceChange', callback?: (device: OutputDeviceInfo) => void): void +getRealPlaybackPositionSync(): number -取消监听播放设备变化的事件。 +获取当前播放位置。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**参数:** +**返回值:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ | -| type | string | 是 | 关闭对应的监听事件,支持关闭事件`'outputDeviceChange'`。 | -| callback | (device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | 否 | 回调函数,参数device是设备相关信息。
当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 类型 | 说明 | +| ------ | ------------------ | +| number | 时间节点,毫秒数。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2445,82 +3826,95 @@ off(type: 'outputDeviceChange', callback?: (device: OutputDeviceInfo) => void): | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** ```js -session.off('outputDeviceChange'); +let time = controller.getRealPlaybackPositionSync(); ``` +### isActive10+ -### off('commonCommand')10+ - -off(type: 'commonCommand', callback?: (command: string, args: {[key:string]: Object}) => void): void +isActive(): Promise\ -取消监听自定义控制命令的变化。 +获取会话是否被激活。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**参数:** +**返回值:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | -| type | string | 是 | 取消对应的监听事件,支持事件`'commonCommand'`。 | -| callback | (command: string, args: {[key:string]: Object}) => void | 否 | 回调函数,参数command是变化的自定义控制命令名,args为自定义控制命令的参数。
该参数为可选参数,若不填写该参数,则认为取消所有对command事件的监听。 | +| 类型 | 说明 | +| ----------------- | ------------------------------------------------------------ | +| Promise | Promise对象,返回会话是否为激活状态,true表示被激活,false表示禁用。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------- | +| -------- | ---------------------------------------- | | 6600101 | Session service exception. | | 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** ```js -session.off('commonCommand'); +controller.isActive().then((isActive) => { + console.info(`IsActive : SUCCESS : isactive : ${isActive}`); +}).catch((err) => { + console.info(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); +}); ``` +### isActive10+ +isActive(callback: AsyncCallback\): void -## AVSessionController10+ +判断会话是否被激活。结果通过callback异步回调方式返回。 -调用[avSession.createController](#avsessioncreatecontroller)后,返回会话控制器实例。控制器可查看会话ID,并可完成对会话发送命令及事件,获取会话元数据,播放状态信息等操作。 +**系统能力:** SystemCapability.Multimedia.AVSession.Core -### 属性 +**参数:** -**系统能力:** SystemCapability.Multimedia.AVSession.Core +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | ------------------------------------------------------------ | +| callback | AsyncCallback | 是 | 回调函数,返回会话是否为激活状态,true表示被激活,false表示禁用。 | -| 名称 | 类型 | 可读 | 可写 | 说明 | -| :-------- | :----- | :--- | :--- | :-------------------------------------- | -| sessionId | string | 是 | 否 | AVSessionController对象唯一的会话标识。 | +**错误码:** +以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | **示例:** + ```js -let sessionId; -await avSession.createController(session.sessionId).then((controller) => { - sessionId = controller.sessionId; -}).catch((err) => { - console.info(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); +controller.isActive(function (err, isActive) { + if (err) { + console.info(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`IsActive : SUCCESS : isactive : ${isActive}`); + } }); ``` -### getAVPlaybackState10+ +### destroy10+ -getAVPlaybackState(): Promise\ +destroy(): Promise\ -获取当前会话播放状态相关信息。结果通过Promise异步回调方式返回。 +销毁当前控制器,销毁后当前控制器不可再用。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **返回值:** -| 类型 | 说明 | -| --------------------------------------------- | --------------------------- | -| Promise<[AVPlaybackState](#avplaybackstate10)\> | Promise对象。返回播放状态对象。 | +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当控制器销毁成功,无返回结果,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2528,31 +3922,31 @@ getAVPlaybackState(): Promise\ | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | **示例:** + ```js -controller.getAVPlaybackState().then((playbackState) => { - console.info(`GetAVPlaybackState : SUCCESS : state : ${playbackState.state}`); +controller.destroy().then(() => { + console.info(`Destroy : SUCCESS `); }).catch((err) => { - console.info(`GetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### getAVPlaybackState10+ +### destroy10+ -getAVPlaybackState(callback: AsyncCallback\): void +destroy(callback: AsyncCallback\): void -获取当前播放状态相关信息。结果通过callback异步回调方式返回。 +销毁当前控制器,销毁后当前控制器不可再用。结果通过callback异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | --------------------------------------------------- | ---- | ---------------------------- | -| callback | AsyncCallback<[AVPlaybackState](#avplaybackstate10)\> | 是 | 回调函数,返回当前播放状态对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------------------- | ---- | ---------- | +| callback | AsyncCallback\ | 是 | 回调函数。当控制器销毁成功,err为undefined,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2560,33 +3954,33 @@ getAVPlaybackState(callback: AsyncCallback\): void | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | **示例:** + ```js -controller.getAVPlaybackState(function (err, playbackState) { +controller.destroy(function (err) { if (err) { - console.info(`GetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); } else { - console.info(`GetAVPlaybackState : SUCCESS : state : ${playbackState.state}`); + console.info(`Destroy : SUCCESS `); } }); ``` -### getAVQueueItems10+ +### getValidCommands10+ -getAVQueueItems(): Promise\> +getValidCommands(): Promise\> -获取当前会话播放列表相关信息。结果通过Promise异步回调方式返回。 +获取会话支持的有效命令。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **返回值:** -| 类型 | 说明 | -| --------------------------------------------- | ----------------------------- | -| Promise\> | Promise对象。返回播放列表队列。 | +| 类型 | 说明 | +| ------------------------------------------------------------ | --------------------------------- | +| Promise\> | Promise对象。返回有效命令的集合。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2598,27 +3992,28 @@ getAVQueueItems(): Promise\> | 6600103 | The session controller does not exist. | **示例:** + ```js -controller.getAVQueueItems().then((items) => { - console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); +controller.getValidCommands.then((validCommands) => { + console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); }).catch((err) => { - console.info(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### getAVQueueItems10+ +### getValidCommands10+ -getAVQueueItems(callback: AsyncCallback\>): void +getValidCommands(callback: AsyncCallback\>): void -获取当前播放列表相关信息。结果通过callback异步回调方式返回。 +获取会话支持的有效命令。结果通过callback异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | --------------------------------------------------- | ---- | ------------------------- | -| callback | AsyncCallback\> | 是 | 回调函数,返回播放列表队列。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ------------------------------ | +| callback | AsyncCallback\\> | 是 | 回调函数,返回有效命令的集合。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2630,29 +4025,40 @@ getAVQueueItems(callback: AsyncCallback\>): void | 6600103 | The session controller does not exist. | **示例:** + ```js -controller.getAVQueueItems(function (err, items) { +controller.getValidCommands(function (err, validCommands) { if (err) { - console.info(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); } else { - console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); + console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); } }); ``` -### getAVQueueTitle10+ +### sendControlCommand10+ -getAVQueueTitle(): Promise\ +sendControlCommand(command: AVControlCommand): Promise\ -获取当前会话播放列表的名称。结果通过Promise异步回调方式返回。 +通过控制器发送命令到其对应的会话。结果通过Promise异步回调方式返回。 + +> **说明:** +> +> 媒体控制方在使用sendControlCommand命令前,需要确保控制对应的媒体会话注册了对应的监听,注册媒体会话相关监听的方法请参见接口[注册媒体会话相关监听](#onplaypausestopplaynextplaypreviousfastforwardrewind10)。 **系统能力:** SystemCapability.Multimedia.AVSession.Core +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------------- | ---- | ------------------------------ | +| command | [AVControlCommand](#avcontrolcommand10) | 是 | 会话的相关命令和命令相关参数。 | + **返回值:** -| 类型 | 说明 | -| ---------------- | ----------------------------- | -| Promise | Promise对象。返回播放列表名称。 | +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2662,69 +4068,108 @@ getAVQueueTitle(): Promise\ | 6600101 | Session service exception. | | 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | +| 6600105 | Invalid session command. | +| 6600106 | The session is not activated. | +| 6600107 | Too many commands or events. | **示例:** + ```js -controller.getAVQueueTitle().then((title) => { - console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); +let avCommand = {command:'play'}; +// let avCommand = {command:'pause'}; +// let avCommand = {command:'stop'}; +// let avCommand = {command:'playNext'}; +// let avCommand = {command:'playPrevious'}; +// let avCommand = {command:'fastForward'}; +// let avCommand = {command:'rewind'}; +// let avCommand = {command:'seek', parameter:10}; +// let avCommand = {command:'setSpeed', parameter:2.6}; +// let avCommand = {command:'setLoopMode', parameter:avSession.LoopMode.LOOP_MODE_SINGLE}; +// let avCommand = {command:'toggleFavorite', parameter:"false"}; +controller.sendControlCommand(avCommand).then(() => { + console.info(`SendControlCommand successfully`); }).catch((err) => { - console.info(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` -### getAVQueueTitle10+ +### sendControlCommand10+ -getAVQueueTitle(callback: AsyncCallback\): void +sendControlCommand(command: AVControlCommand, callback: AsyncCallback\): void -获取当前播放列表的名称。结果通过callback异步回调方式返回。 +通过会话控制器发送命令到其对应的会话。结果通过callback异步回调方式返回。 + +> **说明:** +> +> 媒体控制方在使用sendControlCommand命令前,需要确保控制对应的媒体会话注册了对应的监听,注册媒体会话相关监听的方法请参见接口[注册媒体会话相关监听](#onplaypausestopplaynextplaypreviousfastforwardrewind10)。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ---------------------- | ---- | ------------------------- | -| callback | AsyncCallback | 是 | 回调函数,返回播放列表名称。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------ | +| command | [AVControlCommand](#avcontrolcommand10) | 是 | 会话的相关命令和命令相关参数。 | +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | -| 6600101 | Session service exception. | -| 6600102 | The session does not exist. | -| 6600103 | The session controller does not exist. | +| -------- | ------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | +| 6600105 | Invalid session command. | +| 6600106 | The session is not activated. | +| 6600107 | Too many commands or events. | **示例:** + ```js -controller.getAVQueueTitle(function (err, title) { +let avCommand = {command:'play'}; +// let avCommand = {command:'pause'}; +// let avCommand = {command:'stop'}; +// let avCommand = {command:'playNext'}; +// let avCommand = {command:'playPrevious'}; +// let avCommand = {command:'fastForward'}; +// let avCommand = {command:'rewind'}; +// let avCommand = {command:'seek', parameter:10}; +// let avCommand = {command:'setSpeed', parameter:2.6}; +// let avCommand = {command:'setLoopMode', parameter:avSession.LoopMode.LOOP_MODE_SINGLE}; +// let avCommand = {command:'toggleFavorite', parameter:"false"}; +controller.sendControlCommand(avCommand, function (err) { if (err) { - console.info(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); } else { - console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); + console.info(`SendControlCommand successfully`); } }); ``` -### skipToQueueItem10+ +### sendCommonCommand10+ -skipToQueueItem(itemId: number): Promise\ +sendCommonCommand(command: string, args: {[key: string]: Object}): Promise\ -设置指定播放列表单项的ID,发送给session端处理,session端可以选择对这个单项歌曲进行播放。结果通过Promise异步回调方式返回。 +通过会话控制器发送自定义控制命令到其对应的会话。结果通过Promise异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------ | ------- | ---- | ------------------------------------------- | -| itemId | number | 是 | 播放列表单项的ID值,用以表示选中的播放列表单项。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------------- | ---- | ------------------------------ | +| command | string | 是 | 需要设置的自定义控制命令的名称 | +| args | {[key: string]: any} | 是 | 需要传递的控制命令键值对 | + +> **说明:** +> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 **返回值:** -| 类型 | 说明 | -| -------------- | --------------------------------------------------------------- | -| Promise\ | Promise对象。当播放列表单项ID设置成功,无返回结果,否则返回错误对象。 | +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -2734,895 +4179,753 @@ skipToQueueItem(itemId: number): Promise\ | 6600101 | Session service exception. | | 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | +| 6600105 | Invalid session command. | +| 6600106 | The session is not activated. | +| 6600107 | Too many commands or events. | **示例:** ```js -let queueItemId = 0; -controller.skipToQueueItem(queueItemId).then(() => { - console.info(`SkipToQueueItem successfully`); -}).catch((err) => { - console.info(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); -}); +let commandName = "my_command"; +let args = { + command : "This is my command" +} +await controller.sendCommonCommand(commandName, args).catch((err) => { + console.info(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); +}) ``` -### skipToQueueItem10+ +### sendCommonCommand10+ -skipToQueueItem(itemId: number, callback: AsyncCallback\): void +sendCommonCommand(command: string, args: {[key: string]: Object}, callback: AsyncCallback\): void -设置指定播放列表单项的ID,发送给session端处理,session端可以选择对这个单项歌曲进行播放。结果通过callback异步回调方式返回。 +通过会话控制器发送自定义命令到其对应的会话。结果通过callback异步回调方式返回。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | --------------------- | ---- | ----------------------------------------------------------- | -| itemId | number | 是 | 播放列表单项的ID值,用以表示选中的播放列表单项。 | -| callback | AsyncCallback\ | 是 | 回调函数。当播放状态设置成功,err为undefined,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------------- | ---- | ------------------------------ | +| command | string | 是 | 需要设置的自定义控制命令的名称 | +| args | {[key: string]: any} | 是 | 需要传递的控制命令键值对 | +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | + +> **说明:** +> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | -| 6600101 | Session service exception. | -| 6600102 | The session does not exist. | -| 6600103 | The session controller does not exist. | +| -------- | ------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | +| 6600105 | Invalid session command. | +| 6600106 | The session is not activated. | +| 6600107 | Too many commands or events. | **示例:** ```js -let queueItemId = 0; -controller.skipToQueueItem(queueItemId, function (err) { - if (err) { - console.info(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SkipToQueueItem successfully`); +let commandName = "my_command"; +let args = { + command : "This is my command" +} +controller.sendCommonCommand(commandName, args, (err) => { + if(err) { + console.info(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); } -}); +}) ``` -### getAVMetadata10+ +### on('metadataChange')10+ -getAVMetadata(): Promise\ +on(type: 'metadataChange', filter: Array\ | 'all', callback: (data: AVMetadata) => void) -获取会话元数据。结果通过Promise异步回调方式返回。 +设置元数据变化的监听事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**返回值:** +**参数:** -| 类型 | 说明 | -| ----------------------------------- | ----------------------------- | -| Promise<[AVMetadata](#avmetadata10)\> | Promise对象,返回会话元数据。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'metadataChange'`:当元数据变化时,触发该事件。 | +| filter | Array\ | 'all' | 是 | 'all' 表示关注元数据所有字段变化;Array 表示关注Array中的字段变化。 | +| callback | (data: [AVMetadata](#avmetadata10)) => void | 是 | 回调函数,参数data是变化后的元数据。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ------------------------------ | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | **示例:** + ```js -controller.getAVMetadata().then((metadata) => { - console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); -}).catch((err) => { - console.info(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); +controller.on('metadataChange', 'all', (metadata) => { + console.info(`on metadataChange assetId : ${metadata.assetId}`); +}); + +let metaFilter = ['assetId', 'title', 'description']; +controller.on('metadataChange', metaFilter, (metadata) => { + console.info(`on metadataChange assetId : ${metadata.assetId}`); }); ``` -### getAVMetadata10+ +### on('playbackStateChange')10+ -getAVMetadata(callback: AsyncCallback\): void +on(type: 'playbackStateChange', filter: Array\ | 'all', callback: (state: AVPlaybackState) => void) -获取会话元数据。结果通过callback异步回调方式返回。 +设置播放状态变化的监听事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------------------------- | ---- | -------------------------- | -| callback | AsyncCallback<[AVMetadata](#avmetadata10)\> | 是 | 回调函数,返回会话元数据。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'playbackStateChange'`:当播放状态变化时,触发该事件。 | +| filter | Array\ | 'all' | 是 | 'all' 表示关注播放状态所有字段变化;Array 表示关注Array中的字段变化。 | +| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | 是 | 回调函数,参数state是变化后的播放状态。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ------------------------------ | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | **示例:** + ```js -controller.getAVMetadata(function (err, metadata) { - if (err) { - console.info(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); - } +controller.on('playbackStateChange', 'all', (playbackState) => { + console.info(`on playbackStateChange state : ${playbackState.state}`); +}); + +let playbackFilter = ['state', 'speed', 'loopMode']; +controller.on('playbackStateChange', playbackFilter, (playbackState) => { + console.info(`on playbackStateChange state : ${playbackState.state}`); }); ``` -### getOutputDevice10+ +### on('sessionEvent')10+ -getOutputDevice(): Promise\ +on(type: 'sessionEvent', callback: (sessionEvent: string, args: {[key:string]: Object}) => void): void -获取播放设备信息。结果通过Promise异步回调方式返回。 +媒体控制器设置会话自定义事件变化的监听器。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**返回值:** +**参数:** -| 类型 | 说明 | -| ----------------------------------------------- | --------------------------------- | -| Promise<[OutputDeviceInfo](#outputdeviceinfo10)\> | Promise对象,返回播放设备信息。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'sessionEvent'`:当会话事件变化时,触发该事件。 | +| callback | (sessionEvent: string, args: {[key:string]: object}) => void | 是 | 回调函数,sessionEvent为变化的会话事件名,args为事件的参数。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ------------------------------ | | 6600101 | Session service exception. | | 6600103 | The session controller does not exist. | **示例:** + ```js -controller.getOutputDevice().then((deviceInfo) => { - console.info(`GetOutputDevice : SUCCESS : isRemote : ${deviceInfo.isRemote}`); -}).catch((err) => { - console.info(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); +controller.on('sessionEvent', (sessionEvent, args) => { + console.info(`OnSessionEvent, sessionEvent is ${sessionEvent}, args: ${JSON.stringify(args)}`); }); ``` -### getOutputDevice10+ +### on('queueItemsChange')10+ -getOutputDevice(callback: AsyncCallback\): void +on(type: 'queueItemsChange', callback: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void -获取播放设备信息。结果通过callback异步回调方式返回。 +媒体控制器设置会话自定义播放列表变化的监听器。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------------------------------------- | ---- | ------------------------------ | -| callback | AsyncCallback<[OutputDeviceInfo](#outputdeviceinfo10)\> | 是 | 回调函数,返回播放设备信息。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------------------------------------- | ---- | ---------------------------------------------------------------------------- | +| type | string | 是 | 事件回调类型,支持事件`'queueItemsChange'`:当session修改播放列表时,触发该事件。 | +| callback | (items: Array<[AVQueueItem](#avqueueitem10)\>) => void | 是 | 回调函数,items为变化的播放列表。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ------------------------------ | | 6600101 | Session service exception. | | 6600103 | The session controller does not exist. | **示例:** ```js -controller.getOutputDevice(function (err, deviceInfo) { - if (err) { - console.info(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`GetOutputDevice : SUCCESS : isRemote : ${deviceInfo.isRemote}`); - } +controller.on('queueItemsChange', (items) => { + console.info(`OnQueueItemsChange, items length is ${items.length}`); }); ``` -### getExtras10+ +### on('queueTitleChange')10+ -getExtras(): Promise\<{[key: string]: Object}> +on(type: 'queueTitleChange', callback: (title: string) => void): void -获取媒体提供方设置的自定义媒体数据包。结果通过Promise异步回调方式返回。 +媒体控制器设置会话自定义播放列表的名称变化的监听器。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**返回值:** +**参数:** -| 类型 | 说明 | -| ----------------------------------- | ----------------------------- | -| Promise<{[key: string]: Object}\> | Promise对象,返回媒体提供方设置的自定义媒体数据包,数据包的内容与setExtras设置的内容完全一致。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------- | +| type | string | 是 | 事件回调类型,支持事件`'queueTitleChange'`:当session修改播放列表名称时,触发该事件。 | +| callback | (title: string) => void | 是 | 回调函数,title为变化的播放列表名称。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ------------------------------ | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | -| 6600105 | Invalid session command. | -| 6600107 | Too many commands or events. | **示例:** + ```js -let extras = await controller.getExtras().catch((err) => { - console.info(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); +controller.on('queueTitleChange', (title) => { + console.info(`queueTitleChange, title is ${title}`); }); ``` -### getExtras10+ +### on('extrasChange')10+ -getExtras(callback: AsyncCallback\<{[key: string]: Object}>): void +on(type: 'extrasChange', callback: (extras: {[key:string]: Object}) => void): void -获取媒体提供方设置的自定义媒体数据包,结果通过callback异步回调方式返回。 +媒体控制器设置自定义媒体数据包事件变化的监听器。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------------------------- | ---- | -------------------------- | -| callback | AsyncCallback<{[key: string]: Object}\> | 是 | 回调函数,返回媒体提供方设置的自定义媒体数据包,数据包的内容与setExtras设置的内容完全一致。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'extrasChange'`:当媒体提供方设置自定义媒体数据包时,触发该事件。 | +| callback | (extras: {[key:string]: object}) => void | 是 | 回调函数,extras为媒体提供方新设置的自定义媒体数据包,该自定义媒体数据包与dispatchSessionEvent方法设置的数据包完全一致。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ------------------------------ | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | -| 6600105 | Invalid session command. | -| 6600107 | Too many commands or events. | +| 401 | Parameter check failed | **示例:** + ```js -let metadata = { - 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", - lyric: "https://www.example.com/example.lrc", - previousAssetId: "121277", - nextAssetId: "121279", -}; -controller.getExtras(function (err, extras) { - if (err) { - console.info(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`getExtras : SUCCESS : assetId : ${metadata.assetId}`); - } +controller.on('extrasChange', (extras) => { + console.info(`Caught extrasChange event,the new extra is: ${JSON.stringify(extras)}`); }); ``` -### sendAVKeyEvent10+ +### on('sessionDestroy')10+ -sendAVKeyEvent(event: KeyEvent): Promise\ +on(type: 'sessionDestroy', callback: () => void) -发送按键事件到控制器对应的会话。结果通过Promise异步回调方式返回。 +会话销毁的监听事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------ | ------------------------------------------------------------ | ---- | ---------- | -| event | [KeyEvent](js-apis-keyevent.md) | 是 | 按键事件。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------- | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'sessionDestroy'`:当检测到会话销毁时,触发该事件)。 | +| callback | () => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ------------------------------ | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | -| 6600105 | Invalid session command. | -| 6600106 | The session is not activated. | - -**返回值:** - -| 类型 | 说明 | -| -------------- | ----------------------------- | -| Promise\ | Promise对象。当事件发送成功,无返回结果,否则返回错误对象。 | **示例:** ```js -let keyItem = {code:0x49, pressedTime:2, deviceId:0}; -let event = {action:2, key:keyItem, keys:[keyItem]}; - -controller.sendAVKeyEvent(event).then(() => { - console.info(`SendAVKeyEvent Successfully`); -}).catch((err) => { - console.info(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); +controller.on('sessionDestroy', () => { + console.info(`on sessionDestroy : SUCCESS `); }); ``` -### sendAVKeyEvent10+ +### on('activeStateChange')10+ -sendAVKeyEvent(event: KeyEvent, callback: AsyncCallback\): void +on(type: 'activeStateChange', callback: (isActive: boolean) => void) -发送按键事件到会话。结果通过callback异步回调方式返回。 +会话的激活状态的监听事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------------ | ---- | ---------- | -| event | [KeyEvent](js-apis-keyevent.md) | 是 | 按键事件。 | -| callback | AsyncCallback\ | 是 | 回调函数。当事件发送成功,err为undefined,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------------- | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'activeStateChange'`:当检测到会话的激活状态发生改变时,触发该事件。 | +| callback | (isActive: boolean) => void | 是 | 回调函数。参数isActive表示会话是否被激活。true表示被激活,false表示禁用。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ----------------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | -| 6600103 | The session controller does not exist. | -| 6600105 | Invalid session command. | -| 6600106 | The session is not activated. | +| 6600103 |The session controller does not exist. | **示例:** ```js -let keyItem = {code:0x49, pressedTime:2, deviceId:0}; -let event = {action:2, key:keyItem, keys:[keyItem]}; - -controller.sendAVKeyEvent(event, function (err) { - if (err) { - console.info(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SendAVKeyEvent Successfully`); - } +controller.on('activeStateChange', (isActive) => { + console.info(`on activeStateChange : SUCCESS : isActive ${isActive}`); }); ``` -### getLaunchAbility10+ +### on('validCommandChange')10+ -getLaunchAbility(): Promise\ +on(type: 'validCommandChange', callback: (commands: Array\) => void) -获取应用在会话中保存的WantAgent对象。结果通过Promise异步回调方式返回。 +会话支持的有效命令变化监听事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**返回值:** +**参数:** -| 类型 | 说明 | -| ------------------------------------------------------- | ------------------------------------------------------------ | -| Promise<[WantAgent](js-apis-app-ability-wantAgent.md)\> | Promise对象,返回在[setLaunchAbility](#setlaunchability10)保存的对象,包括应用的相关属性信息,如bundleName,abilityName,deviceId等。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'validCommandChange'`:当检测到会话的合法命令发生改变时,触发该事件。 | +| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype10)\>) => void | 是 | 回调函数。参数commands是有效命令的集合。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ------------------------------ | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | **示例:** ```js -import wantAgent from '@ohos.app.ability.wantAgent'; - -controller.getLaunchAbility().then((agent) => { - console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); -}).catch((err) => { - console.info(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); +controller.on('validCommandChange', (validCommands) => { + console.info(`validCommandChange : SUCCESS : size : ${validCommands.size}`); + console.info(`validCommandChange : SUCCESS : validCommands : ${validCommands.values()}`); }); ``` -### getLaunchAbility10+ +### on('outputDeviceChange')10+ -getLaunchAbility(callback: AsyncCallback\): void +on(type: 'outputDeviceChange', callback: (state: ConnectionState, device: OutputDeviceInfo) => void): void -获取应用在会话中保存的WantAgent对象。结果通过callback异步回调方式返回。 +设置播放设备变化的监听事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | -| callback | AsyncCallback<[WantAgent](js-apis-app-ability-wantAgent.md)\> | 是 | 回调函数。返回在[setLaunchAbility](#setlaunchability10)保存的对象,包括应用的相关属性信息,如bundleName,abilityName,deviceId等。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件为`'outputDeviceChange'`:当播放设备变化时,触发该事件)。 | +| callback | (state: [ConnectionState](#connectionstate10), device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | 是 | 回调函数,参数device是设备相关信息。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ----------------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | **示例:** ```js -import wantAgent from '@ohos.app.ability.wantAgent'; - -controller.getLaunchAbility(function (err, agent) { - if (err) { - console.info(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); - } +controller.on('outputDeviceChange', (state, device) => { + console.info(`on outputDeviceChange state: ${state}, device : ${device}`); }); ``` -### getRealPlaybackPositionSync10+ +### off('metadataChange')10+ -getRealPlaybackPositionSync(): number +off(type: 'metadataChange', callback?: (data: AVMetadata) => void) -获取当前播放位置。 +媒体控制器取消监听元数据变化的事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**返回值:** +**参数:** -| 类型 | 说明 | -| ------ | ------------------ | -| number | 时间节点,毫秒数。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------ | +| type | string | 是 | 取消对应的监听事件,支持事件`'metadataChange'`。 | +| callback | (data: [AVMetadata](#avmetadata10)) => void | 否 | 回调函数,参数data是变化后的元数据。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ---------------- | | 6600101 | Session service exception. | | 6600103 | The session controller does not exist. | **示例:** ```js -let time = controller.getRealPlaybackPositionSync(); +controller.off('metadataChange'); ``` -### isActive10+ +### off('playbackStateChange')10+ -isActive(): Promise\ +off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void) -获取会话是否被激活。结果通过Promise异步回调方式返回。 +媒体控制器取消监听播放状态变化的事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**返回值:** +**参数:** -| 类型 | 说明 | -| ----------------- | ------------------------------------------------------------ | -| Promise | Promise对象,返回会话是否为激活状态,true表示被激活,false表示禁用。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'playbackStateChange'`。 | +| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | 否 | 回调函数,参数state是变化后的播放状态。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ---------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | **示例:** -```js -controller.isActive().then((isActive) => { - console.info(`IsActive : SUCCESS : isactive : ${isActive}`); -}).catch((err) => { - console.info(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); -}); +```js +controller.off('playbackStateChange'); ``` -### isActive10+ +### off('sessionEvent')10+ -isActive(callback: AsyncCallback\): void +off(type: 'sessionEvent', callback?: (sessionEvent: string, args: {[key:string]: Obejct}) => void): void -判断会话是否被激活。结果通过callback异步回调方式返回。 +媒体控制器取消监听会话事件的变化通知。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------- | ---- | ------------------------------------------------------------ | -| callback | AsyncCallback | 是 | 回调函数,返回会话是否为激活状态,true表示被激活,false表示禁用。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'sessionEvent'`。 | +| callback | (sessionEvent: string, args: {[key:string]: object}) => void | 否 | 回调函数,参数sessionEvent是变化的事件名,args为事件的参数。
该参数为可选参数,若不填写该参数,则认为取消所有对sessionEvent事件的监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ---------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | **示例:** ```js -controller.isActive(function (err, isActive) { - if (err) { - console.info(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`IsActive : SUCCESS : isactive : ${isActive}`); - } -}); +controller.off('sessionEvent'); ``` -### destroy10+ +### off('queueItemsChange')10+ -destroy(): Promise\ +off(type: 'queueItemsChange', callback?: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void -销毁当前控制器,销毁后当前控制器不可再用。结果通过Promise异步回调方式返回。 +媒体控制器取消监听播放列表变化的事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**返回值:** +**参数:** -| 类型 | 说明 | -| -------------- | ----------------------------- | -| Promise\ | Promise对象。当控制器销毁成功,无返回结果,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'queueItemsChange'`。 | +| callback | (items: Array<[AVQueueItem](#avqueueitem10)\>) => void | 否 | 回调函数,参数items是变化的播放列表。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ---------------- | | 6600101 | Session service exception. | | 6600103 | The session controller does not exist. | **示例:** ```js -controller.destroy().then(() => { - console.info(`Destroy : SUCCESS `); -}).catch((err) => { - console.info(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); -}); +controller.off('queueItemsChange'); ``` -### destroy10+ +### off('queueTitleChange')10+ -destroy(callback: AsyncCallback\): void +off(type: 'queueTitleChange', callback?: (title: string) => void): void -销毁当前控制器,销毁后当前控制器不可再用。结果通过callback异步回调方式返回。 +媒体控制器取消监听播放列表名称变化的事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | -------------------- | ---- | ---------- | -| callback | AsyncCallback\ | 是 | 回调函数。当控制器销毁成功,err为undefined,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'queueTitleChange'`。 | +| callback | (title: string) => void | 否 | 回调函数,参数items是变化的播放列表名称。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ---------------- | | 6600101 | Session service exception. | | 6600103 | The session controller does not exist. | **示例:** ```js -controller.destroy(function (err) { - if (err) { - console.info(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`Destroy : SUCCESS `); - } -}); +controller.off('queueTitleChange'); ``` -### getValidCommands10+ +### off('extrasChange')10+ -getValidCommands(): Promise\> +off(type: 'extrasChange', callback?: (extras: {[key:string]: Object}) => void): void -获取会话支持的有效命令。结果通过Promise异步回调方式返回。 +媒体控制器取消监听自定义媒体数据包变化事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core -**返回值:** +**参数:** -| 类型 | 说明 | -| ------------------------------------------------------------ | --------------------------------- | -| Promise\> | Promise对象。返回有效命令的集合。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'extrasChange'`。 | +| callback | ({[key:string]: Object}) => void | 否 | 注册监听事件时的回调函数。
该参数为可选参数,若不填写该参数,则认为取消会话所有与此事件相关的监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | -| 6600101 | Session service exception. | -| 6600102 | The session does not exist. | +| -------- | ---------------- | +| 6600101 | Session service exception. | | 6600103 | The session controller does not exist. | +| 401 | Parameter check failed | **示例:** ```js -controller.getValidCommands.then((validCommands) => { - console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); -}).catch((err) => { - console.info(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); -}); +controller.off('extrasChange'); ``` -### getValidCommands10+ +### off('sessionDestroy')10+ -getValidCommands(callback: AsyncCallback\>): void +off(type: 'sessionDestroy', callback?: () => void) -获取会话支持的有效命令。结果通过callback异步回调方式返回。 +媒体控制器取消监听会话的销毁事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------------ | ---- | ------------------------------ | -| callback | AsyncCallback\\> | 是 | 回调函数,返回有效命令的集合。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------- | ---- | ----------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'sessionDestroy'`。 | +| callback | () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ---------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | **示例:** ```js -controller.getValidCommands(function (err, validCommands) { - if (err) { - console.info(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); - } -}); +controller.off('sessionDestroy'); ``` -### sendControlCommand10+ - -sendControlCommand(command: AVControlCommand): Promise\ +### off('activeStateChange')10+ -通过控制器发送命令到其对应的会话。结果通过Promise异步回调方式返回。 +off(type: 'activeStateChange', callback?: (isActive: boolean) => void) -> **说明:** -> -> 媒体控制方在使用sendControlCommand命令前,需要确保控制对应的媒体会话注册了对应的监听,注册媒体会话相关监听的方法请参见接口[注册媒体会话相关监听](#onplaypausestopplaynextplaypreviousfastforwardrewind10)。 +媒体控制器取消监听会话激活状态变化的事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------- | ------------------------------------- | ---- | ------------------------------ | -| command | [AVControlCommand](#avcontrolcommand10) | 是 | 会话的相关命令和命令相关参数。 | - -**返回值:** - -| 类型 | 说明 | -| -------------- | ----------------------------- | -| Promise\ | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | --------------------------- | ---- | ----------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'activeStateChange'`。 | +| callback | (isActive: boolean) => void | 否 | 回调函数。参数isActive表示会话是否被激活。true表示被激活,false表示禁用。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| -------- | ---------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | -| 6600105 | Invalid session command. | -| 6600106 | The session is not activated. | -| 6600107 | Too many commands or events. | **示例:** ```js -let avCommand = {command:'play'}; -// let avCommand = {command:'pause'}; -// let avCommand = {command:'stop'}; -// let avCommand = {command:'playNext'}; -// let avCommand = {command:'playPrevious'}; -// let avCommand = {command:'fastForward'}; -// let avCommand = {command:'rewind'}; -// let avCommand = {command:'seek', parameter:10}; -// let avCommand = {command:'setSpeed', parameter:2.6}; -// let avCommand = {command:'setLoopMode', parameter:avSession.LoopMode.LOOP_MODE_SINGLE}; -// let avCommand = {command:'toggleFavorite', parameter:"false"}; -controller.sendControlCommand(avCommand).then(() => { - console.info(`SendControlCommand successfully`); -}).catch((err) => { - console.info(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); -}); +controller.off('activeStateChange'); ``` -### sendControlCommand10+ - -sendControlCommand(command: AVControlCommand, callback: AsyncCallback\): void +### off('validCommandChange')10+ -通过会话控制器发送命令到其对应的会话。结果通过callback异步回调方式返回。 +off(type: 'validCommandChange', callback?: (commands: Array\) => void) -> **说明:** -> -> 媒体控制方在使用sendControlCommand命令前,需要确保控制对应的媒体会话注册了对应的监听,注册媒体会话相关监听的方法请参见接口[注册媒体会话相关监听](#onplaypausestopplaynextplaypreviousfastforwardrewind10)。 +媒体控制器取消监听会话有效命令变化的事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------- | ---- | ------------------------------ | -| command | [AVControlCommand](#avcontrolcommand10) | 是 | 会话的相关命令和命令相关参数。 | -| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'validCommandChange'`。 | +| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype10)\>) => void | 否 | 回调函数。参数commands是有效命令的集合。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 -| 错误码ID | 错误信息 | -| -------- | ------------------------------- | -| 6600101 | Session service exception. | -| 6600102 | The session does not exist. | -| 6600103 | The session controller does not exist. | -| 6600105 | Invalid session command. | -| 6600106 | The session is not activated. | -| 6600107 | Too many commands or events. | +| 错误码ID | 错误信息 | +| -------- | ---------------- | +| 6600101 | Session service exception. | +| 6600103 | The session controller does not exist. | **示例:** ```js -let avCommand = {command:'play'}; -// let avCommand = {command:'pause'}; -// let avCommand = {command:'stop'}; -// let avCommand = {command:'playNext'}; -// let avCommand = {command:'playPrevious'}; -// let avCommand = {command:'fastForward'}; -// let avCommand = {command:'rewind'}; -// let avCommand = {command:'seek', parameter:10}; -// let avCommand = {command:'setSpeed', parameter:2.6}; -// let avCommand = {command:'setLoopMode', parameter:avSession.LoopMode.LOOP_MODE_SINGLE}; -// let avCommand = {command:'toggleFavorite', parameter:"false"}; -controller.sendControlCommand(avCommand, function (err) { - if (err) { - console.info(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SendControlCommand successfully`); - } -}); +controller.off('validCommandChange'); ``` -### sendCommonCommand10+ +### off('outputDeviceChange')10+ -sendCommonCommand(command: string, args: {[key: string]: Object}): Promise\ +off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: OutputDeviceInfo) => void): void -通过会话控制器发送自定义控制命令到其对应的会话。结果通过Promise异步回调方式返回。 +媒体控制器取消监听分布式设备变化的事件。 **系统能力:** SystemCapability.Multimedia.AVSession.Core **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| ------- | ------------------------------------- | ---- | ------------------------------ | -| command | string | 是 | 需要设置的自定义控制命令的名称 | -| args | {[key: string]: any} | 是 | 需要传递的控制命令键值对 | - -> **说明:** -> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 - -**返回值:** - -| 类型 | 说明 | -| -------------- | ----------------------------- | -| Promise\ | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ | +| type | string | 是 | 取消对应的监听事件,支持事件`'outputDeviceChange'`。 | +| callback | (state: [ConnectionState](#connectionstate10), device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | 否 | 回调函数,参数device是设备相关信息。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 -| 错误码ID | 错误信息 | -| -------- | ---------------------------------------- | +| 错误码ID | 错误信息 | +| -------- | ---------------- | | 6600101 | Session service exception. | -| 6600102 | The session does not exist. | -| 6600103 | The session controller does not exist. | -| 6600105 | Invalid session command. | -| 6600106 | The session is not activated. | -| 6600107 | Too many commands or events. | **示例:** -```js -let commandName = "my_command"; -let args = { - command : "This is my command" -} -await controller.sendCommonCommand(commandName, args).catch((err) => { - console.info(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); -}) +```js +controller.off('outputDeviceChange'); ``` -### sendCommonCommand10+ +## AVCastController10+ -sendCommonCommand(command: string, args: {[key: string]: Object}, callback: AsyncCallback\): void +在投播建立后,调用[avSession.getAVCastController](#getavcastcontroller10)后,返回会话控制器实例。控制器可查看会话ID,并可完成对会话发送命令及事件,获取会话元数据,播放状态信息等操作。 -通过会话控制器发送自定义命令到其对应的会话。结果通过callback异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +### getAVPlaybackState10+ -**参数:** +getAVPlaybackState(callback: AsyncCallback): void; -| 参数名 | 类型 | 必填 | 说明 | -| ------- | ------------------------------------- | ---- | ------------------------------ | -| command | string | 是 | 需要设置的自定义控制命令的名称 | -| args | {[key: string]: any} | 是 | 需要传递的控制命令键值对 | -| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | +设备建立连接后,获取投播控制器。结果通过callback异步回调方式返回。 -> **说明:** -> 参数args支持的数据类型有:字符串、数字、布尔、对象、数组和文件描述符等,详细介绍请参见[@ohos.app.ability.Want(Want)](./js-apis-app-ability-want.md)。 +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| --------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | +| callback | AsyncCallback<[[AVPlaybackState](#avplaybackstate10))\> | 是 | 回调函数,返回投播控制器实例。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ------------------------------- | -| 6600101 | Session service exception. | -| 6600102 | The session does not exist. | -| 6600103 | The session controller does not exist. | -| 6600105 | Invalid session command. | -| 6600106 | The session is not activated. | -| 6600107 | Too many commands or events. | +| -------- | ---------------------------------------- | +| 6600102 | The session does not exist. | +| 6600110 | The remote connection is not established. | **示例:** ```js -let commandName = "my_command"; -let args = { - command : "This is my command" -} -controller.sendCommonCommand(commandName, args, (err) => { - if(err) { - console.info(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); - } -}) +let controller; +session.getAVCastController().then((avcontroller) => { + controller = avcontroller; + console.info(`getAVCastController : SUCCESS : sessionid : ${controller.sessionId}`); +}).catch((err) => { + console.info(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); +}); ``` -### on('metadataChange')10+ +### getAVPlaybackState10+ -on(type: 'metadataChange', filter: Array\ | 'all', callback: (data: AVMetadata) => void) +getAVPlaybackState(): Promise; -设置元数据变化的监听事件。 +获取当前的远端播放状态。结果通过callback异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast -**参数:** +**返回值:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件`'metadataChange'`:当元数据变化时,触发该事件。 | -| filter | Array\ | 'all' | 是 | 'all' 表示关注元数据所有字段变化;Array 表示关注Array中的字段变化。 | -| callback | (data: [AVMetadata](#avmetadata10)) => void | 是 | 回调函数,参数data是变化后的元数据。 | +| 类型 | 说明 | +| --------- | ------------------------------------------------------------ | +| Promise<[AVPlaybackState](#avplaybackstate10)\> | Promise对象。返回投播控制器实例。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ------------------------------ | -| 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | +| -------- | ---------------------------------------- | +| 6600102 | The session does not exist. | +| 6600110 | The remote connection is not established. | **示例:** ```js -controller.on('metadataChange', 'all', (metadata) => { - console.info(`on metadataChange assetId : ${metadata.assetId}`); -}); - -let metaFilter = ['assetId', 'title', 'description']; -controller.on('metadataChange', metaFilter, (metadata) => { - console.info(`on metadataChange assetId : ${metadata.assetId}`); +let controller; +session.getAVPlaybackState(function (err, avcontroller) { + if (err) { + console.info(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + controller = avcontroller; + console.info(`getAVCastController : SUCCESS : sessionid : ${controller.sessionId}`); + } }); ``` @@ -3632,7 +4935,7 @@ on(type: 'playbackStateChange', filter: Array\ | 'all', c 设置播放状态变化的监听事件。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** @@ -3663,51 +4966,48 @@ controller.on('playbackStateChange', playbackFilter, (playbackState) => { }); ``` -### on('sessionEvent')10+ +### off('playbackStateChange')10+ -on(type: 'sessionEvent', callback: (sessionEvent: string, args: {[key:string]: Object}) => void): void +off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void) -媒体控制器设置会话自定义事件变化的监听器。 +媒体控制器取消监听播放状态变化的事件。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件`'sessionEvent'`:当会话事件变化时,触发该事件。 | -| callback | (sessionEvent: string, args: {[key:string]: object}) => void | 是 | 回调函数,sessionEvent为变化的会话事件名,args为事件的参数。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'playbackStateChange'`。 | +| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | 否 | 回调函数,参数state是变化后的播放状态。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ------------------------------ | +| -------- | ---------------- | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | **示例:** ```js -controller.on('sessionEvent', (sessionEvent, args) => { - console.info(`OnSessionEvent, sessionEvent is ${sessionEvent}, args: ${JSON.stringify(args)}`); -}); +controller.off('playbackStateChange'); ``` -### on('queueItemsChange')10+ +### on('mediaItemChange')10+ -on(type: 'queueItemsChange', callback: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void +on(type: 'mediaItemChange', callback: Callback) -媒体控制器设置会话自定义播放列表变化的监听器。 +设置投播当前播放媒体内容的监听事件。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------------------------------------- | ---- | ---------------------------------------------------------------------------- | -| type | string | 是 | 事件回调类型,支持事件`'queueItemsChange'`:当session修改播放列表时,触发该事件。 | -| callback | (items: Array<[AVQueueItem](#avqueueitem10)\>) => void | 是 | 回调函数,items为变化的播放列表。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'mediaItemChange'`:当播放状态变化时,触发该事件。 | +| callback | (state: [AVQueueItem](#avqueueitem10)) => void | 是 | 回调函数,参数AVQueueItem是d当前正在播放的媒体内容。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -3715,61 +5015,56 @@ on(type: 'queueItemsChange', callback: (items: Array<[AVQueueItem](#avqueueitem1 | 错误码ID | 错误信息 | | -------- | ------------------------------ | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | **示例:** ```js -controller.on('queueItemsChange', (items) => { - console.info(`OnQueueItemsChange, items length is ${items.length}`); +controller.on('mediaItemChange', (item) => { + console.info(`on mediaItemChange state : ${item.itemId}`); }); ``` -### on('queueTitleChange')10+ +### off('mediaItemChange')10+ -on(type: 'queueTitleChange', callback: (title: string) => void): void +off(type: 'mediaItemChange') -媒体控制器设置会话自定义播放列表的名称变化的监听器。 +取消设置投播当前播放媒体内容的监听事件。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------- | -| type | string | 是 | 事件回调类型,支持事件`'queueTitleChange'`:当session修改播放列表名称时,触发该事件。 | -| callback | (title: string) => void | 是 | 回调函数,title为变化的播放列表名称。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'mediaItemChange'`。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ------------------------------ | +| -------- | ---------------- | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | **示例:** ```js -controller.on('queueTitleChange', (title) => { - console.info(`queueTitleChange, title is ${title}`); -}); +controller.off('mediaItemChange'); ``` -### on('extrasChange')10+ +### on('playNext')10+ -on(type: 'extrasChange', callback: (extras: {[key:string]: Object}) => void): void +on(type: 'playNext', callback: Callback) -媒体控制器设置自定义媒体数据包事件变化的监听器。 +设置播放下一首资源的监听事件。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件`'extrasChange'`:当媒体提供方设置自定义媒体数据包时,触发该事件。 | -| callback | (extras: {[key:string]: object}) => void | 是 | 回调函数,extras为媒体提供方新设置的自定义媒体数据包,该自定义媒体数据包与dispatchSessionEvent方法设置的数据包完全一致。 | +| type | string | 是 | 事件回调类型,支持事件`'playNext'`:当播放下一首状态变化时,触发该事件。 | +| callback | Callback\ | 是 | 回调函数 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -3777,156 +5072,169 @@ on(type: 'extrasChange', callback: (extras: {[key:string]: Object}) => void): vo | 错误码ID | 错误信息 | | -------- | ------------------------------ | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | -| 401 | Parameter check failed | **示例:** ```js -controller.on('extrasChange', (extras) => { - console.info(`Caught extrasChange event,the new extra is: ${JSON.stringify(extras)}`); +controller.on('playNext', () => { + console.info(`on playNext`); }); ``` -### on('sessionDestroy')10+ +### off('playNext')10+ -on(type: 'sessionDestroy', callback: () => void) +off(type: 'playNext') -会话销毁的监听事件。 +取消设置播放下一首资源的监听事件。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ---------- | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件`'sessionDestroy'`:当检测到会话销毁时,触发该事件)。 | -| callback | () => void | 是 | 回调函数。当监听事件注册成功,err为undefined,否则为错误对象。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'playNext'`。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ------------------------------ | +| -------- | ---------------- | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | **示例:** ```js -controller.on('sessionDestroy', () => { - console.info(`on sessionDestroy : SUCCESS `); -}); +controller.off('playNext'); ``` -### on('activeStateChange')10+ +### on('playPrevious')10+ -on(type: 'activeStateChange', callback: (isActive: boolean) => void) +on(type: 'playPrevious', callback: Callback) -会话的激活状态的监听事件。 +设置播放上一首资源的监听事件。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | --------------------------- | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件`'activeStateChange'`:当检测到会话的激活状态发生改变时,触发该事件。 | -| callback | (isActive: boolean) => void | 是 | 回调函数。参数isActive表示会话是否被激活。true表示被激活,false表示禁用。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'playPrevious'`:当播放下一首状态变化时,触发该事件。 | +| callback | Callback\ | 是 | 回调函数 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ----------------------------- | +| -------- | ------------------------------ | | 6600101 | Session service exception. | -| 6600103 |The session controller does not exist. | **示例:** ```js -controller.on('activeStateChange', (isActive) => { - console.info(`on activeStateChange : SUCCESS : isActive ${isActive}`); +controller.on('playPrevious', () => { + console.info(`on playPrevious`); + // 设置播放参数,开始播放 + var playItem = { + itemId: 0, + description: { + mediaId: '12345', + mediaName: 'song1', + mediaType: 'AUDIO', + mediaUri: 'http://resource1_address', + mediaSize: 12345, + startPosition: 0, + duration: 0, + artist: 'mysong', + albumTitle: 'song1_title', + albumCoverUri: "http://resource1_album_address", + lyricUri: "http://resource1_lyric_address", + iconUri: "http://resource1_icon_address", + appName: 'MyMusic' + } + }; + // 准备播放,这个不会触发真正的播放,会进行加载和缓冲 + controller.prepare(playItem, () => { + console.info('prepare done'); + }); + // 启动播放 + controller.start(playItem, () => { + console.info('play done'); + }); }); ``` -### on('validCommandChange')10+ +### off('playPrevious')10+ -on(type: 'validCommandChange', callback: (commands: Array\) => void) +off(type: 'playPrevious') -会话支持的有效命令变化监听事件。 +取消设置播放下一首资源的监听事件。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件`'validCommandChange'`:当检测到会话的合法命令发生改变时,触发该事件。 | -| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype10)\>) => void | 是 | 回调函数。参数commands是有效命令的集合。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'playPrevious'`。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ------------------------------ | +| -------- | ---------------- | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | **示例:** ```js -controller.on('validCommandChange', (validCommands) => { - console.info(`validCommandChange : SUCCESS : size : ${validCommands.size}`); - console.info(`validCommandChange : SUCCESS : validCommands : ${validCommands.values()}`); -}); +controller.off('playPrevious'); ``` -### on('outputDeviceChange')10+ +### on('seekDone')10+ -on(type: 'outputDeviceChange', callback: (device: OutputDeviceInfo) => void): void +on(type: 'seekDone', callback: Callback) -设置播放设备变化的监听事件。 +设置seek结束的监听事件。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------------ | -| type | string | 是 | 事件回调类型,支持事件为`'outputDeviceChange'`:当播放设备变化时,触发该事件)。 | -| callback | (device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | 是 | 回调函数,参数device是设备相关信息。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | +| type | string | 是 | 事件回调类型,支持事件`'seekDone'`:当seek结束时,触发该事件。 | +| callback | Callback\ | 是 | 回调函数,返回seek后播放的位置 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ----------------------- | +| -------- | ------------------------------ | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | **示例:** ```js -controller.on('outputDeviceChange', (device) => { - console.info(`on outputDeviceChange device isRemote : ${device.isRemote}`); +controller.on('seekDone', (pos) => { + console.info(`on seekDone pos:${pos} `); }); ``` -### off('metadataChange')10+ +### off('seekDone')10+ -off(type: 'metadataChange', callback?: (data: AVMetadata) => void) +off(type: 'seekDone') -媒体控制器取消监听元数据变化的事件。 +取消设置seek结束的监听事件。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------ | -| type | string | 是 | 取消对应的监听事件,支持事件`'metadataChange'`。 | -| callback | (data: [AVMetadata](#avmetadata10)) => void | 否 | 回调函数,参数data是变化后的元数据。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | +| type | string | 是 | 取消对应的监听事件,支持事件`'seekDone'`。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -3934,276 +5242,471 @@ off(type: 'metadataChange', callback?: (data: AVMetadata) => void) | 错误码ID | 错误信息 | | -------- | ---------------- | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | **示例:** ```js -controller.off('metadataChange'); +controller.off('seekDone'); ``` -### off('playbackStateChange')10+ +### on('error')10+ -off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void) +on(type: 'error', callback: ErrorCallback): void -媒体控制器取消监听播放状态变化的事件。 +监听远端播放器的错误事件,该事件仅用于错误提示,不需要用户停止播控动作。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.Media.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | -| type | string | 是 | 取消对应的监听事件,支持事件`'playbackStateChange'`。 | -| callback | (state: [AVPlaybackState](#avplaybackstate10)) => void | 否 | 回调函数,参数state是变化后的播放状态。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | -------- | ---- | ------------------------------------------------------------ | +| type | string | 是 | 错误事件回调类型,支持的事件:'error',用户操作和系统都会触发此事件。 | +| callback | function | 是 | 错误事件回调方法:远端播放过程中发生的错误,会提供错误码ID和错误信息。 | -**错误码:** -以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 +回调的**错误分类**可以分为以下几种: -| 错误码ID | 错误信息 | -| -------- | ---------------- | -| 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | +| 错误码ID | 错误信息 | 说明 | +| -------- | --------------------- | ------------------------------------------------------------ | +| 201 | No Permission: | 无权限执行此操作 | +| 401 | Invalid Parameter: | 入参错误,表示调用无效。 | +| 5400101 | No Memory: | 播放内存不足 | +| 5400102 | Operate Not Permit: | 当前状态机不支持此操作,表示调用无效。 | +| 5400103 | IO Error: | 播放中发现码流异常| +| 5400104 | Network Timeout: | 网络原因超时响应 | +| 5400105 | Service Died: | 播放进程死亡 | +| 5400106 | Unsupport Format: | 不支持的文件格式 | **示例:** ```js -controller.off('playbackStateChange'); +controller.on('error', (error) => { + console.error('error happened,and error message is :' + error.message) + console.error('error happened,and error code is :' + error.code) +}) ``` -### off('sessionEvent')10+ +### off('error')10+ -off(type: 'sessionEvent', callback?: (sessionEvent: string, args: {[key:string]: Obejct}) => void): void +off(type: 'error'): void -媒体控制器取消监听会话事件的变化通知。 +取消监听播放的错误事件。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.Media.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------------ | ---- | ----------------------------------------------------- | -| type | string | 是 | 取消对应的监听事件,支持事件`'sessionEvent'`。 | -| callback | (sessionEvent: string, args: {[key:string]: object}) => void | 否 | 回调函数,参数sessionEvent是变化的事件名,args为事件的参数。
该参数为可选参数,若不填写该参数,则认为取消所有对sessionEvent事件的监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------ | ---- | ----------------------------------------- | +| type | string | 是 | 错误事件回调类型,取消注册的事件:'error' | + +**示例:** + +```js +controller.off('error') +``` + + +### sendControlCommand10+ + +sendControlCommand(command: AVCastControlCommand): Promise\ + +通过控制器发送命令到其对应的会话。结果通过Promise异步回调方式返回。 + + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------------- | ---- | ------------------------------ | +| command | [AVCastControlCommand](#avcastcontrolcommand10) | 是 | 会话的相关命令和命令相关参数。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------- | +| -------- | ---------------------------------------- | | 6600101 | Session service exception. | +| 6600102 | The session does not exist. | | 6600103 | The session controller does not exist. | +| 6600105 | Invalid session command. | +| 6600106 | The session is not activated. | +| 6600107 | Too many commands or events. | **示例:** ```js -controller.off('sessionEvent'); +let avCommand = {command:'play'}; +// let avCommand = {command:'pause'}; +// let avCommand = {command:'stop'}; +// let avCommand = {command:'playNext'}; +// let avCommand = {command:'playPrevious'}; +// let avCommand = {command:'fastForward'}; +// let avCommand = {command:'rewind'}; +// let avCommand = {command:'seek', parameter:10}; +controller.sendControlCommand(avCommand).then(() => { + console.info(`SendControlCommand successfully`); +}).catch((err) => { + console.info(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); +}); ``` -### off('queueItemsChange')10+ +### sendControlCommand10+ -off(type: 'queueItemsChange', callback?: (items: Array<[AVQueueItem](#avqueueitem10)\>) => void): void +sendControlCommand(command: AVCastControlCommand, callback: AsyncCallback\): void -媒体控制器取消监听播放列表变化的事件。 +通过会话控制器发送命令到其对应的会话。结果通过callback异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ---------------------------------------------------- | ---- | --------------------------------------------------------------------------------------------------- | -| type | string | 是 | 取消对应的监听事件,支持事件`'queueItemsChange'`。 | -| callback | (items: Array<[AVQueueItem](#avqueueitem10)\>) => void | 否 | 回调函数,参数items是变化的播放列表。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------ | +| command | [AVCastControlCommand](#avcastcontrolcommand10) | 是 | 会话的相关命令和命令相关参数。 | +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------- | -| 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | +| -------- | ------------------------------- | +| 6600101 | Session service exception. | +| 6600102 | The session does not exist. | +| 6600103 | The session controller does not exist. | +| 6600105 | Invalid session command. | +| 6600106 | The session is not activated. | +| 6600107 | Too many commands or events. | **示例:** ```js -controller.off('queueItemsChange'); +let avCommand = {command:'play'}; +// let avCommand = {command:'pause'}; +// let avCommand = {command:'stop'}; +// let avCommand = {command:'playNext'}; +// let avCommand = {command:'playPrevious'}; +// let avCommand = {command:'fastForward'}; +// let avCommand = {command:'rewind'}; +// let avCommand = {command:'seek', parameter:10}; +controller.sendControlCommand(avCommand, function (err) { + if (err) { + console.info(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SendControlCommand successfully`); + } +}); ``` -### off('queueTitleChange')10+ +### prepare10+ -off(type: 'queueTitleChange', callback?: (title: string) => void): void +prepare(item: AVQueueItem, callback: AsyncCallback): void -媒体控制器取消监听播放列表名称变化的事件。 +启动播放某个媒体资源。结果通过callback异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------------------------------- | -| type | string | 是 | 取消对应的监听事件,支持事件`'queueTitleChange'`。 | -| callback | (title: string) => void | 否 | 回调函数,参数items是变化的播放列表名称。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------------- | ---- | ------------------------------ | +| item | [AVQueueItem](#avqueueitem10) | 是 | 播放列表中单项的相关属性。 | +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------- | +| -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | **示例:** ```js -controller.off('queueTitleChange'); +// 设置播放参数,开始播放 +var playItem = { + itemId: 0, + description: { + mediaId: '12345', + mediaName: 'song1', + mediaType: 'AUDIO', + mediaUri: 'http://resource1_address', + mediaSize: 12345, + startPosition: 0, + duration: 0, + artist: 'mysong', + albumTitle: 'song1_title', + albumCoverUri: "http://resource1_album_address", + lyricUri: "http://resource1_lyric_address", + iconUri: "http://resource1_icon_address", + appName: 'MyMusic' + } +}; +// 准备播放,这个不会触发真正的播放,会进行加载和缓冲 +controller.prepare(playItem, () => { + console.info('prepare done'); +}); +// 启动播放 +controller.start(playItem, () => { + console.info('play done'); +}); ``` -### off('extrasChange')10+ -off(type: 'extrasChange', callback?: (extras: {[key:string]: Object}) => void): void +### prepare10+ -媒体控制器取消监听自定义媒体数据包变化事件。 +prepare(item: AVQueueItem): Promise + +启动播放某个媒体资源。结果通过Promise异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ----------------------- | ---- | ------------------------------------------------------------------------------------------------------- | -| type | string | 是 | 取消对应的监听事件,支持事件`'extrasChange'`。 | -| callback | ({[key:string]: Object}) => void | 否 | 注册监听事件时的回调函数。
该参数为可选参数,若不填写该参数,则认为取消会话所有与此事件相关的监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------------- | ---- | ------------------------------ | +| item | [AVQueueItem](#avqueueitem10) | 是 | 播放列表中单项的相关属性。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------- | -| 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | -| 401 | Parameter check failed | +| -------- | ---------------------------------------- | +| 6600101 | Session service exception. | + **示例:** ```js -controller.off('extrasChange'); +// 设置播放参数,开始播放 +var playItem = { + itemId: 0, + description: { + mediaId: '12345', + mediaName: 'song1', + mediaType: 'AUDIO', + mediaUri: 'http://resource1_address', + mediaSize: 12345, + startPosition: 0, + duration: 0, + artist: 'mysong', + albumTitle: 'song1_title', + albumCoverUri: "http://resource1_album_address", + lyricUri: "http://resource1_lyric_address", + iconUri: "http://resource1_icon_address", + appName: 'MyMusic' + } +}; +// 准备播放,这个不会触发真正的播放,会进行加载和缓冲 +controller.prepare(playItem, () => { + console.info('prepare done'); +}); +// 启动播放 +controller.start(playItem).then(() => { + console.info(`start successfully`); +}).catch((err) => { + console.info(`start BusinessError: code: ${err.code}, message: ${err.message}`); +}); ``` -### off('sessionDestroy')10+ +### start10+ -off(type: 'sessionDestroy', callback?: () => void) +start(item: AVQueueItem, callback: AsyncCallback): void -媒体控制器取消监听会话的销毁事件。 +启动播放某个媒体资源。结果通过callback异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ---------- | ---- | ----------------------------------------------------- | -| type | string | 是 | 取消对应的监听事件,支持事件`'sessionDestroy'`。 | -| callback | () => void | 否 | 回调函数。当监听事件取消成功,err为undefined,否则返回错误对象。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------------- | ---- | ------------------------------ | +| item | [AVQueueItem](#avqueueitem10) | 是 | 播放列表中单项的相关属性。 | +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------- | +| -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | **示例:** ```js -controller.off('sessionDestroy'); +// 设置播放参数,开始播放 +var playItem = { +itemId: 0, +description: { + mediaId: '12345', + mediaName: 'song1', + mediaType: 'AUDIO', + mediaUri: 'http://resource1_address', + mediaSize: 12345, + startPosition: 0, + duration: 0, + artist: 'mysong', + albumTitle: 'song1_title', + albumCoverUri: "http://resource1_album_address", + lyricUri: "http://resource1_lyric_address", + iconUri: "http://resource1_icon_address", + appName: 'MyMusic' +} +}; +// 准备播放,这个不会触发真正的播放,会进行加载和缓冲 +controller.prepare(playItem, () => { + console.info('prepare done'); +}); +// 启动播放 +controller.start(playItem, () => { + console.info('play done'); +}); ``` -### off('activeStateChange')10+ -off(type: 'activeStateChange', callback?: (isActive: boolean) => void) +### start10+ -媒体控制器取消监听会话激活状态变化的事件。 +start(item: AVQueueItem): Promise -**系统能力:** SystemCapability.Multimedia.AVSession.Core +启动播放某个媒体资源。结果通过Promise异步回调方式返回。 + + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | --------------------------- | ---- | ----------------------------------------------------- | -| type | string | 是 | 取消对应的监听事件,支持事件`'activeStateChange'`。 | -| callback | (isActive: boolean) => void | 否 | 回调函数。参数isActive表示会话是否被激活。true表示被激活,false表示禁用。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| ------- | ------------------------------------- | ---- | ------------------------------ | +| item | [AVQueueItem](#avqueueitem10) | 是 | 播放列表中单项的相关属性。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当命令发送成功,无返回结果,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 | 错误码ID | 错误信息 | -| -------- | ---------------- | +| -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | + **示例:** ```js -controller.off('activeStateChange'); +// 设置播放参数,开始播放 +var playItem = { +itemId: 0, +description: { + mediaId: '12345', + mediaName: 'song1', + mediaType: 'AUDIO', + mediaUri: 'http://resource1_address', + mediaSize: 12345, + startPosition: 0, + duration: 0, + artist: 'mysong', + albumTitle: 'song1_title', + albumCoverUri: "http://resource1_album_address", + lyricUri: "http://resource1_lyric_address", + iconUri: "http://resource1_icon_address", + appName: 'MyMusic' +} +}; +// 准备播放,这个不会触发真正的播放,会进行加载和缓冲 +controller.prepare(playItem, () => { + console.info('prepare done'); +}); +// 启动播放 +controller.start(playItem).then(() => { + console.info(`start successfully`); +}).catch((err) => { + console.info(`start BusinessError: code: ${err.code}, message: ${err.message}`); +}); ``` -### off('validCommandChange')10+ +### stopCasting10+ -off(type: 'validCommandChange', callback?: (commands: Array\) => void) +stopCasting(callback: AsyncCallback): void -媒体控制器取消监听会话有效命令变化的事件。 +结束投播。结果通过callback异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast **参数:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------------ | ---- | -------------------------------------------------------- | -| type | string | 是 | 取消对应的监听事件,支持事件`'validCommandChange'`。 | -| callback | (commands: Array<[AVControlCommandType](#avcontrolcommandtype10)\>) => void | 否 | 回调函数。参数commands是有效命令的集合。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------------- | ---- | ------------------------------------- | +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 -| 错误码ID | 错误信息 | -| -------- | ---------------- | +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | **示例:** ```js -controller.off('validCommandChange'); +avSession.stopCasting(function (err) { + if (err) { + console.info(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`stopCasting successfully`); + } +}); ``` -### off('outputDeviceChange')10+ +### stopCasting10+ -off(type: 'outputDeviceChange', callback?: (device: OutputDeviceInfo) => void): void +stopCasting(): Promise -媒体控制器取消监听分布式设备变化的事件。 +结束投播。结果通过Promise异步回调方式返回。 -**系统能力:** SystemCapability.Multimedia.AVSession.Core +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast -**参数:** +**返回值:** -| 参数名 | 类型 | 必填 | 说明 | -| -------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ | -| type | string | 是 | 取消对应的监听事件,支持事件`'outputDeviceChange'`。 | -| callback | (device: [OutputDeviceInfo](#outputdeviceinfo10)) => void | 否 | 回调函数,参数device是设备相关信息。
该参数为可选参数,若不填写该参数,则认为取消所有相关会话的事件监听。 | +| 类型 | 说明 | +| -------------- | ----------------------------- | +| Promise\ | Promise对象。当停止投播索成功,无返回结果,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 -| 错误码ID | 错误信息 | -| -------- | ---------------- | +| 错误码ID | 错误信息 | +| -------- | ---------------------------------------- | | 6600101 | Session service exception. | -| 6600103 | The session controller does not exist. | **示例:** ```js -controller.off('outputDeviceChange'); +avSession.stopCasting().then(() => { + console.info(`stopCasting successfully`); +}).catch((err) => { + console.info(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); +}); ``` + ## SessionToken 会话令牌的信息。 @@ -4217,8 +5720,8 @@ controller.off('outputDeviceChange'); | 名称 | 类型 | 必填 | 说明 | | :-------- | :----- | :--- | :----------- | | sessionId | string | 是 | 会话ID | -| pid | number | 是 | 会话的进程ID | -| uid | number | 是 | 用户ID | +| pid | number | 否 | 会话的进程ID | +| uid | number | 否 | 用户ID | ## AVSessionType10+ 当前会话支持的会话类型。 @@ -4279,6 +5782,38 @@ controller.off('outputDeviceChange'); | command | [AVControlCommandType](#avcontrolcommandtype10) | 是 | 命令 | | parameter | [LoopMode](#loopmode10) | string | number | 否 | 命令对应的参数 | +## AVCastControlCommandType10+ + +投播控制器可传递的命令。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +| 名称 | 类型 | 说明 | +| -------------- | ------ | ------------ | +| play | string | 播放 | +| pause | string | 暂停 | +| stop | string | 停止 | +| playNext | string | 下一首 | +| playPrevious | string | 上一首 | +| fastForward | string | 快进 | +| rewind | string | 快退 | +| seek | numbder | 跳转某一节点 | +| setSpeed | number | 设置播放倍速 | +| setLoopMode | string | 设置循环模式 | +| toggleFavorite | string | 是否收藏 | +| setVolume | number | 设置音量 | + +## AVCastControlCommand10+ + +投播控制器接受的命令的对象描述。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +| 名称 | 类型 | 必填 | 说明 | +| --------- | ------------------------------------------------- | ---- | -------------- | +| command | [AVCastControlCommandType](#avcastcontrolcommandtype10) | 是 | 命令 | +| parameter | [LoopMode](#loopmode10) | string | number | 否 | 命令对应的参数 | + ## AVMetadata10+ 媒体元数据的相关属性。 @@ -4319,6 +5854,18 @@ controller.off('outputDeviceChange'); | iconUri | string | 否 | 播放列表媒体图片路径地址。 | | extras | {[key: string]: any} | 否 | 播放列表媒体额外字段。 | | mediaUri | string | 否 | 播放列表媒体URI。 | +| mediaType | string | 否 | 播放列表媒体类型。 | +| mediaSize | number | 否 | 播放列表媒体的大小。 | +| albumTitle | string | 否 | 播放列表媒体专辑标题。 | +| albumCoverUri | string | 否 | 播放列表媒体专辑标题URI。 | +| lyricContent | string | 否 | 播放列表媒体歌词内容。 | +| lyricUri | string | 否 | 播放列表媒体歌词URI。 | +| artist | string | 否 | 播放列表媒体专辑作者。 | +| fdSrc | media.AVFileDescriptor | 否 | 播放列表媒体本地文件的句柄。 | +| duration | number | 否 | 播放列表媒体播放时长。 | +| startPosition | number | 否 | 播放列表媒体起始播放位置。 | +| creditsPosition | number | 否 | 播放列表媒体的片尾播放位置。 | +| appName | string | 否 | 播放列表提供的应用的名字。 | ## AVQueueItem10+ @@ -4359,6 +5906,72 @@ controller.off('outputDeviceChange'); | elapsedTime | number | 是 | 已用时间,单位毫秒(ms)。 | | updateTime | number | 是 | 更新时间,单位毫秒(ms)。 | +## AVCastCategory10+ + +播放设备的类别枚举。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +| 名称 | 值 | 说明 | +| --------------------------- | ---- | ----------- | +| CATEGORY_LOCAL | 0 | 本地播放,默认播放设备,声音从本机或者连接的蓝牙耳机设备出声。 | +| CATEGORY_REMOTE | 1 | 远端播放,远端播放设备,声音从其他设备发出声音或者画面。 | + +## ConnectionState10+ + +播放设备的类别枚举。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +| 名称 | 值 | 说明 | +| --------------------------- | ---- | ----------- | +| STATE_CONNECTING | 0 | 设备连接中 | +| STATE_CONNECTED | 1 | 设备连接成功 | +| STATE_DISCONNECTED | 6 | 设备断开连接 | + +## ProtocolType10+ + +远端设备支持的协议类型。 + +**系统能力:** SystemCapability.Multimedia.AVSession.AVCast + +**系统接口:** 该接口为系统接口。 + +| 名称 | 值 | 说明 | +| --------------------------- | ---- | ----------- | +| STATE_CONNECTING | 0 | 设备连接中 | +| STATE_CONNECTED | 1 | 设备连接成功 | +| STATE_DISCONNECTED | 6 | 设备断开连接 | + +## DeviceType10+ + +播放设备的类型枚举。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +| 名称 | 值 | 说明 | +| --------------------------- | ---- | ----------- | +| DEVICE_TYPE_LOCAL | 0 | 本地播放类型 | +| DEVICE_TYPE_TV | 2 | 电视 | +| DEVICE_TYPE_SMART_SPEAKER | 3 | 音箱设备 | +| DEVICE_TYPE_BLUETOOTH | 10 | 蓝牙设备 | + + +## DeviceInfo10+ + +播放设备的相关信息。 + +**系统能力:** SystemCapability.Multimedia.AVSession.Core + +| 名称 | 类型 | 必填 | 说明 | +| ---------- | -------------- | ---- | ---------------------- | +| castCategory | AVCastCategory | 是 | 播放设备的类别 | +| deviceId | Array | 是 | 播放设备的ID。 | +| deviceName | string | 是 | 播放设备的名称。 | +| deviceType | DeviceType | 是 | 播放设备的类型。 | +| ipAddress | string | 否 | 播放设备的ip地址。 | +| providerId | number | 否 | 播放设备提供商。 | + ## OutputDeviceInfo10+ 播放设备的相关信息。 @@ -4367,9 +5980,7 @@ controller.off('outputDeviceChange'); | 名称 | 类型 | 必填 | 说明 | | ---------- | -------------- | ---- | ---------------------- | -| isRemote | boolean | 是 | 设备是否连接。 | -| audioDeviceId | Array | 是 | 播放设备的ID集合。 | -| deviceName | Array | 是 | 播放设备的名称集合。 | +| devices | Array\ | 是 | 播放设备的集合。 | ## PlaybackState10+ @@ -4386,6 +5997,9 @@ controller.off('outputDeviceChange'); | PLAYBACK_STATE_FAST_FORWARD | 4 | 快进 | | PLAYBACK_STATE_REWIND | 5 | 快退 | | PLAYBACK_STATE_STOP | 6 | 停止 | +| PLAYBACK_STATE_COMPLETED | 7 | 播放完成 | +| PLAYBACK_STATE_RELEASED | 8 | 释放 | +| PLAYBACK_STATE_ERROR | 9 | 错误 | ## LoopMode10+ @@ -4416,5 +6030,7 @@ controller.off('outputDeviceChange'); | ERR_CODE_COMMAND_INVALID | 6600105 | Invalid session command. | | ERR_CODE_SESSION_INACTIVE | 6600106 | The session is not activated. | | ERR_CODE_MESSAGE_OVERLOAD | 6600107 | Too many commands or events. | +| ERR_CODE_DEVICE_CONNECTION_FAILED | 6600108 | Device connecting failed. | +| ERR_CODE_REMOTE_CONNECTION_NOT_EXIST | 6600109 | The remote connection is not established. |