1/* 2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16// @ts-nocheck 17import mediaLibrary from '@ohos.multimedia.mediaLibrary'; 18import Logger from '../model/Logger'; 19 20const TAG: string = 'MediaUtils'; 21 22class MediaUtils { 23 private mediaList: mediaLibrary.FileAsset[] = []; 24 public mediaLib: mediaLibrary.MediaLibrary = undefined; 25 26 async queryFile(id: string): Promise<mediaLibrary.FileAsset> { 27 Logger.info(TAG, `queryFile,id = ${id}`); 28 const fetchOp: mediaLibrary.MediaFetchOptions = { 29 selections: `${mediaLibrary.FileKey.ID}=?`, 30 selectionArgs: [id], 31 }; 32 const fetchFileResult = await this.mediaLib.getFileAssets(fetchOp); 33 Logger.info(TAG, `fetchFileResult.getCount() = ${fetchFileResult.getCount()}`); 34 const fileAsset = await fetchFileResult.getAllObject(); 35 return fileAsset[0]; 36 } 37 38 async getFdPath(fileAsset: any) { 39 let fd = await fileAsset.open('Rw'); 40 Logger.info(TAG, `fd = ${fd}`); 41 return fd; 42 } 43 44 async getFileAssetsFromType(mediaType: number) { 45 Logger.info(TAG, `getFileAssetsFromType,mediaType = ${mediaType}`); 46 let fileKeyObj = mediaLibrary.FileKey; 47 let fetchOp = { 48 selections: `${fileKeyObj.MEDIA_TYPE}=?`, 49 selectionArgs: [`${mediaType}`], 50 }; 51 let fetchFileResult = await this.mediaLib.getFileAssets(fetchOp); 52 Logger.info(TAG, `getFileAssetsFromType,fetchFileResult.count = ${fetchFileResult.getCount()}`); 53 if (fetchFileResult.getCount() > 0) { 54 this.mediaList = await fetchFileResult.getAllObject(); 55 } 56 return this.mediaList; 57 } 58 59 deleteFile(fileAsset: mediaLibrary.FileAsset): Promise<void> { 60 Logger.info(TAG, `deleteFile,displayName=${fileAsset.displayName},uri = ${fileAsset.uri}`); 61 return fileAsset.trash(true); 62 } 63 64 onDateChange(audioCallback: () => void, videoCallback: () => void) { 65 this.mediaLib.on('audioChange', () => { 66 Logger.info(TAG, 'videoChange called'); 67 audioCallback(); 68 }); 69 this.mediaLib.on('videoChange', () => { 70 Logger.info(TAG, 'videoChange called'); 71 videoCallback(); 72 }); 73 } 74 75 offDateChange() { 76 this.mediaLib.off('videoChange'); 77 this.mediaLib.off('audioChange'); 78 } 79} 80 81export default new MediaUtils();