From 6d9cf064ba204d0258d23980f1047f63e058c216 Mon Sep 17 00:00:00 2001 From: zhangkai269 Date: Wed, 6 Sep 2023 15:22:36 +0800 Subject: [PATCH] =?UTF-8?q?js-api-avsession=20arkts=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangkai269 --- .../reference/apis/js-apis-avsession.md | 2543 ++++++++++------- 1 file changed, 1455 insertions(+), 1088 deletions(-) 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 6ef5769506..c543ba2923 100755 --- a/zh-cn/application-dev/reference/apis/js-apis-avsession.md +++ b/zh-cn/application-dev/reference/apis/js-apis-avsession.md @@ -14,7 +14,7 @@ ## 导入模块 -```js +```ts import avSession from '@ohos.multimedia.avsession'; ``` @@ -50,20 +50,20 @@ createAVSession(context: Context, tag: string, type: AVSessionType): Promise\ { - currentAVSession = data; - sessionId = currentAVSession.sessionId; - console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); -}).catch((err) => { - console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); +avSession.createAVSession(context, tag, "audio").then((data: avSession.AVSession) => { + currentAVSession = data; + sessionId = currentAVSession.sessionId; + console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); +}).catch((err: BusinessError) => { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -94,22 +94,22 @@ createAVSession(context: Context, tag: string, type: AVSessionType, callback: As **示例:** -```js -import featureAbility from '@ohos.ability.featureAbility'; +```ts +import { BusinessError } from '@ohos.base'; -let currentAVSession; +let currentAVSession: avSession.AVSession; let tag = "createNewSession"; -let context = featureAbility.getContext(); -let sessionId; //供后续函数入参使用 +let context: Context = this.context; +let sessionId: string; //供后续函数入参使用 -avSession.createAVSession(context, tag, "audio", function (err, data) { - if (err) { - console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - currentAVSession = data; - sessionId = currentAVSession.sessionId; - console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); - } +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + sessionId = currentAVSession.sessionId; + console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); + } }); ``` @@ -140,16 +140,18 @@ getAllSessionDescriptors(): Promise\>> **示例:** -```js -avSession.getAllSessionDescriptors().then((descriptors) => { - console.info(`getAllSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); - if(descriptors.length > 0 ){ - console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); - console.info(`GetAllSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); - console.info(`GetAllSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); - } -}).catch((err) => { - console.error(`GetAllSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +avSession.getAllSessionDescriptors().then((descriptors: avSession.AVSessionDescriptor) => { + console.info(`getAllSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); + if(descriptors.length > 0 ){ + console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); + console.info(`GetAllSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); + console.info(`GetAllSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); + } +}).catch((err: BusinessError) => { + console.error(`GetAllSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -181,18 +183,20 @@ getAllSessionDescriptors(callback: AsyncCallback\ 0 ){ - console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); - console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); - console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avSession.getAllSessionDescriptors((err: BusinessError, descriptors: avSession.AVSessionDescriptor) => { + if (err) { + console.error(`GetAllSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetAllSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); + if(descriptors.length > 0 ){ + console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); + console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); + console.info(`getAllSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); } + } }); ``` @@ -230,18 +234,20 @@ getHistoricalSessionDescriptors(maxSize?: number): Promise\ { - console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); - if(descriptors.length > 0 ){ - console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); - console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); - console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); - console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionId : ${descriptors[0].sessionId}`); - console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].elementName.bundleName : ${descriptors[0].elementName.bundleName}`); - } -}).catch((err) => { - console.error(`getHistoricalSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +avSession.getHistoricalSessionDescriptors().then((descriptors: avSession.AVSessionDescriptor) => { + console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); + if(descriptors.length > 0 ){ + console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); + console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); + console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); + console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionId : ${descriptors[0].sessionId}`); + console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].elementName.bundleName : ${descriptors[0].elementName.bundleName}`); + } +}).catch((err: BusinessError) => { + console.error(`getHistoricalSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -274,20 +280,22 @@ getHistoricalSessionDescriptors(maxSize: number, callback: AsyncCallback\ 0 ){ - console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); - console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); - console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); - console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionId : ${descriptors[0].sessionId}`); - console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].elementName.bundleName : ${descriptors[0].elementName.bundleName}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avSession.getHistoricalSessionDescriptors(1, (err: BusinessError, descriptors: avSession.AVSessionDescriptor) => { + if (err) { + console.error(`getHistoricalSessionDescriptors BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors.length : ${descriptors.length}`); + if(descriptors.length > 0 ){ + console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].isActive : ${descriptors[0].isActive}`); + console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].type : ${descriptors[0].type}`); + console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionTag : ${descriptors[0].sessionTag}`); + console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].sessionId : ${descriptors[0].sessionId}`); + console.info(`getHistoricalSessionDescriptors : SUCCESS : descriptors[0].elementName.bundleName : ${descriptors[0].elementName.bundleName}`); } + } }); ``` @@ -326,30 +334,30 @@ createController(sessionId: string): Promise\ **示例:** -```js -import featureAbility from '@ohos.ability.featureAbility'; +```ts +import { BusinessError } from '@ohos.base'; -let currentAVSession; +let currentAVSession: avSession.AVSession; let tag = "createNewSession"; -let context = featureAbility.getContext(); -let sessionId; //供后续函数入参使用 +let context: Context = this.context; +let sessionId: string; //供后续函数入参使用 -avSession.createAVSession(context, tag, "audio", function (err, data) { - if (err) { - console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - currentAVSession = data; - sessionId = currentAVSession.sessionId; - console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); - } +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + sessionId = currentAVSession.sessionId; + console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); + } }); -let currentAVcontroller; -avSession.createController(sessionId).then((avcontroller) => { - currentAVcontroller = avcontroller; - console.info('CreateController : SUCCESS '); -}).catch((err) => { - console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); +let currentAVcontroller: avSession.AVSessionController; +avSession.createController(sessionId).then((avcontroller: avSession.AVSessionController) => { + currentAVcontroller = avcontroller; + console.info('CreateController : SUCCESS '); +}).catch((err: BusinessError) => { + console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -382,32 +390,32 @@ createController(sessionId: string, callback: AsyncCallback\ { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + sessionId = currentAVSession.sessionId; + console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); + } }); -let currentAVcontroller; -avSession.createController(sessionId, function (err, avcontroller) { - if (err) { - console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - currentAVcontroller = avcontroller; - console.info('CreateController : SUCCESS '); - } +let currentAVcontroller: avSession.AVSessionController; +avSession.createController(sessionId, (err: BusinessError, avcontroller: avSession.AVSessionController) => { + if (err) { + console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVcontroller = avcontroller; + console.info('CreateController : SUCCESS '); + } }); ``` @@ -449,23 +457,24 @@ castAudio(session: SessionToken | 'all', audioDevices: Array { - audioDevices = data; - console.info(`Promise returned to indicate that the device list is obtained.`); -}).catch((err) => { - console.error(`GetDevices BusinessError: code: ${err.code}, message: ${err.message}`); +let audioDevices: audio.AudioDeviceDescriptors; +audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => { + audioDevices = data; + console.info(`Promise returned to indicate that the device list is obtained.`); +}).catch((err: BusinessError) => { + console.error(`GetDevices BusinessError: code: ${err.code}, message: ${err.message}`); }); avSession.castAudio('all', audioDevices).then(() => { - console.info(`CreateController : SUCCESS`); -}).catch((err) => { - console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`CreateController : SUCCESS`); +}).catch((err: BusinessError) => { + console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -502,25 +511,26 @@ castAudio(session: SessionToken | 'all', audioDevices: Array { - audioDevices = data; - console.info(`Promise returned to indicate that the device list is obtained.`); -}).catch((err) => { - console.error(`GetDevices BusinessError: code: ${err.code}, message: ${err.message}`); +let audioDevices: audio.AudioDeviceDescriptors; +audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG).then((data) => { + audioDevices = data; + console.info(`Promise returned to indicate that the device list is obtained.`); +}).catch((err: BusinessError) => { + console.error(`GetDevices BusinessError: code: ${err.code}, message: ${err.message}`); }); -avSession.castAudio('all', audioDevices, function (err) { - if (err) { - console.error(`CastAudio BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`CastAudio : SUCCESS `); - } +avSession.castAudio('all', audioDevices, (err: BusinessError) => { + if (err) { + console.error(`CastAudio BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`CastAudio : SUCCESS `); + } }); ``` @@ -568,11 +578,11 @@ on(type: 'sessionCreate', callback: (session: AVSessionDescriptor) => void): voi **示例:** -```js -avSession.on('sessionCreate', (descriptor) => { - console.info(`on sessionCreate : isActive : ${descriptor.isActive}`); - console.info(`on sessionCreate : type : ${descriptor.type}`); - console.info(`on sessionCreate : sessionTag : ${descriptor.sessionTag}`); +```ts +avSession.on('sessionCreate', (descriptor: avSession.AVSessionDescriptor) => { + console.info(`on sessionCreate : isActive : ${descriptor.isActive}`); + console.info(`on sessionCreate : type : ${descriptor.type}`); + console.info(`on sessionCreate : sessionTag : ${descriptor.sessionTag}`); }); ``` @@ -605,11 +615,11 @@ on(type: 'sessionDestroy', callback: (session: AVSessionDescriptor) => void): vo **示例:** -```js -avSession.on('sessionDestroy', (descriptor) => { - console.info(`on sessionDestroy : isActive : ${descriptor.isActive}`); - console.info(`on sessionDestroy : type : ${descriptor.type}`); - console.info(`on sessionDestroy : sessionTag : ${descriptor.sessionTag}`); +```ts +avSession.on('sessionDestroy', (descriptor: avSession.AVSessionDescriptor) => { + console.info(`on sessionDestroy : isActive : ${descriptor.isActive}`); + console.info(`on sessionDestroy : type : ${descriptor.type}`); + console.info(`on sessionDestroy : sessionTag : ${descriptor.sessionTag}`); }); ``` @@ -641,11 +651,11 @@ on(type: 'topSessionChange', callback: (session: AVSessionDescriptor) => void): **示例:** -```js -avSession.on('topSessionChange', (descriptor) => { - console.info(`on topSessionChange : isActive : ${descriptor.isActive}`); - console.info(`on topSessionChange : type : ${descriptor.type}`); - console.info(`on topSessionChange : sessionTag : ${descriptor.sessionTag}`); +```ts +avSession.on('topSessionChange', (descriptor: avSession.AVSessionDescriptor) => { + console.info(`on topSessionChange : isActive : ${descriptor.isActive}`); + console.info(`on topSessionChange : type : ${descriptor.type}`); + console.info(`on topSessionChange : sessionTag : ${descriptor.sessionTag}`); }); ``` @@ -677,7 +687,7 @@ off(type: 'sessionCreate', callback?: (session: AVSessionDescriptor) => void): v **示例:** -```js +```ts avSession.off('sessionCreate'); ``` @@ -709,7 +719,7 @@ off(type: 'sessionDestroy', callback?: (session: AVSessionDescriptor) => void): **示例:** -```js +```ts avSession.off('sessionDestroy'); ``` @@ -741,7 +751,7 @@ off(type: 'topSessionChange', callback?: (session: AVSessionDescriptor) => void) **示例:** -```js +```ts avSession.off('topSessionChange'); ``` @@ -771,9 +781,9 @@ on(type: 'sessionServiceDie', callback: () => void): void **示例:** -```js +```ts avSession.on('sessionServiceDie', () => { - console.info(`on sessionServiceDie : session is Died `); + console.info(`on sessionServiceDie : session is Died `); }); ``` @@ -803,7 +813,7 @@ off(type: 'sessionServiceDie', callback?: () => void): void **示例:** -```js +```ts avSession.off('sessionServiceDie'); ``` @@ -836,16 +846,19 @@ sendSystemAVKeyEvent(event: KeyEvent, callback: AsyncCallback\): void **示例:** -```js -let keyItem = {code:0x49, pressedTime:2, deviceId:0}; -let event = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; +```ts +import keyEvent from '@ohos.multimodalInput.keyEvent'; +import { BusinessError } from '@ohos.base'; -avSession.sendSystemAVKeyEvent(event, function (err) { - if (err) { - console.error(`SendSystemAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SendSystemAVKeyEvent : SUCCESS `); - } +let keyItem: keyEvent.Key = {code:0x49, pressedTime:2, deviceId:0}; +let event: keyEvent.KeyEvent = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; + +avSession.sendSystemAVKeyEvent(event, (err: BusinessError) => { + if (err) { + console.error(`SendSystemAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SendSystemAVKeyEvent : SUCCESS `); + } }); ``` @@ -883,15 +896,17 @@ sendSystemAVKeyEvent(event: KeyEvent): Promise\ **示例:** -```js +```ts +import keyEvent from '@ohos.multimodalInput.keyEvent'; +import { BusinessError } from '@ohos.base'; -let keyItem = {code:0x49, pressedTime:2, deviceId:0}; -let event = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; +let keyItem: keyEvent.Key = {code:0x49, pressedTime:2, deviceId:0}; +let event: keyEvent.KeyEvent = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; avSession.sendSystemAVKeyEvent(event).then(() => { - console.info(`SendSystemAVKeyEvent Successfully`); -}).catch((err) => { - console.error(`SendSystemAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`SendSystemAVKeyEvent Successfully`); +}).catch((err: BusinessError) => { + console.error(`SendSystemAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -925,7 +940,9 @@ sendSystemControlCommand(command: AVControlCommand, callback: AsyncCallback\ { + if (err) { + console.error(`SendSystemControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`sendSystemControlCommand successfully`); + } }); ``` @@ -986,7 +1003,10 @@ sendSystemControlCommand(command: AVControlCommand): Promise\ **示例:** -```js +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + let cmd : avSession.AVControlCommandType = 'play'; // let cmd : avSession.AVControlCommandType = 'pause'; // let cmd : avSession.AVControlCommandType = 'stop'; @@ -994,7 +1014,7 @@ let cmd : avSession.AVControlCommandType = 'play'; // let cmd : avSession.AVControlCommandType = 'playPrevious'; // let cmd : avSession.AVControlCommandType = 'fastForward'; // let cmd : avSession.AVControlCommandType = 'rewind'; -let avcommand = {command:cmd}; +let avcommand: avSession.AVControlCommand = {command:cmd}; // let cmd : avSession.AVControlCommandType = 'seek'; // let avcommand = {command:cmd, parameter:10}; // let cmd : avSession.AVControlCommandType = 'setSpeed'; @@ -1004,9 +1024,9 @@ let avcommand = {command:cmd}; // let cmd : avSession.AVControlCommandType = 'toggleFavorite'; // let avcommand = {command:cmd, parameter:"false"}; avSession.sendSystemControlCommand(avcommand).then(() => { - console.info(`SendSystemControlCommand successfully`); -}).catch((err) => { - console.error(`SendSystemControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`SendSystemControlCommand successfully`); +}).catch((err: BusinessError) => { + console.error(`SendSystemControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -1043,13 +1063,15 @@ startCastDeviceDiscovery(callback: AsyncCallback\): void **示例:** -```js -avSession.startCastDeviceDiscovery(function (err) { - if (err) { - console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`startCastDeviceDiscovery successfully`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avSession.startCastDeviceDiscovery((err: BusinessError) => { + if (err) { + console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`startCastDeviceDiscovery successfully`); + } }); ``` @@ -1073,14 +1095,16 @@ startCastDeviceDiscovery(filter: number, callback: AsyncCallback\): void **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + let filter = 2; -avSession.startCastDeviceDiscovery(filter, function (err) { - if (err) { - console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`startCastDeviceDiscovery successfully`); - } +avSession.startCastDeviceDiscovery(filter, (err: BusinessError) => { + if (err) { + console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`startCastDeviceDiscovery successfully`); + } }); ``` @@ -1107,12 +1131,14 @@ startCastDeviceDiscovery(filter?: number): Promise\ **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + let filter = 2; avSession.startCastDeviceDiscovery(filter).then(() => { - console.info(`startCastDeviceDiscovery successfully`); -}).catch((err) => { - console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`startCastDeviceDiscovery successfully`); +}).catch((err: BusinessError) => { + console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -1135,13 +1161,15 @@ stopCastDeviceDiscovery(callback: AsyncCallback\): void **示例:** -```js -avSession.stopCastDeviceDiscovery(function (err) { - if (err) { - console.error(`stopCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`stopCastDeviceDiscovery successfully`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avSession.stopCastDeviceDiscovery((err: BusinessError) => { + if (err) { + console.error(`stopCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`stopCastDeviceDiscovery successfully`); + } }); ``` @@ -1163,11 +1191,13 @@ stopCastDeviceDiscovery(): Promise\ **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + avSession.stopCastDeviceDiscovery().then(() => { - console.info(`startCastDeviceDiscovery successfully`); -}).catch((err) => { - console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`startCastDeviceDiscovery successfully`); +}).catch((err: BusinessError) => { + console.error(`startCastDeviceDiscovery BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -1191,13 +1221,15 @@ setDiscoverable(enable: boolean, callback: AsyncCallback\): void **示例:** -```js -avSession.setDiscoverable(true, function (err) { - if (err) { - console.error(`setDiscoverable BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`setDiscoverable successfully`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avSession.setDiscoverable(true, (err: BusinessError) => { + if (err) { + console.error(`setDiscoverable BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`setDiscoverable successfully`); + } }); ``` @@ -1225,11 +1257,13 @@ setDiscoverable(enable: boolean): Promise\ **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + avSession.setDiscoverable(true).then(() => { - console.info(`setDiscoverable successfully`); -}).catch((err) => { - console.error(`setDiscoverable BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`setDiscoverable successfully`); +}).catch((err: BusinessError) => { + console.error(`setDiscoverable BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -1259,11 +1293,11 @@ on(type: 'deviceAvailable', callback: (device: OutputDeviceInfo) => void): void **示例:** -```js -let castDevice; -avSession.on('deviceAvailable', (device) => { - castDevice = device; - console.info(`on deviceAvailable : ${device} `); +```ts +let castDevice: avSession.OutputDeviceInfo; +avSession.on('deviceAvailable', (device: avSession.OutputDeviceInfo) => { + castDevice = device; + console.info(`on deviceAvailable : ${device} `); }); ``` @@ -1292,7 +1326,7 @@ off(type: 'deviceAvailable', callback?: (device: OutputDeviceInfo) => void): voi **示例:** -```js +```ts avSession.off('deviceAvailable'); ``` @@ -1325,32 +1359,32 @@ getAVCastController(sessionId: string, callback: AsyncCallback\ { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + sessionId = currentAVSession.sessionId; + console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); + } }); -let aVCastController; -avSession.getAVCastController(sessionId ,function (err, avcontroller) { - if (err) { - console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - aVCastController = avcontroller; - console.info('getAVCastController : SUCCESS '); - } +let aVCastController: avSession.AVCastController; +avSession.getAVCastController(sessionId , (err: BusinessError, avcontroller: avSession.AVCastController) => { + if (err) { + console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + aVCastController = avcontroller; + console.info('getAVCastController : SUCCESS '); + } }); ``` @@ -1388,30 +1422,30 @@ getAVCastController(sessionId: string): Promise\; **示例:** -```js -import featureAbility from '@ohos.ability.featureAbility'; +```ts +import { BusinessError } from '@ohos.base'; -let currentAVSession; +let currentAVSession: avSession.AVSession; let tag = "createNewSession"; -let context = featureAbility.getContext(); -let sessionId; //供后续函数入参使用 +let context: Context = this.context; +let sessionId: string; //供后续函数入参使用 -avSession.createAVSession(context, tag, "audio", function (err, data) { - if (err) { - console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - currentAVSession = data; - sessionId = currentAVSession.sessionId; - console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); - } +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + sessionId = currentAVSession.sessionId; + console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); + } }); -let aVCastController; -avSession.getAVCastController(sessionId).then((avcontroller) => { - aVCastController = avcontroller; - console.info('getAVCastController : SUCCESS'); -}).catch((err) => { - console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); +let aVCastController: avSession.AVCastController; +avSession.getAVCastController(sessionId).then((avcontroller: avSession.AVCastController) => { + aVCastController = avcontroller; + console.info('getAVCastController : SUCCESS'); +}).catch((err: BusinessError) => { + console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -1445,38 +1479,39 @@ startCasting(session: SessionToken, device: OutputDeviceInfo, callback: AsyncCal **示例:** -```js -import featureAbility from '@ohos.ability.featureAbility'; +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; -let currentAVSession; +let currentAVSession: avSession.AVSession; let tag = "createNewSession"; -let context = featureAbility.getContext(); -let currSessionId; //供后续函数入参使用 +let context: Context = this.context; +let sessionId: string; //供后续函数入参使用 -avSession.createAVSession(context, tag, "audio", function (err, data) { - if (err) { - console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - currentAVSession = data; - currSessionId = currentAVSession.sessionId; - console.info(`CreateAVSession : SUCCESS : sessionId = ${currSessionId}`); - } +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + sessionId = currentAVSession.sessionId; + console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); + } }); -let myToken = { - sessionId: currSessionId, +let myToken: avSession.SessionToken = { + sessionId: sessionId, } -let castDevice; -avSession.on('deviceAvailable', (device) => { - castDevice = device; - console.info(`on deviceAvailable : ${device} `); +let castDevice: avSession.OutputDeviceInfo; +avSession.on('deviceAvailable', (device: avSession.OutputDeviceInfo) => { + castDevice = device; + console.info(`on deviceAvailable : ${device} `); }); -avSession.startCasting(myToken, castDevice, function (err) { - if (err) { - console.error(`startCasting BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`startCasting successfully`); - } +avSession.startCasting(myToken, castDevice, (err: BusinessError) => { + if (err) { + console.error(`startCasting BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`startCasting successfully`); + } }); ``` @@ -1515,36 +1550,37 @@ startCasting(session: SessionToken, device: OutputDeviceInfo): Promise\ **示例:** -```js -import featureAbility from '@ohos.ability.featureAbility'; +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; -let currentAVSession; +let currentAVSession: avSession.AVSession; let tag = "createNewSession"; -let context = featureAbility.getContext(); -let currSessionId; //供后续函数入参使用 +let context: Context = this.context; +let sessionId: string; //供后续函数入参使用 -avSession.createAVSession(context, tag, "audio", function (err, data) { - if (err) { - console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - currentAVSession = data; - currSessionId = currentAVSession.sessionId; - console.info(`CreateAVSession : SUCCESS : sessionId = ${currSessionId}`); - } +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + sessionId = currentAVSession.sessionId; + console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); + } }); -let myToken = { - sessionId: currSessionId, +let myToken: avSession.SessionToken = { + sessionId: sessionId, } -let castDevice; -avSession.on('deviceAvailable', (device) => { - castDevice = device; - console.info(`on deviceAvailable : ${device} `); +let castDevice: avSession.OutputDeviceInfo; +avSession.on('deviceAvailable', (device: avSession.OutputDeviceInfo) => { + castDevice = device; + console.info(`on deviceAvailable : ${device} `); }); avSession.startCasting(myToken, castDevice).then(() => { - console.info(`startCasting successfully`); -}).catch((err) => { - console.error(`startCasting BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`startCasting successfully`); +}).catch((err: BusinessError) => { + console.error(`startCasting BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -1574,33 +1610,34 @@ stopCasting(session: SessionToken, callback: AsyncCallback\): void **示例:** -```js -import featureAbility from '@ohos.ability.featureAbility'; +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; -let currentAVSession; +let currentAVSession: avSession.AVSession; let tag = "createNewSession"; -let context = featureAbility.getContext(); -let currSessionId; //供后续函数入参使用 +let context: Context = this.context; +let sessionId: string; //供后续函数入参使用 -avSession.createAVSession(context, tag, "audio", function (err, data) { - if (err) { - console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - currentAVSession = data; - currSessionId = currentAVSession.sessionId; - console.info(`CreateAVSession : SUCCESS : sessionId = ${currSessionId}`); - } +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + sessionId = currentAVSession.sessionId; + console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); + } }); -let myToken = { - sessionId: currSessionId, +let myToken: avSession.SessionToken = { + sessionId: sessionId, } -avSession.stopCasting(myToken, function (err) { - if (err) { - console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`stopCasting successfully`); - } +avSession.stopCasting(myToken, (err: BusinessError) => { + if (err) { + console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`stopCasting successfully`); + } }); ``` @@ -1635,32 +1672,34 @@ stopCasting(session: SessionToken): Promise\ **示例:** -```js -import featureAbility from '@ohos.ability.featureAbility'; +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; -let currentAVSession; +let currentAVSession: avSession.AVSession; let tag = "createNewSession"; -let context = featureAbility.getContext(); -let currSessionId; //供后续函数入参使用 +let context: Context = this.context; +let sessionId: string; //供后续函数入参使用 -avSession.createAVSession(context, tag, "audio", function (err, data) { - if (err) { - console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - currentAVSession = data; - currSessionId = currentAVSession.sessionId; - console.info(`CreateAVSession : SUCCESS : sessionId = ${currSessionId}`); - } +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + sessionId = currentAVSession.sessionId; + console.info(`CreateAVSession : SUCCESS : sessionId = ${sessionId}`); + } }); -let myToken = { - sessionId: currSessionId, +let myToken: avSession.SessionToken = { + sessionId: sessionId, } avSession.stopCasting(myToken).then(() => { - console.info(`stopCasting successfully`); -}).catch((err) => { - console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`stopCasting successfully`); +}).catch((err: BusinessError) => { + console.error(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); }); + ``` ## AVSessionType10+ @@ -1688,9 +1727,9 @@ avSession.stopCasting(myToken).then(() => { **示例:** -```js -let sessionId = currentAVSession.sessionId; -let sessionType = currentAVSession.sessionType; +```ts +let sessionId: string = currentAVSession.sessionId; +let sessionType: avSession.AVSessionType = currentAVSession.sessionType; ``` ### setAVMetadata10+ @@ -1723,27 +1762,30 @@ setAVMetadata(data: AVMetadata): Promise\ **示例:** -```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", +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let metadata: avSession.AVMetadata = { + assetId: "121278", + title: "lose yourself", + artist: "Eminem", + author: "ST", + album: "Slim shady", + writer: "ST", + composer: "ST", + duration: 2222, + mediaImage: "https://www.example.com/example.jpg", + subtitle: "8 Mile", + description: "Rap", + lyric: "https://www.example.com/example.lrc", + previousAssetId: "121277", + nextAssetId: "121279", }; currentAVSession.setAVMetadata(metadata).then(() => { - console.info(`SetAVMetadata successfully`); -}).catch((err) => { - console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`SetAVMetadata successfully`); +}).catch((err: BusinessError) => { + console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -1772,29 +1814,32 @@ setAVMetadata(data: AVMetadata, callback: AsyncCallback\): void **示例:** -```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", +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let metadata: avSession.AVMetadata = { + assetId: "121278", + title: "lose yourself", + artist: "Eminem", + author: "ST", + album: "Slim shady", + writer: "ST", + composer: "ST", + duration: 2222, + mediaImage: "https://www.example.com/example.jpg", + subtitle: "8 Mile", + description: "Rap", + lyric: "https://www.example.com/example.lrc", + previousAssetId: "121277", + nextAssetId: "121279", }; -currentAVSession.setAVMetadata(metadata, function (err) { - if (err) { - console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SetAVMetadata successfully`); - } +currentAVSession.setAVMetadata(metadata, (err: BusinessError) => { + if (err) { + console.error(`SetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SetAVMetadata successfully`); + } }); ``` @@ -1828,19 +1873,22 @@ setAVPlaybackState(state: AVPlaybackState): Promise\ **示例:** -```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, +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let playbackState: avSession.AVPlaybackState = { + state:avSession.PlaybackState.PLAYBACK_STATE_PLAY, + speed: 1.0, + position:{elapsedTime:10, updateTime:(new Date()).getTime()}, + bufferedTime:1000, + loopMode:avSession.LoopMode.LOOP_MODE_SINGLE, + isFavorite:true, }; currentAVSession.setAVPlaybackState(playbackState).then(() => { - console.info(`SetAVPlaybackState successfully`); -}).catch((err) => { - console.info(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`SetAVPlaybackState successfully`); +}).catch((err: BusinessError) => { + console.info(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -1869,21 +1917,24 @@ setAVPlaybackState(state: AVPlaybackState, callback: AsyncCallback\): void **示例:** -```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, +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let PlaybackState: avSession.AVPlaybackState = { + state:avSession.PlaybackState.PLAYBACK_STATE_PLAY, + speed: 1.0, + position:{elapsedTime:10, updateTime:(new Date()).getTime()}, + bufferedTime:1000, + loopMode:avSession.LoopMode.LOOP_MODE_SINGLE, + isFavorite:true, }; -currentAVSession.setAVPlaybackState(PlaybackState, function (err) { - if (err) { - console.info(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SetAVPlaybackState successfully`); - } +currentAVSession.setAVPlaybackState(PlaybackState, (err: BusinessError) => { + if (err) { + console.info(`SetAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SetAVPlaybackState successfully`); + } }); ``` @@ -1917,43 +1968,44 @@ setLaunchAbility(ability: WantAgent): Promise\ **示例:** -```js +```ts import wantAgent from '@ohos.app.ability.wantAgent'; +import { BusinessError } from '@ohos.base'; //WantAgentInfo对象 -let wantAgentInfo = { - wants: [ +let wantAgentInfo: wantAgent.WantAgentInfo = { + wants: [ + { + deviceId: "deviceId", + bundleName: "com.example.myapplication", + abilityName: "EntryAbility", + action: "action1", + entities: ["entity1"], + type: "MIMETYPE", + uri: "key={true,true,false}", + parameters: { - 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, - } + 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] + } + ], + operationType: wantAgent.OperationType.START_ABILITIES, + requestCode: 0, + wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] } wantAgent.getWantAgent(wantAgentInfo).then((agent) => { - currentAVSession.setLaunchAbility(agent).then(() => { - console.info(`SetLaunchAbility successfully`); - }).catch((err) => { - console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); - }); + currentAVSession.setLaunchAbility(agent).then(() => { + console.info(`SetLaunchAbility successfully`); + }).catch((err: BusinessError) => { + console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); + }); }); ``` @@ -1982,45 +2034,46 @@ setLaunchAbility(ability: WantAgent, callback: AsyncCallback\): void **示例:** -```js +```ts import wantAgent from '@ohos.app.ability.wantAgent'; +import { BusinessError } from '@ohos.base'; //WantAgentInfo对象 -let wantAgentInfo = { - wants: [ +let wantAgentInfo: wantAgent.WantAgentInfo = { + wants: [ + { + deviceId: "deviceId", + bundleName: "com.example.myapplication", + abilityName: "EntryAbility", + action: "action1", + entities: ["entity1"], + type: "MIMETYPE", + uri: "key={true,true,false}", + parameters: { - 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, - } + 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] + } + ], + operationType: wantAgent.OperationType.START_ABILITIES, + requestCode: 0, + wantAgentFlags:[wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] } wantAgent.getWantAgent(wantAgentInfo).then((agent) => { - currentAVSession.setLaunchAbility(agent, function (err) { - if (err) { - console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SetLaunchAbility successfully`); - } - }); + currentAVSession.setLaunchAbility(agent, (err: BusinessError) => { + if (err) { + console.error(`SetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SetLaunchAbility successfully`); + } + }); }); ``` @@ -2058,13 +2111,27 @@ dispatchSessionEvent(event: string, args: {[key: string]: Object}): Promise\ { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + } +}); let eventName = "dynamic_lyric"; -let args = { - lyric : "This is lyric" -} -currentAVSession.dispatchSessionEvent(eventName, args).catch((err) => { - console.info(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); + +currentAVSession.dispatchSessionEvent(eventName, {lyric : "This is lyric"}).then(() => { + console.info(`dispatchSessionEvent successfully`); +}).catch((err: BusinessError) => { + console.info(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); }) ``` @@ -2097,15 +2164,26 @@ dispatchSessionEvent(event: string, args: {[key: string]: Object}, callback: Asy **示例:** -```js +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let currentAVSession: avSession.AVSession; +let tag = "createNewSession"; +let context: Context = this.context; + +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + } +}); let eventName = "dynamic_lyric"; -let args = { - lyric : "This is lyric" -} -currentAVSession.dispatchSessionEvent(eventName, args, (err) => { - if(err) { - console.error(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); - } +currentAVSession.dispatchSessionEvent(eventName, {lyric : "This is lyric"}, (err: BusinessError) => { + if(err) { + console.error(`dispatchSessionEvent BusinessError: code: ${err.code}, message: ${err.message}`); + } }) ``` @@ -2139,44 +2217,49 @@ setAVQueueItems(items: Array\): Promise\ **示例:** -```js +```ts import image from '@ohos.multimedia.image'; import resourceManager from '@ohos.resourceManager'; +import { BusinessError } from '@ohos.base'; +import avSession from '@ohos.multimedia.avsession'; -let value : Uint8Array = await resourceManager.getRawFileContent('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 value: Uint8Array = resourceManager.getRawFileContent('IMAGE_URI'); +let imageSource: image.ImageSource = image.createImageSource(value.buffer); +let imagePixel: image.PixelMap; +imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}).then((data) => { + imagePixel = data; +}).catch((err: BusinessError) => { + console.error(`createPixelMap BusinessError: code: ${err.code}, message: ${err.message}`); +}) +let queueItemDescription_1: avSession.AVMediaDescription = { + assetId: '001', + title: 'music_name', + subtitle: 'music_sub_name', + description: 'music_description', + mediaImage : imagePixel, + extras: {extras:'any'} }; -let queueItem_1 = { - itemId: 1, - description: queueItemDescription_1 +let queueItem_1: avSession.AVQueueItem = { + 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 queueItemDescription_2: avSession.AVMediaDescription = { + assetId: '002', + title: 'music_name', + subtitle: 'music_sub_name', + description: 'music_description', + mediaImage: imagePixel, + extras: {extras:'any'} }; -let queueItem_2 = { - itemId: 2, - description: queueItemDescription_2 +let queueItem_2: avSession.AVQueueItem = { + itemId: 2, + description: queueItemDescription_2 }; -let queueItemsArray = [queueItem_1, queueItem_2]; +let queueItemsArray: avSession.AVQueueItem[] = [queueItem_1, queueItem_2]; currentAVSession.setAVQueueItems(queueItemsArray).then(() => { - console.info(`SetAVQueueItems successfully`); -}).catch((err) => { - console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`SetAVQueueItems successfully`); +}).catch((err: BusinessError) => { + console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -2205,46 +2288,51 @@ setAVQueueItems(items: Array\, callback: AsyncCallback\): voi **示例:** -```js +```ts import image from '@ohos.multimedia.image'; import resourceManager from '@ohos.resourceManager'; +import { BusinessError } from '@ohos.base'; +import avSession from '@ohos.multimedia.avsession'; -let value : Uint8Array = await resourceManager.getRawFileContent('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 value: Uint8Array = resourceManager.getRawFileContent('IMAGE_URI'); +let imageSource: image.ImageSource = image.createImageSource(value.buffer); +let imagePixel: image.PixelMap; +imageSource.createPixelMap({desiredSize:{width: 150, height: 150}}).then((data) => { + imagePixel = data; +}).catch((err: BusinessError) => { + console.error(`createPixelMap BusinessError: code: ${err.code}, message: ${err.message}`); +}) +let queueItemDescription_1: avSession.AVMediaDescription = { + assetId: '001', + title: 'music_name', + subtitle: 'music_sub_name', + description: 'music_description', + mediaImage : imagePixel, + extras: {extras:'any'} }; -let queueItem_1 = { - itemId: 1, - description: queueItemDescription_1 +let queueItem_1: avSession.AVQueueItem = { + 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 queueItemDescription_2: avSession.AVMediaDescription = { + assetId: '002', + title: 'music_name', + subtitle: 'music_sub_name', + description: 'music_description', + mediaImage: imagePixel, + extras: {extras:'any'} }; -let queueItem_2 = { - itemId: 2, - description: queueItemDescription_2 +let queueItem_2: avSession.AVQueueItem = { + itemId: 2, + description: queueItemDescription_2 }; -let queueItemsArray = [queueItem_1, queueItem_2]; -currentAVSession.setAVQueueItems(queueItemsArray, function (err) { - if (err) { - console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SetAVQueueItems successfully`); - } +let queueItemsArray: avSession.AVQueueItem[] = [queueItem_1, queueItem_2]; +currentAVSession.setAVQueueItems(queueItemsArray, (err: BusinessError) => { + if (err) { + console.error(`SetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SetAVQueueItems successfully`); + } }); ``` @@ -2278,12 +2366,14 @@ setAVQueueTitle(title: string): Promise\ **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + let queueTitle = 'QUEUE_TITLE'; currentAVSession.setAVQueueTitle(queueTitle).then(() => { - console.info(`SetAVQueueTitle successfully`); -}).catch((err) => { - console.error(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`SetAVQueueTitle successfully`); +}).catch((err: BusinessError) => { + console.error(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -2312,14 +2402,16 @@ setAVQueueTitle(title: string, callback: AsyncCallback\): void **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + let queueTitle = 'QUEUE_TITLE'; -currentAVSession.setAVQueueTitle(queueTitle, function (err) { - if (err) { - console.info(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.error(`SetAVQueueTitle successfully`); - } +currentAVSession.setAVQueueTitle(queueTitle, (err: BusinessError) => { + if (err) { + console.info(`SetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.error(`SetAVQueueTitle successfully`); + } }); ``` @@ -2356,12 +2448,23 @@ setExtras(extras: {[key: string]: Object}): Promise\ **示例:** -```js -let extras = { - extras : "This is custom media packet" -} -currentAVSession.setExtras(extras).catch((err) => { - console.info(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let currentAVSession: avSession.AVSession; +let tag = "createNewSession"; +let context: Context = this.context; + +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + } +}); +currentAVSession.setExtras({"extras" : "This is custom media packet"}).catch((err: BusinessError) => { + console.info(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); }) ``` @@ -2393,14 +2496,25 @@ setExtras(extras: {[key: string]: Object}, callback: AsyncCallback\): void **示例:** -```js -let extras = { - extras : "This is custom media packet" -} -currentAVSession.setExtras(extras, (err) => { - if(err) { - console.error(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); - } +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let currentAVSession: avSession.AVSession; +let tag = "createNewSession"; +let context: Context = this.context; + +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + } +}); +currentAVSession.setExtras({"extras" : "This is custom media packet"}, (err: BusinessError) => { + if(err) { + console.error(`setExtras BusinessError: code: ${err.code}, message: ${err.message}`); + } }) ``` @@ -2428,13 +2542,15 @@ getController(): Promise\ **示例:** -```js -let avsessionController; -currentAVSession.getController().then((avcontroller) => { - avsessionController = avcontroller; - console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`); -}).catch((err) => { - console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +let avsessionController: avSession.AVSessionController; +currentAVSession.getController().then((avcontroller: avSession.AVSessionController) => { + avsessionController = avcontroller; + console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`); +}).catch((err: BusinessError) => { + console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -2462,15 +2578,17 @@ getController(callback: AsyncCallback\): void **示例:** -```js -let avsessionController; -currentAVSession.getController(function (err, avcontroller) { - if (err) { - console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - avsessionController = avcontroller; - console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +let avsessionController: avSession.AVSessionController; +currentAVSession.getController((err: BusinessError, avcontroller: avSession.AVSessionController) => { + if (err) { + console.error(`GetController BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + avsessionController = avcontroller; + console.info(`GetController : SUCCESS : sessionid : ${avsessionController.sessionId}`); + } }); ``` @@ -2498,13 +2616,15 @@ getAVCastController(callback: AsyncCallback\): void **示例:** -```js -let aVCastController; -currentAVSession.getAVCastController().then((avcontroller) => { - aVCastController = avcontroller; - console.info(`getAVCastController : SUCCESS : sessionid : ${aVCastController.sessionId}`); -}).catch((err) => { - console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +let aVCastController: avSession.AVCastController; +currentAVSession.getAVCastController().then((avcontroller: avSession.AVCastController) => { + aVCastController = avcontroller; + console.info(`getAVCastController : SUCCESS : sessionid : ${aVCastController.sessionId}`); +}).catch((err: BusinessError) => { + console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -2532,15 +2652,17 @@ getAVCastController(): Promise\; **示例:** -```js -let aVCastController; -currentAVSession.getAVCastController(function (err, avcontroller) { - if (err) { - console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - aVCastController = avcontroller; - console.info(`getAVCastController : SUCCESS : sessionid : ${aVCastController.sessionId}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +let aVCastController: avSession.AVCastController; +currentAVSession.getAVCastController((err: BusinessError, avcontroller: avSession.AVCastController) => { + if (err) { + console.error(`getAVCastController BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + aVCastController = avcontroller; + console.info(`getAVCastController : SUCCESS : sessionid : ${aVCastController.sessionId}`); + } }); ``` @@ -2569,12 +2691,14 @@ getOutputDevice(): Promise\ **示例:** -```js -currentAVSession.getOutputDevice().then((outputDeviceInfo) => { - console.info(`GetOutputDevice : SUCCESS : isRemote : ${outputDeviceInfo.isRemote}`); -}).catch((err) => { - console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); -}); +```ts +import { BusinessError } from '@ohos.base'; + +currentAVSession.getOutputDevice().then((outputDeviceInfo: avSession.OutputDeviceInfo) => { + console.info(`GetOutputDevice : SUCCESS : isRemote : ${outputDeviceInfo.isRemote}`); +}).catch((err: BusinessError) => { + console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); +}) ``` ### getOutputDevice10+ @@ -2602,13 +2726,15 @@ getOutputDevice(callback: AsyncCallback\): void **示例:** -```js -currentAVSession.getOutputDevice(function (err, outputDeviceInfo) { - if (err) { - console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`GetOutputDevice : SUCCESS : isRemote : ${outputDeviceInfo.isRemote}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +currentAVSession.getOutputDevice((err: BusinessError, outputDeviceInfo: avSession.OutputDeviceInfo) => { + if (err) { + console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetOutputDevice : SUCCESS : isRemote : ${outputDeviceInfo.isRemote}`); + } }); ``` @@ -2636,11 +2762,13 @@ activate(): Promise\ **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + currentAVSession.activate().then(() => { - console.info(`Activate : SUCCESS `); -}).catch((err) => { - console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`Activate : SUCCESS `); +}).catch((err: BusinessError) => { + console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -2668,13 +2796,15 @@ activate(callback: AsyncCallback\): void **示例:** -```js -currentAVSession.activate(function (err) { - if (err) { - console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`Activate : SUCCESS `); - } +```ts +import { BusinessError } from '@ohos.base'; + +currentAVSession.activate((err: BusinessError) => { + if (err) { + console.error(`Activate BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`Activate : SUCCESS `); + } }); ``` @@ -2702,11 +2832,13 @@ deactivate(): Promise\ **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + currentAVSession.deactivate().then(() => { - console.info(`Deactivate : SUCCESS `); -}).catch((err) => { - console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`Deactivate : SUCCESS `); +}).catch((err: BusinessError) => { + console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -2736,13 +2868,15 @@ deactivate(callback: AsyncCallback\): void **示例:** -```js -currentAVSession.deactivate(function (err) { - if (err) { - console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`Deactivate : SUCCESS `); - } +```ts +import { BusinessError } from '@ohos.base'; + +currentAVSession.deactivate((err: BusinessError) => { + if (err) { + console.error(`Deactivate BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`Deactivate : SUCCESS `); + } }); ``` @@ -2770,11 +2904,13 @@ destroy(): Promise\ **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + currentAVSession.destroy().then(() => { - console.info(`Destroy : SUCCESS `); -}).catch((err) => { - console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`Destroy : SUCCESS `); +}).catch((err: BusinessError) => { + console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -2802,13 +2938,15 @@ destroy(callback: AsyncCallback\): void **示例:** -```js -currentAVSession.destroy(function (err) { - if (err) { - console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`Destroy : SUCCESS `); - } +```ts +import { BusinessError } from '@ohos.base'; + +currentAVSession.destroy((err: BusinessError) => { + if (err) { + console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`Destroy : SUCCESS `); + } }); ``` @@ -2837,9 +2975,9 @@ on(type: 'play', callback: () => void): void **示例:** -```js +```ts currentAVSession.on('play', () => { - console.info(`on play entry`); + console.info(`on play entry`); }); ``` @@ -2868,9 +3006,9 @@ on(type: 'pause', callback: () => void): void **示例:** -```js +```ts currentAVSession.on('pause', () => { - console.info(`on pause entry`); + console.info(`on pause entry`); }); ``` @@ -2899,9 +3037,9 @@ on(type:'stop', callback: () => void): void **示例:** -```js +```ts currentAVSession.on('stop', () => { - console.info(`on stop entry`); + console.info(`on stop entry`); }); ``` @@ -2930,9 +3068,9 @@ on(type:'playNext', callback: () => void): void **示例:** -```js +```ts currentAVSession.on('playNext', () => { - console.info(`on playNext entry`); + console.info(`on playNext entry`); }); ``` @@ -2961,9 +3099,9 @@ on(type:'playPrevious', callback: () => void): void **示例:** -```js +```ts currentAVSession.on('playPrevious', () => { - console.info(`on playPrevious entry`); + console.info(`on playPrevious entry`); }); ``` @@ -2992,9 +3130,9 @@ on(type: 'fastForward', callback: () => void): void **示例:** -```js +```ts currentAVSession.on('fastForward', () => { - console.info(`on fastForward entry`); + console.info(`on fastForward entry`); }); ``` @@ -3023,9 +3161,9 @@ on(type:'rewind', callback: () => void): void **示例:** -```js +```ts currentAVSession.on('rewind', () => { - console.info(`on rewind entry`); + console.info(`on rewind entry`); }); ``` @@ -3054,9 +3192,9 @@ on(type: 'seek', callback: (time: number) => void): void **示例:** -```js -currentAVSession.on('seek', (time) => { - console.info(`on seek entry time : ${time}`); +```ts +currentAVSession.on('seek', (time: number) => { + console.info(`on seek entry time : ${time}`); }); ``` @@ -3085,9 +3223,9 @@ on(type: 'setSpeed', callback: (speed: number) => void): void **示例:** -```js -currentAVSession.on('setSpeed', (speed) => { - console.info(`on setSpeed speed : ${speed}`); +```ts +currentAVSession.on('setSpeed', (speed: number) => { + console.info(`on setSpeed speed : ${speed}`); }); ``` @@ -3116,9 +3254,9 @@ on(type: 'setLoopMode', callback: (mode: LoopMode) => void): void **示例:** -```js -currentAVSession.on('setLoopMode', (mode) => { - console.info(`on setLoopMode mode : ${mode}`); +```ts +currentAVSession.on('setLoopMode', (mode: avSession.LoopMode) => { + console.info(`on setLoopMode mode : ${mode}`); }); ``` @@ -3147,9 +3285,9 @@ on(type: 'toggleFavorite', callback: (assetId: string) => void): void **示例:** -```js -currentAVSession.on('toggleFavorite', (assetId) => { - console.info(`on toggleFavorite mode : ${assetId}`); +```ts +currentAVSession.on('toggleFavorite', (assetId: string) => { + console.info(`on toggleFavorite mode : ${assetId}`); }); ``` @@ -3178,9 +3316,9 @@ on(type: 'skipToQueueItem', callback: (itemId: number) => void): void **示例:** -```js -currentAVSession.on('skipToQueueItem', (itemId) => { - console.info(`on skipToQueueItem id : ${itemId}`); +```ts +currentAVSession.on('skipToQueueItem', (itemId: number) => { + console.info(`on skipToQueueItem id : ${itemId}`); }); ``` @@ -3209,10 +3347,13 @@ on(type: 'handleKeyEvent', callback: (event: KeyEvent) => void): void **示例:** -```js -currentAVSession.on('handleKeyEvent', (event) => { - console.info(`on handleKeyEvent event : ${event}`); +```ts +import keyEvent from '@ohos.multimodalInput.keyEvent'; + +currentAVSession.on('handleKeyEvent', (event: keyEvent.KeyEvent) => { + console.info(`on handleKeyEvent event : ${event}`); }); + ``` ### on('outputDeviceChange')10+ @@ -3240,9 +3381,9 @@ on(type: 'outputDeviceChange', callback: (state: ConnectionState, device: Output **示例:** -```js -currentAVSession.on('outputDeviceChange', (state, device) => { - console.info(`on outputDeviceChange device : ${device}`); +```ts +currentAVSession.on('outputDeviceChange', (state: avSession.ConnectionState, device: avSession.OutputDeviceInfo) => { + console.info(`on outputDeviceChange device : ${device}`); }); ``` @@ -3271,9 +3412,22 @@ on(type: 'commonCommand', callback: (command: string, args: {[key: string]: Obje **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; +import avSession from '@ohos.multimedia.avsession'; +let currentAVSession: avSession.AVSession; +let tag = "createNewSession"; +let context: Context = this.context; + +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + } +}); currentAVSession.on('commonCommand', (commonCommand, args) => { - console.info(`OnCommonCommand, the command is ${commonCommand}, args: ${JSON.stringify(args)}`); + console.info(`OnCommonCommand, the command is ${commonCommand}, args: ${JSON.stringify(args)}`); }); ``` @@ -3302,7 +3456,7 @@ off(type: 'play', callback?: () => void): void **示例:** -```js +```ts currentAVSession.off('play'); ``` @@ -3331,7 +3485,7 @@ off(type: 'pause', callback?: () => void): void **示例:** -```js +```ts currentAVSession.off('pause'); ``` @@ -3360,7 +3514,7 @@ off(type: 'stop', callback?: () => void): void **示例:** -```js +```ts currentAVSession.off('stop'); ``` @@ -3389,7 +3543,7 @@ off(type: 'playNext', callback?: () => void): void **示例:** -```js +```ts currentAVSession.off('playNext'); ``` @@ -3418,7 +3572,7 @@ off(type: 'playPrevious', callback?: () => void): void **示例:** -```js +```ts currentAVSession.off('playPrevious'); ``` @@ -3447,7 +3601,7 @@ off(type: 'fastForward', callback?: () => void): void **示例:** -```js +```ts currentAVSession.off('fastForward'); ``` @@ -3476,7 +3630,7 @@ off(type: 'rewind', callback?: () => void): void **示例:** -```js +```ts currentAVSession.off('rewind'); ``` @@ -3505,7 +3659,7 @@ off(type: 'seek', callback?: (time: number) => void): void **示例:** -```js +```ts currentAVSession.off('seek'); ``` @@ -3534,7 +3688,7 @@ off(type: 'setSpeed', callback?: (speed: number) => void): void **示例:** -```js +```ts currentAVSession.off('setSpeed'); ``` @@ -3563,7 +3717,7 @@ off(type: 'setLoopMode', callback?: (mode: LoopMode) => void): void **示例:** -```js +```ts currentAVSession.off('setLoopMode'); ``` @@ -3592,7 +3746,7 @@ off(type: 'toggleFavorite', callback?: (assetId: string) => void): void **示例:** -```js +```ts currentAVSession.off('toggleFavorite'); ``` @@ -3621,7 +3775,7 @@ off(type: 'skipToQueueItem', callback?: (itemId: number) => void): void **示例:** -```js +```ts currentAVSession.off('skipToQueueItem'); ``` @@ -3650,7 +3804,7 @@ off(type: 'handleKeyEvent', callback?: (event: KeyEvent) => void): void **示例:** -```js +```ts currentAVSession.off('handleKeyEvent'); ``` @@ -3679,7 +3833,7 @@ off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: Outp **示例:** -```js +```ts currentAVSession.off('outputDeviceChange'); ``` @@ -3709,7 +3863,7 @@ off(type: 'commonCommand', callback?: (command: string, args: {[key:string]: Obj **示例:** -```js +```ts currentAVSession.off('commonCommand'); ``` @@ -3736,13 +3890,15 @@ stopCasting(callback: AsyncCallback\): void **示例:** -```js -currentAVSession.stopCasting(function (err) { - if (err) { - console.info(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`stopCasting successfully`); - } +```ts +import { BusinessError } from '@ohos.base'; + +currentAVSession.stopCasting((err: BusinessError) => { + if (err) { + console.info(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`stopCasting successfully`); + } }); ``` @@ -3769,11 +3925,13 @@ stopCasting(): Promise\ **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + currentAVSession.stopCasting().then(() => { - console.info(`stopCasting successfully`); -}).catch((err) => { - console.info(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`stopCasting successfully`); +}).catch((err: BusinessError) => { + console.info(`stopCasting BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -3837,9 +3995,9 @@ setDisplaySurface(surfaceId: string): Promise\ | 6600109 | The remote connection is not established. | **示例:** -```js +```ts aVCastController.setDisplaySurface().then(() => { - console.info(`setDisplaySurface : SUCCESS :`); + console.info(`setDisplaySurface : SUCCESS`); }); ``` @@ -3867,13 +4025,15 @@ setDisplaySurface(surfaceId: string, callback: AsyncCallback\): void | 6600109 | The remote connection is not established. | **示例:** -```js -aVCastController.setDisplaySurface(function (err, value) { - if (err) { - console.info(`setDisplaySurface BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`setDisplaySurface : SUCCESS : state : ${value}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +aVCastController.setDisplaySurface((err: BusinessError) => { + if (err) { + console.info(`setDisplaySurface BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`setDisplaySurface : SUCCESS`); + } }); ``` @@ -3900,13 +4060,15 @@ getAVPlaybackState(callback: AsyncCallback\): void **示例:** -```js -aVCastController.getAVPlaybackState(function (err, state) { - if (err) { - console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`getAVPlaybackState : SUCCESS`); - } +```ts +import { BusinessError } from '@ohos.base'; + +aVCastController.getAVPlaybackState((err: BusinessError, state: avSession.AVPlaybackState) => { + if (err) { + console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`getAVPlaybackState : SUCCESS`); + } }); ``` @@ -3933,11 +4095,13 @@ getAVPlaybackState(): Promise\; **示例:** -```js -aVCastController.getAVPlaybackState().then((state) => { - console.info(`getAVPlaybackState : SUCCESS :`); -}).catch((err) => { - console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +aVCastController.getAVPlaybackState().then((state: avSession.AVPlaybackState) => { + console.info(`getAVPlaybackState : SUCCESS`); +}).catch((err: BusinessError) => { + console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -3973,8 +4137,11 @@ sendControlCommand(command: AVCastControlCommand): Promise\ **示例:** -```js -let avCommand = {command:'play'}; +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let avCommand: avSession.AVCastControlCommand = {command:'play'}; // let avCommand = {command:'pause'}; // let avCommand = {command:'stop'}; // let avCommand = {command:'playNext'}; @@ -3983,9 +4150,9 @@ let avCommand = {command:'play'}; // let avCommand = {command:'rewind'}; // let avCommand = {command:'seek', parameter:10}; aVCastController.sendControlCommand(avCommand).then(() => { - console.info(`SendControlCommand successfully`); -}).catch((err) => { - console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`SendControlCommand successfully`); +}).catch((err: BusinessError) => { + console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -4016,8 +4183,11 @@ sendControlCommand(command: AVCastControlCommand, callback: AsyncCallback\ **示例:** -```js -let avCommand = {command:'play'}; +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let avCommand: avSession.AVCastControlCommand = {command:'play'}; // let avCommand = {command:'pause'}; // let avCommand = {command:'stop'}; // let avCommand = {command:'playNext'}; @@ -4025,12 +4195,12 @@ let avCommand = {command:'play'}; // let avCommand = {command:'fastForward'}; // let avCommand = {command:'rewind'}; // let avCommand = {command:'seek', parameter:10}; -aVCastController.sendControlCommand(avCommand, function (err) { - if (err) { - console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SendControlCommand successfully`); - } +aVCastController.sendControlCommand(avCommand, (err: BusinessError) => { + if (err) { + console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SendControlCommand successfully`); + } }); ``` @@ -4047,7 +4217,7 @@ prepare(item: AVQueueItem, callback: AsyncCallback\): void | 参数名 | 类型 | 必填 | 说明 | | ------- | ------------------------------------- | ---- | ------------------------------ | | item | [AVQueueItem](#avqueueitem10) | 是 | 播放列表中单项的相关属性。 | -| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -4058,13 +4228,15 @@ prepare(item: AVQueueItem, callback: AsyncCallback\): void **示例:** -```js +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + // 设置播放参数,开始播放 -var playItem = { - itemId: 0, - description: { - mediaId: '12345', - mediaName: 'song1', +let playItem: avSession.AVQueueItem = { + itemId: 0, + description: { + assetId: '12345', mediaType: 'AUDIO', mediaUri: 'http://resource1_address', mediaSize: 12345, @@ -4074,13 +4246,16 @@ var playItem = { albumTitle: 'song1_title', albumCoverUri: "http://resource1_album_address", lyricUri: "http://resource1_lyric_address", - iconUri: "http://resource1_icon_address", appName: 'MyMusic' - } + } }; // 准备播放,这个不会触发真正的播放,会进行加载和缓冲 -aVCastController.prepare(playItem, () => { - console.info('prepare done'); +aVCastController.prepare(playItem, (err: BusinessError) => { + if (err) { + console.error(`prepare BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`prepare successfully`); + } }); ``` @@ -4116,29 +4291,32 @@ prepare(item: AVQueueItem): Promise\ **示例:** -```js +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + // 设置播放参数,开始播放 -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' - } +let playItem: avSession.AVQueueItem = { + itemId: 0, + description: { + assetId: '12345', + 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", + appName: 'MyMusic' + } }; // 准备播放,这个不会触发真正的播放,会进行加载和缓冲 -aVCastController.prepare(playItem, () => { - console.info('prepare done'); +aVCastController.prepare(playItem).then(() => { + console.info(`prepare successfully`); +}).catch((err: BusinessError) => { + console.error(`prepare BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -4155,7 +4333,7 @@ start(item: AVQueueItem, callback: AsyncCallback\): void | 参数名 | 类型 | 必填 | 说明 | | ------- | ------------------------------------- | ---- | ------------------------------ | | item | [AVQueueItem](#avqueueitem10) | 是 | 播放列表中单项的相关属性。 | -| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 +| callback | AsyncCallback\ | 是 | 回调函数。当命令发送成功,err为undefined,否则返回错误对象。 | **错误码:** 以下错误码的详细介绍请参见[媒体会话管理错误码](../errorcodes/errorcode-avsession.md)。 @@ -4166,30 +4344,35 @@ start(item: AVQueueItem, callback: AsyncCallback\): void **示例:** -```js +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + // 设置播放参数,开始播放 -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' -} +let playItem: avSession.AVQueueItem = { + itemId: 0, + description: { + assetId: '12345', + 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", + appName: 'MyMusic' + } }; // 启动播放 -aVCastController.start(playItem, () => { - console.info('play done'); +aVCastController.start(playItem, (err: BusinessError) => { + if (err) { + console.error(`start BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`start successfully`); + } }); ``` @@ -4224,13 +4407,15 @@ start(item: AVQueueItem): Promise\ **示例:** -```js +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + // 设置播放参数,开始播放 -var playItem = { -itemId: 0, -description: { - mediaId: '12345', - mediaName: 'song1', +let playItem: avSession.AVQueueItem = { + itemId: 0, + description: { + assetId: '12345', mediaType: 'AUDIO', mediaUri: 'http://resource1_address', mediaSize: 12345, @@ -4240,15 +4425,14 @@ description: { albumTitle: 'song1_title', albumCoverUri: "http://resource1_album_address", lyricUri: "http://resource1_lyric_address", - iconUri: "http://resource1_icon_address", appName: 'MyMusic' -} + } }; // 启动播放 aVCastController.start(playItem).then(() => { - console.info(`start successfully`); -}).catch((err) => { - console.info(`start BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`start successfully`); +}).catch((err: BusinessError) => { + console.info(`start BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -4275,13 +4459,15 @@ getCurrentItem(callback: AsyncCallback\): void **示例:** -```js -aVCastController.getCurrentItem(function (err, value) { - if (err) { - console.error(`getCurrentItem BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`getCurrentItem successfully`); - } +```ts +import { BusinessError } from '@ohos.base'; + +aVCastController.getCurrentItem((err: BusinessError, value: avSession.AVQueueItem) => { + if (err) { + console.error(`getCurrentItem BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`getCurrentItem successfully`); + } }); ``` @@ -4308,12 +4494,15 @@ getCurrentItem(): Promise\ **示例:** -```js -aVCastController.getCurrentItem().then((AVQueueItem) => { - console.info(`getCurrentItem successfully`); -}).catch((err) => { - console.error(`getCurrentItem BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +aVCastController.getCurrentItem().then((value: avSession.AVQueueItem) => { + console.info(`getCurrentItem successfully`); +}).catch((err: BusinessError) => { + console.error(`getCurrentItem BusinessError: code: ${err.code}, message: ${err.message}`); }); + ``` ### on('playbackStateChange')10+ @@ -4341,14 +4530,14 @@ on(type: 'playbackStateChange', filter: Array\ | 'all', c **示例:** -```js -aVCastController.on('playbackStateChange', 'all', (playbackState) => { - console.info(`on playbackStateChange state : ${playbackState.state}`); +```ts +aVCastController.on('playbackStateChange', 'all', (playbackState: avSession.AVPlaybackState) => { + console.info(`on playbackStateChange state : ${playbackState.state}`); }); let playbackFilter = ['state', 'speed', 'loopMode']; -aVCastController.on('playbackStateChange', playbackFilter, (playbackState) => { - console.info(`on playbackStateChange state : ${playbackState.state}`); +aVCastController.on('playbackStateChange', playbackFilter, (playbackState: avSession.AVPlaybackState) => { + console.info(`on playbackStateChange state : ${playbackState.state}`); }); ``` @@ -4376,7 +4565,7 @@ off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void): v **示例:** -```js +```ts aVCastController.off('playbackStateChange'); ``` @@ -4404,9 +4593,9 @@ on(type: 'mediaItemChange', callback: Callback\): void **示例:** -```js -aVCastController.on('mediaItemChange', (item) => { - console.info(`on mediaItemChange state : ${item.itemId}`); +```ts +aVCastController.on('mediaItemChange', (item: avSession.AVQueueItem) => { + console.info(`on mediaItemChange state : ${item.itemId}`); }); ``` @@ -4433,7 +4622,7 @@ off(type: 'mediaItemChange'): void **示例:** -```js +```ts aVCastController.off('mediaItemChange'); ``` @@ -4461,9 +4650,9 @@ on(type: 'playNext', callback: Callback\): void **示例:** -```js +```ts aVCastController.on('playNext', () => { - console.info(`on playNext`); + console.info(`on playNext`); }); ``` @@ -4490,7 +4679,7 @@ off(type: 'playNext'): void **示例:** -```js +```ts aVCastController.off('playNext'); ``` @@ -4518,9 +4707,9 @@ on(type: 'playPrevious', callback: Callback\): void **示例:** -```js +```ts aVCastController.on('playPrevious', () => { - console.info(`on playPrevious`); + console.info(`on playPrevious`); }); ``` @@ -4547,7 +4736,7 @@ off(type: 'playPrevious'): void **示例:** -```js +```ts aVCastController.off('playPrevious'); ``` @@ -4575,9 +4764,9 @@ on(type: 'seekDone', callback: Callback\): void **示例:** -```js -aVCastController.on('seekDone', (pos) => { - console.info(`on seekDone pos:${pos} `); +```ts +aVCastController.on('seekDone', (pos: number) => { + console.info(`on seekDone pos:${pos} `); }); ``` @@ -4604,7 +4793,7 @@ off(type: 'seekDone'): void **示例:** -```js +```ts aVCastController.off('seekDone'); ``` @@ -4626,10 +4815,10 @@ on(type: 'videoSizeChange', callback: (width:number, height:number) => void): vo **示例:** -```js -aVCastController.on('videoSizeChange', (width, height) => { - console.info(`width :${width} `); - console.info(`height:${height} `); +```ts +aVCastController.on('videoSizeChange', (width: number, height: number) => { + console.info(`width :${width} `); + console.info(`height:${height} `); }); ``` @@ -4650,7 +4839,7 @@ off(type: 'videoSizeChange'): void **示例:** -```js +```ts aVCastController.off('videoSizeChange'); ``` @@ -4684,8 +4873,10 @@ on(type: 'error', callback: ErrorCallback): void **示例:** -```js -aVCastController.on('error', (error) => { +```ts +import { BusinessError } from '@ohos.base' + +aVCastController.on('error', (error: BusinessError) => { console.error('error happened,and error message is :' + error.message) console.error('error happened,and error code is :' + error.code) }) @@ -4720,7 +4911,7 @@ off(type: 'error'): void **示例:** -```js +```ts aVCastController.off('error') ``` @@ -4942,12 +5133,14 @@ aVCastController.off('error') **示例:** -```js -let AVSessionController; -avSession.createController(currentAVSession.sessionId).then((controller) => { - AVSessionController = controller; -}).catch((err) => { - console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +let AVSessionController: avSession.AVSessionController; +avSession.createController(currentAVSession.sessionId).then((controller: avSession.AVSessionController) => { + AVSessionController = controller; +}).catch((err: BusinessError) => { + console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -4976,13 +5169,15 @@ getAVPlaybackState(callback: AsyncCallback\): void **示例:** -```js -avsessionController.getAVPlaybackState(function (err, state) { - if (err) { - console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`getAVPlaybackState : SUCCESS`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getAVPlaybackState((err: BusinessError, state: avSession.AVPlaybackState) => { + if (err) { + console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`getAVPlaybackState : SUCCESS`); + } }); ``` @@ -5011,11 +5206,13 @@ getAVPlaybackState(): Promise\; **示例:** -```js -avsessionController.getAVPlaybackState().then((state) => { - console.info(`getAVPlaybackState : SUCCESS :`); -}).catch((err) => { - console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getAVPlaybackState().then((state: avSession.AVPlaybackState) => { + console.info(`getAVPlaybackState : SUCCESS`); +}).catch((err: BusinessError) => { + console.error(`getAVPlaybackState BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -5043,11 +5240,13 @@ getAVMetadata(): Promise\ | 6600103 | The session controller does not exist. | **示例:** -```js -avsessionController.getAVMetadata().then((metadata) => { - console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); -}).catch((err) => { - console.error(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getAVMetadata().then((metadata: avSession.AVMetadata) => { + console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); +}).catch((err: BusinessError) => { + console.error(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -5075,13 +5274,15 @@ getAVMetadata(callback: AsyncCallback\): void | 6600103 | The session controller does not exist. | **示例:** -```js -avsessionController.getAVMetadata(function (err, metadata) { - if (err) { - console.error(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getAVMetadata((err: BusinessError, metadata: avSession.AVMetadata) => { + if (err) { + console.error(`GetAVMetadata BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetAVMetadata : SUCCESS : assetId : ${metadata.assetId}`); + } }); ``` @@ -5109,11 +5310,13 @@ getAVQueueTitle(): Promise\ | 6600103 | The session controller does not exist. | **示例:** -```js -avsessionController.getAVQueueTitle().then((title) => { - console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); -}).catch((err) => { - console.error(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getAVQueueTitle().then((title: string) => { + console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); +}).catch((err: BusinessError) => { + console.error(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -5141,13 +5344,15 @@ getAVQueueTitle(callback: AsyncCallback\): void | 6600103 | The session controller does not exist. | **示例:** -```js -avsessionController.getAVQueueTitle(function (err, title) { - if (err) { - console.error(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getAVQueueTitle((err: BusinessError, title: string) => { + if (err) { + console.error(`GetAVQueueTitle BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetAVQueueTitle : SUCCESS : title : ${title}`); + } }); ``` @@ -5175,11 +5380,13 @@ getAVQueueItems(): Promise\> | 6600103 | The session controller does not exist. | **示例:** -```js -avsessionController.getAVQueueItems().then((items) => { - console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); -}).catch((err) => { - console.error(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getAVQueueItems().then((items: avSession.AVQueueItem[]) => { + console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); +}).catch((err: BusinessError) => { + console.error(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -5207,13 +5414,15 @@ getAVQueueItems(callback: AsyncCallback\>): void | 6600103 | The session controller does not exist. | **示例:** -```js -avsessionController.getAVQueueItems(function (err, items) { - if (err) { - console.error(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getAVQueueItems((err: BusinessError, items: avSession.AVQueueItem[]) => { + if (err) { + console.error(`GetAVQueueItems BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetAVQueueItems : SUCCESS : length : ${items.length}`); + } }); ``` @@ -5248,12 +5457,14 @@ skipToQueueItem(itemId: number): Promise\ **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + let queueItemId = 0; avsessionController.skipToQueueItem(queueItemId).then(() => { - console.info(`SkipToQueueItem successfully`); -}).catch((err) => { - console.error(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`SkipToQueueItem successfully`); +}).catch((err: BusinessError) => { + console.error(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -5283,14 +5494,16 @@ skipToQueueItem(itemId: number, callback: AsyncCallback\): void **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + let queueItemId = 0; -avsessionController.skipToQueueItem(queueItemId, function (err) { - if (err) { - console.error(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SkipToQueueItem successfully`); - } +avsessionController.skipToQueueItem(queueItemId, (err: BusinessError) => { + if (err) { + console.error(`SkipToQueueItem BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SkipToQueueItem successfully`); + } }); ``` @@ -5318,11 +5531,13 @@ getOutputDevice(): Promise\ | 600103 | The session controller does not exist. | **示例:** -```js -avsessionController.getOutputDevice().then((deviceInfo) => { - console.info(`GetOutputDevice : SUCCESS : isRemote : ${deviceInfo.isRemote}`); -}).catch((err) => { - console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getOutputDevice().then((deviceInfo: avSession.OutputDeviceInfo) => { + console.info(`GetOutputDevice : SUCCESS`); +}).catch((err: BusinessError) => { + console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -5350,13 +5565,15 @@ getOutputDevice(callback: AsyncCallback\): void **示例:** -```js -avsessionController.getOutputDevice(function (err, deviceInfo) { - if (err) { - console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`GetOutputDevice : SUCCESS : isRemote : ${deviceInfo.isRemote}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getOutputDevice((err: BusinessError, deviceInfo: avSession.OutputDeviceInfo) => { + if (err) { + console.error(`GetOutputDevice BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetOutputDevice : SUCCESS`); + } }); ``` @@ -5393,14 +5610,17 @@ sendAVKeyEvent(event: KeyEvent): Promise\ **示例:** -```js -let keyItem = {code:0x49, pressedTime:2, deviceId:0}; -let event = {action:2, key:keyItem, keys:[keyItem]}; +```ts +import keyEvent from '@ohos.multimodalInput.keyEvent'; +import { BusinessError } from '@ohos.base'; + +let keyItem: keyEvent.Key = {code:0x49, pressedTime:2, deviceId:0}; +let event: keyEvent.KeyEvent = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; avsessionController.sendAVKeyEvent(event).then(() => { - console.info(`SendAVKeyEvent Successfully`); -}).catch((err) => { - console.error(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`SendAVKeyEvent Successfully`); +}).catch((err: BusinessError) => { + console.error(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -5432,16 +5652,19 @@ sendAVKeyEvent(event: KeyEvent, callback: AsyncCallback\): void **示例:** -```js -let keyItem = {code:0x49, pressedTime:2, deviceId:0}; -let event = {action:2, key:keyItem, keys:[keyItem]}; +```ts +import keyEvent from '@ohos.multimodalInput.keyEvent'; +import { BusinessError } from '@ohos.base'; -avsessionController.sendAVKeyEvent(event, function (err) { - if (err) { - console.error(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`SendAVKeyEvent Successfully`); - } +let keyItem: keyEvent.Key = {code:0x49, pressedTime:2, deviceId:0}; +let event: keyEvent.KeyEvent = {id:1, deviceId:0, actionTime:1, screenId:1, windowId:1, action:2, key:keyItem, unicodeChar:0, keys:[keyItem], ctrlKey:false, altKey:false, shiftKey:false, logoKey:false, fnKey:false, capsLock:false, numLock:false, scrollLock:false}; + +avsessionController.sendAVKeyEvent(event, (err: BusinessError) => { + if (err) { + console.error(`SendAVKeyEvent BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`SendAVKeyEvent Successfully`); + } }); ``` @@ -5470,11 +5693,13 @@ getLaunchAbility(): Promise\ **示例:** -```js -avsessionController.getLaunchAbility().then((agent) => { - console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); -}).catch((err) => { - console.error(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getLaunchAbility().then((agent: object) => { + console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); +}).catch((err: BusinessError) => { + console.error(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -5503,13 +5728,15 @@ getLaunchAbility(callback: AsyncCallback\): void **示例:** -```js -avsessionController.getLaunchAbility(function (err, agent) { - if (err) { - console.error(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getLaunchAbility((err: BusinessError, agent: object) => { + if (err) { + console.error(`GetLaunchAbility BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetLaunchAbility : SUCCESS : wantAgent : ${agent}`); + } }); ``` @@ -5537,8 +5764,8 @@ getRealPlaybackPositionSync(): number **示例:** -```js -let time = avsessionController.getRealPlaybackPositionSync(); +```ts +let time: number = avsessionController.getRealPlaybackPositionSync(); ``` ### isActive10+ @@ -5566,11 +5793,13 @@ isActive(): Promise\ **示例:** -```js -avsessionController.isActive().then((isActive) => { - console.info(`IsActive : SUCCESS : isactive : ${isActive}`); -}).catch((err) => { - console.error(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.isActive().then((isActive: boolean) => { + console.info(`IsActive : SUCCESS : isactive : ${isActive}`); +}).catch((err: BusinessError) => { + console.error(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -5599,13 +5828,15 @@ isActive(callback: AsyncCallback\): void **示例:** -```js -avsessionController.isActive(function (err, isActive) { - if (err) { - console.error(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`IsActive : SUCCESS : isactive : ${isActive}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.isActive((err: BusinessError, isActive: boolean) => { + if (err) { + console.error(`IsActive BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`IsActive : SUCCESS : isactive : ${isActive}`); + } }); ``` @@ -5633,11 +5864,13 @@ destroy(): Promise\ **示例:** -```js +```ts +import { BusinessError } from '@ohos.base'; + avsessionController.destroy().then(() => { - console.info(`Destroy : SUCCESS `); -}).catch((err) => { - console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`Destroy : SUCCESS `); +}).catch((err: BusinessError) => { + console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -5665,13 +5898,15 @@ destroy(callback: AsyncCallback\): void **示例:** -```js -avsessionController.destroy(function (err) { - if (err) { - console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`Destroy : SUCCESS `); - } +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.destroy((err: BusinessError) => { + if (err) { + console.error(`Destroy BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`Destroy : SUCCESS `); + } }); ``` @@ -5700,11 +5935,13 @@ getValidCommands(): Promise\> **示例:** -```js -avsessionController.getValidCommands.then((validCommands) => { - console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); -}).catch((err) => { - console.error(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getValidCommands.then((validCommands: avSession.AVControlCommandType[]) => { + console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); +}).catch((err: BusinessError) => { + console.error(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -5733,13 +5970,15 @@ getValidCommands(callback: AsyncCallback\>): void **示例:** -```js -avsessionController.getValidCommands(function (err, validCommands) { - if (err) { - console.error(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); - } +```ts +import { BusinessError } from '@ohos.base'; + +avsessionController.getValidCommands((err: BusinessError, validCommands: avSession.AVControlCommandType[]) => { + if (err) { + console.error(`GetValidCommands BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`GetValidCommands : SUCCESS : size : ${validCommands.length}`); + } }); ``` @@ -5781,8 +6020,11 @@ sendControlCommand(command: AVControlCommand): Promise\ **示例:** -```js -let avCommand = {command:'play'}; +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let avCommand: avSession.AVControlCommand = {command:'play'}; // let avCommand = {command:'pause'}; // let avCommand = {command:'stop'}; // let avCommand = {command:'playNext'}; @@ -5794,9 +6036,9 @@ let avCommand = {command:'play'}; // let avCommand = {command:'setLoopMode', parameter:avSession.LoopMode.LOOP_MODE_SINGLE}; // let avCommand = {command:'toggleFavorite', parameter:"false"}; avsessionController.sendControlCommand(avCommand).then(() => { - console.info(`SendControlCommand successfully`); -}).catch((err) => { - console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); + console.info(`SendControlCommand successfully`); +}).catch((err: BusinessError) => { + console.error(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -5833,8 +6075,11 @@ sendControlCommand(command: AVControlCommand, callback: AsyncCallback\): v **示例:** -```js -let avCommand = {command:'play'}; +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let avCommand: avSession.AVControlCommand = {command:'play'}; // let avCommand = {command:'pause'}; // let avCommand = {command:'stop'}; // let avCommand = {command:'playNext'}; @@ -5845,12 +6090,12 @@ let avCommand = {command:'play'}; // let avCommand = {command:'setSpeed', parameter:2.6}; // let avCommand = {command:'setLoopMode', parameter:avSession.LoopMode.LOOP_MODE_SINGLE}; // let avCommand = {command:'toggleFavorite', parameter:"false"}; -avsessionController.sendControlCommand(avCommand, function (err) { - if (err) { - console.info(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.error(`SendControlCommand successfully`); - } +avsessionController.sendControlCommand(avCommand, (err: BusinessError) => { + if (err) { + console.info(`SendControlCommand BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.error(`SendControlCommand successfully`); + } }); ``` @@ -5892,13 +6137,33 @@ sendCommonCommand(command: string, args: {[key: string]: Object}): Promise\ { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + } +}); +avSession.createController(currentAVSession.sessionId).then((controller: avSession.AVSessionController) => { + avSessionController = controller; +}).catch((err: BusinessError) => { + console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); +}); + let commandName = "my_command"; -let args = { - command : "This is my command" -} -avsessionController.sendCommonCommand(commandName, args).catch((err) => { - console.info(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); +avSessionController.sendCommonCommand(commandName, {command : "This is my command"}).then(() => { + console.info(`SendCommonCommand successfully`); +}).catch((err: BusinessError) => { + console.info(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); }) ``` @@ -5935,15 +6200,32 @@ sendCommonCommand(command: string, args: {[key: string]: Object}, callback: Asyn **示例:** -```js +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; +let avSessionController: avSession.AVSessionController; +let currentAVSession: avSession.AVSession; +let tag = "createNewSession"; +let context: Context = this.context; + +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + } +}); +avSession.createController(currentAVSession.sessionId).then((controller: avSession.AVSessionController) => { + avSessionController = controller; +}).catch((err: BusinessError) => { + console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); +}); + let commandName = "my_command"; -let args = { - command : "This is my command" -} -avsessionController.sendCommonCommand(commandName, args, (err) => { - if(err) { - console.info(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); - } +avSessionController.sendCommonCommand(commandName, {command : "This is my command"}, (err: BusinessError) => { + if(err) { + console.info(`SendCommonCommand BusinessError: code: ${err.code}, message: ${err.message}`); + } }) ``` @@ -5973,9 +6255,32 @@ getExtras(): Promise\<{[key: string]: Object}> | 6600107 | Too many commands or events. | **示例:** -```js -let extras = await avsessionController.getExtras().catch((err) => { - console.info(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let avSessionController: avSession.AVSessionController; +let currentAVSession: avSession.AVSession; +let tag = "createNewSession"; +let context: Context = this.context; + +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + } +}); +avSession.createController(currentAVSession.sessionId).then((controller: avSession.AVSessionController) => { + avSessionController = controller; +}).catch((err: BusinessError) => { + console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); +}); + +avSessionController.getExtras().then((extras) => { + console.info(`getExtras : SUCCESS : ${extras}`); +}).catch((err: BusinessError) => { + console.info(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); }); ``` @@ -6005,13 +6310,34 @@ getExtras(callback: AsyncCallback\<{[key: string]: Object}>): void | 6600107 | Too many commands or events. | **示例:** -```js -avsessionController.getExtras(function (err, extras) { - if (err) { - console.error(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); - } else { - console.info(`getExtras : SUCCESS : assetId : ${extras}`); - } +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let avSessionController: avSession.AVSessionController; +let currentAVSession: avSession.AVSession; +let tag = "createNewSession"; +let context: Context = this.context; + +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + } +}); +avSession.createController(currentAVSession.sessionId).then((controller: avSession.AVSessionController) => { + avSessionController = controller; +}).catch((err: BusinessError) => { + console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); +}); + +avSessionController.getExtras((err, extras) => { + if (err) { + console.error(`getExtras BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + console.info(`getExtras : SUCCESS : ${extras}`); + } }); ``` @@ -6041,15 +6367,15 @@ on(type: 'metadataChange', filter: Array\ | 'all', callback: ( **示例:** -```js -avsessionController.on('metadataChange', 'all', (metadata) => { - console.info(`on metadataChange assetId : ${metadata.assetId}`); +```ts +avsessionController.on('metadataChange', 'all', (metadata: avSession.AVMetadata) => { + console.info(`on metadataChange assetId : ${metadata.assetId}`); }); -let metaFilter = ['assetId', 'title', 'description']; -avsessionController.on('metadataChange', metaFilter, (metadata) => { - console.info(`on metadataChange assetId : ${metadata.assetId}`); +avsessionController.on('metadataChange', ['assetId', 'title', 'description'], (metadata: avSession.AVMetadata) => { + console.info(`on metadataChange assetId : ${metadata.assetId}`); }); + ``` ### off('metadataChange')10+ @@ -6077,7 +6403,7 @@ off(type: 'metadataChange', callback?: (data: AVMetadata) => void) **示例:** -```js +```ts avsessionController.off('metadataChange'); ``` @@ -6107,14 +6433,13 @@ on(type: 'playbackStateChange', filter: Array\ | 'all', c **示例:** -```js -avsessionController.on('playbackStateChange', 'all', (playbackState) => { - console.info(`on playbackStateChange state : ${playbackState.state}`); +```ts +avsessionController.on('playbackStateChange', 'all', (playbackState: avSession.AVPlaybackState) => { + console.info(`on playbackStateChange state : ${playbackState.state}`); }); -let playbackFilter = ['state', 'speed', 'loopMode']; -avsessionController.on('playbackStateChange', playbackFilter, (playbackState) => { - console.info(`on playbackStateChange state : ${playbackState.state}`); +avsessionController.on('playbackStateChange', ['state', 'speed', 'loopMode'], (playbackState: avSession.AVPlaybackState) => { + console.info(`on playbackStateChange state : ${playbackState.state}`); }); ``` @@ -6143,7 +6468,7 @@ off(type: 'playbackStateChange', callback?: (state: AVPlaybackState) => void) **示例:** -```js +```ts avsessionController.off('playbackStateChange'); ``` @@ -6172,9 +6497,9 @@ on(type: 'sessionDestroy', callback: () => void) **示例:** -```js +```ts avsessionController.on('sessionDestroy', () => { - console.info(`on sessionDestroy : SUCCESS `); + console.info(`on sessionDestroy : SUCCESS `); }); ``` @@ -6203,7 +6528,7 @@ off(type: 'sessionDestroy', callback?: () => void) **示例:** -```js +```ts avsessionController.off('sessionDestroy'); ``` @@ -6232,9 +6557,9 @@ on(type: 'activeStateChange', callback: (isActive: boolean) => void) **示例:** -```js -avsessionController.on('activeStateChange', (isActive) => { - console.info(`on activeStateChange : SUCCESS : isActive ${isActive}`); +```ts +avsessionController.on('activeStateChange', (isActive: boolean) => { + console.info(`on activeStateChange : SUCCESS : isActive ${isActive}`); }); ``` @@ -6263,7 +6588,7 @@ off(type: 'activeStateChange', callback?: (isActive: boolean) => void) **示例:** -```js +```ts avsessionController.off('activeStateChange'); ``` @@ -6292,10 +6617,10 @@ on(type: 'validCommandChange', callback: (commands: Array\ **示例:** -```js -avsessionController.on('validCommandChange', (validCommands) => { - console.info(`validCommandChange : SUCCESS : size : ${validCommands.size}`); - console.info(`validCommandChange : SUCCESS : validCommands : ${validCommands.values()}`); +```ts +avsessionController.on('validCommandChange', (validCommands: avSession.AVControlCommandType[]) => { + console.info(`validCommandChange : SUCCESS : size : ${validCommands.length}`); + console.info(`validCommandChange : SUCCESS : validCommands : ${validCommands.values()}`); }); ``` @@ -6324,7 +6649,7 @@ off(type: 'validCommandChange', callback?: (commands: Array\ { - console.info(`on outputDeviceChange state: ${state}, device : ${device}`); +```ts +avsessionController.on('outputDeviceChange', (state: avSession.ConnectionState, device: avSession.OutputDeviceInfo) => { + console.info(`on outputDeviceChange state: ${state}, device : ${device}`); }); ``` @@ -6384,7 +6709,7 @@ off(type: 'outputDeviceChange', callback?: (state: ConnectionState, device: Outp **示例:** -```js +```ts avsessionController.off('outputDeviceChange'); ``` @@ -6414,9 +6739,30 @@ on(type: 'sessionEvent', callback: (sessionEvent: string, args: {[key:string]: O **示例:** -```js -avsessionController.on('sessionEvent', (sessionEvent, args) => { - console.info(`OnSessionEvent, sessionEvent is ${sessionEvent}, args: ${JSON.stringify(args)}`); +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let avSessionController: avSession.AVSessionController; +let currentAVSession: avSession.AVSession; +let tag = "createNewSession"; +let context: Context = this.context; + +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + } +}); +avSession.createController(currentAVSession.sessionId).then((controller: avSession.AVSessionController) => { + avSessionController = controller; +}).catch((err: BusinessError) => { + console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); +}); + +avSessionController.on('sessionEvent', (sessionEvent, args) => { + console.info(`OnSessionEvent, sessionEvent is ${sessionEvent}, args: ${JSON.stringify(args)}`); }); ``` @@ -6445,7 +6791,7 @@ off(type: 'sessionEvent', callback?: (sessionEvent: string, args: {[key:string]: **示例:** -```js +```ts avsessionController.off('sessionEvent'); ``` @@ -6474,9 +6820,9 @@ on(type: 'queueItemsChange', callback: (items: Array<[AVQueueItem](#avqueueitem1 **示例:** -```js -avsessionController.on('queueItemsChange', (items) => { - console.info(`OnQueueItemsChange, items length is ${items.length}`); +```ts +avsessionController.on('queueItemsChange', (items: avSession.AVQueueItem[]) => { + console.info(`OnQueueItemsChange, items length is ${items.length}`); }); ``` @@ -6505,7 +6851,7 @@ off(type: 'queueItemsChange', callback?: (items: Array<[AVQueueItem](#avqueueite **示例:** -```js +```ts avsessionController.off('queueItemsChange'); ``` @@ -6534,9 +6880,9 @@ on(type: 'queueTitleChange', callback: (title: string) => void): void **示例:** -```js -avsessionController.on('queueTitleChange', (title) => { - console.info(`queueTitleChange, title is ${title}`); +```ts +avsessionController.on('queueTitleChange', (title: string) => { + console.info(`queueTitleChange, title is ${title}`); }); ``` @@ -6565,7 +6911,7 @@ off(type: 'queueTitleChange', callback?: (title: string) => void): void **示例:** -```js +```ts avsessionController.off('queueTitleChange'); ``` @@ -6594,9 +6940,30 @@ on(type: 'extrasChange', callback: (extras: {[key:string]: Object}) => void): vo **示例:** -```js -avsessionController.on('extrasChange', (extras) => { - console.info(`Caught extrasChange event,the new extra is: ${JSON.stringify(extras)}`); +```ts +import avSession from '@ohos.multimedia.avsession'; +import { BusinessError } from '@ohos.base'; + +let avSessionController: avSession.AVSessionController; +let currentAVSession: avSession.AVSession; +let tag = "createNewSession"; +let context: Context = this.context; + +avSession.createAVSession(context, tag, "audio", (err: BusinessError, data: avSession.AVSession) => { + if (err) { + console.info(`CreateAVSession BusinessError: code: ${err.code}, message: ${err.message}`); + } else { + currentAVSession = data; + } +}); +avSession.createController(currentAVSession.sessionId).then((controller: avSession.AVSessionController) => { + avSessionController = controller; +}).catch((err: BusinessError) => { + console.error(`CreateController BusinessError: code: ${err.code}, message: ${err.message}`); +}); + +avSessionController.on('extrasChange', (extras) => { + console.info(`Caught extrasChange event,the new extra is: ${JSON.stringify(extras)}`); }); ``` @@ -6625,7 +6992,7 @@ off(type: 'extrasChange', callback?: (extras: {[key:string]: Object}) => void): **示例:** -```js +```ts avsessionController.off('extrasChange'); ``` -- GitLab