diff --git a/RationalUseOfFrontEndResources/entry/src/main/ets/pages/MusicPlayRule.ets b/RationalUseOfFrontEndResources/entry/src/main/ets/pages/MusicPlayRule.ets index 287e1e1e30f718885ff2bc648f4b7fdce52af42d..347c9a0212b8a19a767aa0310ff9e9f806a199b8 100644 --- a/RationalUseOfFrontEndResources/entry/src/main/ets/pages/MusicPlayRule.ets +++ b/RationalUseOfFrontEndResources/entry/src/main/ets/pages/MusicPlayRule.ets @@ -71,41 +71,47 @@ audio.createAudioRenderer(audioRendererOptions, (err, data) => { const uiContext: UIContext | undefined = AppStorage.get('uiContext'); let context: Context = uiContext?.getHostContext()!; -async function setListener() { +async function setListener(): Promise { // Assuming that a session has been created, see the previous example for how to create a session let type: avSession.AVSessionType = 'audio'; - let session = await avSession.createAVSession(context, 'SESSION_NAME', type); + avSession.createAVSession(context, 'SESSION_NAME', type) + .then((session: avSession.AVSession) => { + // Set the duration of the property + let metadata: avSession.AVMetadata = { + assetId: '0', + title: 'TITLE', + mediaImage: 'IMAGE', + duration: 23000, // The duration of the resource, measured in milliseconds + }; - // Set the duration of the property - let metadata: avSession.AVMetadata = { - assetId: '0', - title: 'TITLE', - mediaImage: 'IMAGE', - duration: 23000, // The duration of the resource, measured in milliseconds - }; - session.setAVMetadata(metadata).then(() => { - hilog.info(0x0000, 'Sample', `SetAVMetadata successfully`); - }).catch((err: BusinessError) => { - hilog.error(0x0000, 'Sample', `Failed to set AVMetadata. Code: ${err.code}, message: ${err.message}`); - }); + session.setAVMetadata(metadata) + .then(() => { + hilog.info(0x0000, 'Sample', `SetAVMetadata successfully`); + }).catch((err: BusinessError) => { + hilog.error(0x0000, 'Sample', `Failed to set AVMetadata. Code: ${err.code}, message: ${err.message}`); + }); - // Set Status: Playback Status, Progress Position, Playback Speed, Cache Time - let playbackState: avSession.AVPlaybackState = { - state: avSession.PlaybackState.PLAYBACK_STATE_PLAY, // Playback status - position: { - elapsedTime: 1000, // The position that has been played, in ms - updateTime: new Date().getTime(), // The timestamp of when the app updated the current location, in ms - }, - speed: 1.0, // Optional, the default is 1.0, the speed of playback, set according to the speed supported in the app, the system does not do verification - bufferedTime: 14000, // Optional, the time for which the resource is cached, in ms - }; - session.setAVPlaybackState(playbackState, (err) => { - if (err) { - hilog.error(0x0000, 'Sample', `Failed to set AVPlaybackState. Code: ${err.code}, message: ${err.message}`); - } else { - hilog.info(0x0000, 'Sample', `SetAVPlaybackState successfully`); - } - }); + // Set Status: Playback Status, Progress Position, Playback Speed, Cache Time + let playbackState: avSession.AVPlaybackState = { + state: avSession.PlaybackState.PLAYBACK_STATE_PLAY, // Playback status + position: { + elapsedTime: 1000, // The position that has been played, in ms + updateTime: new Date().getTime(), // The timestamp of when the app updated the current location, in ms + }, + speed: 1.0, // Optional, the default is 1.0, the speed of playback, set according to the speed supported in the app, the system does not do verification + bufferedTime: 14000, // Optional, the time for which the resource is cached, in ms + }; + session.setAVPlaybackState(playbackState, (err) => { + if (err) { + hilog.error(0x0000, 'Sample', `Failed to set AVPlaybackState. Code: ${err.code}, message: ${err.message}`); + } else { + hilog.info(0x0000, 'Sample', `SetAVPlaybackState successfully`); + } + }); + }) + .catch((err: BusinessError) => { + hilog.error(0x0000, 'Sample', `Failed to set createAVSession. Code: ${err.code}, message: ${err.message}`); + }); } // [End setListener] \ No newline at end of file diff --git a/RationalUseOfFrontEndResources/entry/src/main/ets/pages/VideoScenesROMRule.ets b/RationalUseOfFrontEndResources/entry/src/main/ets/pages/VideoScenesROMRule.ets index 29d9c9651f36fcb33de35aab60862c712cd30155..c53dc1fa06bdf407140014c9ee584c910158a74c 100644 --- a/RationalUseOfFrontEndResources/entry/src/main/ets/pages/VideoScenesROMRule.ets +++ b/RationalUseOfFrontEndResources/entry/src/main/ets/pages/VideoScenesROMRule.ets @@ -21,19 +21,28 @@ import { fileIo as fileIo } from '@kit.CoreFileKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; -const pathDir = "" +const pathDir = ""; // [Start filePath] let filePath = pathDir + "/test.txt"; -let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); let str: string = "hello, world"; -// Use asynchronous methods to write files to the ROM -fileIo.write(file.fd, str).then((writeLen: number) => { - hilog.info(0x0000, 'Sample', 'write data to file succeed and size is:' + writeLen); -}).catch((err: BusinessError) => { - hilog.error(0x0000, 'Sample', 'write data to file failed with error message: ' + err.message + ', error code: ' + err.code); -}).finally(() => { - fileIo.closeSync(file); -}); +try { + let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); + // Use asynchronous methods to write files to the ROM + fileIo.write(file.fd, str) + .then((writeLen: number) => { + hilog.info(0x0000, 'Sample', 'write data to file succeed and size is:' + writeLen); + }) + .catch((err: BusinessError) => { + hilog.error(0x0000, 'Sample', + 'write data to file failed with error message: ' + err.message + ', error code: ' + err.code); + }) + .finally(() => { + fileIo.closeSync(file); + }); +} catch (error) { + let err = error as BusinessError; + hilog.error(0x0000, 'Sample', `Failed to fileIo openSync, code=${err.code}, message=${err.message}`); +} // [End filePath]