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 16import mediaLibrary from '@ohos.multimedia.mediaLibrary' 17import dataStorage from '@ohos.data.storage' 18import featureAbility from '@ohos.ability.featureAbility' 19import DateTimeUtil from '../model/DateTimeUtil' 20import Logger from './Logger' 21import { Record } from './Record' 22 23const TAG = '[Recorder.MediaManager]' 24let PATH: string = '' 25 26class MediaManager { 27 private mediaTest: mediaLibrary.MediaLibrary = mediaLibrary.getMediaLibrary() 28 private storage: any = undefined 29 30 constructor() { 31 this.initStorage() 32 } 33 34 initStorage() { 35 let context = featureAbility.getContext() 36 context.getFilesDir().then(path => { 37 PATH = path + '/' 38 console.info(`${TAG}create store PATH=${PATH}`) 39 this.storage = dataStorage.getStorageSync(path + '/myStore') 40 console.info(`${TAG}create store success`) 41 }) 42 } 43 44 async createAudioFile() { 45 this.mediaTest = mediaLibrary.getMediaLibrary() 46 let info = { 47 suffix: '.m4a', directory: mediaLibrary.DirectoryType.DIR_AUDIO 48 } 49 let dateTimeUtil = new DateTimeUtil() 50 let name = `${dateTimeUtil.getDate()}_${dateTimeUtil.getTime()}` 51 let displayName = `${name}${info.suffix}` 52 Logger.info(TAG, `createAudioFile displayName=${displayName}`) 53 let publicPath = await this.mediaTest.getPublicDirectory(info.directory) 54 Logger.info(TAG, `createAudioFile publicPath=${publicPath}`) 55 return await this.mediaTest.createAsset(mediaLibrary.MediaType.AUDIO, displayName, publicPath) 56 } 57 58 async queryAllAudios() { 59 let fileKeyObj = mediaLibrary.FileKey 60 let fetchOp = { 61 selections: `${fileKeyObj.MEDIA_TYPE}=?`, 62 selectionArgs: [`${mediaLibrary.MediaType.AUDIO}`], 63 } 64 const fetchFileResult = await this.mediaTest.getFileAssets(fetchOp) 65 let result: Array<Record> = [] 66 Logger.info(TAG, `queryAllAudios fetchFileResult=${fetchFileResult.getCount()}`) 67 if (fetchFileResult.getCount() > 0) { 68 let fileAssets = await fetchFileResult.getAllObject() 69 for (let i = 0; i < fileAssets.length; i++) { 70 let record = new Record(fileAssets[i], false) 71 result.push(record) 72 } 73 } 74 return result 75 } 76 77 async queryFile(id: number) { 78 let fileKeyObj = mediaLibrary.FileKey 79 if (id !== undefined) { 80 let args = id.toString() 81 let fetchOp = { 82 selections: `${fileKeyObj.ID}=?`, 83 selectionArgs: [args], 84 } 85 const fetchFileResult = await this.mediaTest.getFileAssets(fetchOp) 86 Logger.info(TAG, `fetchFileResult.getCount() = ${fetchFileResult.getCount()}`) 87 const fileAsset = await fetchFileResult.getAllObject() 88 return new Record(fileAsset[0], false) 89 } else { 90 return undefined 91 } 92 } 93 94 deleteFile(uri) { 95 Logger.info(TAG, `deleteFile,uri = ${uri}`) 96 return this.mediaTest.deleteAsset(uri) 97 } 98 99 onAudioChange(callback: () => void) { 100 this.mediaTest.on('audioChange', () => { 101 callback() 102 }) 103 } 104 105 saveFileDuration(name: string, value) { 106 this.storage.putSync(name, value) 107 this.storage.flush() 108 } 109 110 getFileDuration(name: string) { 111 return this.storage.getSync(name, '00:00') 112 } 113} 114 115export default new MediaManager()