1/* 2 * Copyright (c) 2022 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 20class MediaUtils { 21 private tag: string = 'MediaUtils' 22 private mediaList: Array<mediaLibrary.FileAsset> = [] 23 public mediaLib: mediaLibrary.MediaLibrary = undefined 24 25 async queryFile(id) { 26 Logger.info(this.tag, `queryFile,id = ${id}`) 27 let fileKeyObj = mediaLibrary.FileKey 28 if (!id) { 29 return 30 } 31 let args = id.toString() 32 let fetchOp = { 33 selections: `${fileKeyObj.ID}=?`, 34 selectionArgs: [args], 35 } 36 const fetchFileResult = await this.mediaLib.getFileAssets(fetchOp) 37 Logger.info(this.tag, `fetchFileResult.getCount() = ${fetchFileResult.getCount()}`) 38 const fileAsset = await fetchFileResult.getAllObject() 39 return fileAsset[0] 40 41 } 42 43 async getFdPath(fileAsset: any) { 44 let fd = await fileAsset.open('Rw') 45 Logger.info(this.tag, `fd = ${fd}`) 46 return fd 47 } 48 49 async getFileAssetsFromType(mediaType: number) { 50 Logger.info(this.tag, `getFileAssetsFromType,mediaType = ${mediaType}`) 51 let fileKeyObj = mediaLibrary.FileKey 52 let fetchOp = { 53 selections: `${fileKeyObj.MEDIA_TYPE}=?`, 54 selectionArgs: [`${mediaType}`], 55 } 56 let fetchFileResult = await this.mediaLib.getFileAssets(fetchOp) 57 Logger.info(this.tag, `getFileAssetsFromType,fetchFileResult.count = ${fetchFileResult.getCount()}`) 58 if (fetchFileResult.getCount() > 0) { 59 this.mediaList = await fetchFileResult.getAllObject() 60 } 61 return this.mediaList 62 } 63 64 deleteFile(media: any) { 65 let uri = media.uri 66 Logger.info(this.tag, `deleteFile,uri = ${uri}`) 67 return this.mediaLib.deleteAsset(uri) 68 } 69 70 onDateChange(audioCallback: () => void, videoCallback: () => void) { 71 this.mediaLib.on('audioChange', () => { 72 Logger.info(this.tag, 'videoChange called') 73 audioCallback() 74 }) 75 this.mediaLib.on('videoChange', () => { 76 Logger.info(this.tag, 'videoChange called') 77 videoCallback() 78 }) 79 } 80 81 offDateChange() { 82 this.mediaLib.off('videoChange') 83 this.mediaLib.off('audioChange') 84 } 85} 86 87export default new MediaUtils()