1bea4f105Sopenharmony_ci/* 2bea4f105Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved. 3bea4f105Sopenharmony_ci */ 4bea4f105Sopenharmony_ciimport dataSharePredicates from '@ohos.data.dataSharePredicates'; 5bea4f105Sopenharmony_ciimport userFileManager from '@ohos.filemanagement.userFileManager'; 6bea4f105Sopenharmony_ciimport image from '@ohos.multimedia.image'; 7bea4f105Sopenharmony_ci 8bea4f105Sopenharmony_ciimport { Logger } from '../../common/util/HiLogger' 9bea4f105Sopenharmony_ciimport { LocalAudioFile } from './LocalAudioFile' 10bea4f105Sopenharmony_ci 11bea4f105Sopenharmony_ciconst logger: Logger = new Logger('LocalAudioManager') 12bea4f105Sopenharmony_ci 13bea4f105Sopenharmony_ci/** 14bea4f105Sopenharmony_ci * 对接媒体库/文件系统,提供本地音频查询能力,本地音频分页查询能力 15bea4f105Sopenharmony_ci */ 16bea4f105Sopenharmony_ciexport class LocalAudioManager { 17bea4f105Sopenharmony_ci private fileAssets: Array<userFileManager.FileAsset> = [] 18bea4f105Sopenharmony_ci 19bea4f105Sopenharmony_ci /** 20bea4f105Sopenharmony_ci * 获取FetchResult<FileAsset> 21bea4f105Sopenharmony_ci */ 22bea4f105Sopenharmony_ci getLocalAudio(offset: number, limit: number, context: Context): Promise<Array<LocalAudioFile>> { 23bea4f105Sopenharmony_ci logger.info('getAudioAssets'); 24bea4f105Sopenharmony_ci let mgr = userFileManager.getUserFileMgr(context); 25bea4f105Sopenharmony_ci let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 26bea4f105Sopenharmony_ci let fetchOptions: userFileManager.FetchOptions = { 27bea4f105Sopenharmony_ci fetchColumns: [ 28bea4f105Sopenharmony_ci userFileManager.AudioKey.TITLE.toString(), 29bea4f105Sopenharmony_ci userFileManager.AudioKey.ARTIST.toString(), 30bea4f105Sopenharmony_ci userFileManager.AudioKey.AUDIOALBUM.toString() 31bea4f105Sopenharmony_ci ], 32bea4f105Sopenharmony_ci predicates: predicates.limit(limit, offset) 33bea4f105Sopenharmony_ci }; 34bea4f105Sopenharmony_ci 35bea4f105Sopenharmony_ci return new Promise(async (resolve) => { 36bea4f105Sopenharmony_ci try { 37bea4f105Sopenharmony_ci let fetchResult: userFileManager.FetchResult<userFileManager.FileAsset> = await mgr 38bea4f105Sopenharmony_ci .getAudioAssets(fetchOptions); 39bea4f105Sopenharmony_ci if (fetchResult) { 40bea4f105Sopenharmony_ci logger.info('fetchFileResult success' + JSON.stringify(fetchResult)); 41bea4f105Sopenharmony_ci this.fileAssets = await fetchResult.getAllObject(); 42bea4f105Sopenharmony_ci if (this.fileAssets && this.fileAssets.length > 0) { 43bea4f105Sopenharmony_ci let localAudioFile = await this.transferFileAssetsToLocalAudioFiles() 44bea4f105Sopenharmony_ci logger.info('localAudioFile length: ' + (await localAudioFile).length) 45bea4f105Sopenharmony_ci resolve(localAudioFile) 46bea4f105Sopenharmony_ci } else { 47bea4f105Sopenharmony_ci logger.error('fileAssets is null') 48bea4f105Sopenharmony_ci resolve([]) 49bea4f105Sopenharmony_ci } 50bea4f105Sopenharmony_ci } 51bea4f105Sopenharmony_ci } catch (err) { 52bea4f105Sopenharmony_ci logger.error('getAudioAssets failed, message = ' + err); 53bea4f105Sopenharmony_ci resolve([]) 54bea4f105Sopenharmony_ci } 55bea4f105Sopenharmony_ci }) 56bea4f105Sopenharmony_ci } 57bea4f105Sopenharmony_ci 58bea4f105Sopenharmony_ci /** 59bea4f105Sopenharmony_ci * 将FileAsset转换为LocalAudioFile 60bea4f105Sopenharmony_ci */ 61bea4f105Sopenharmony_ci transferFileAssetsToLocalAudioFiles(): Promise<Array<LocalAudioFile>> { 62bea4f105Sopenharmony_ci let localAudioFile: LocalAudioFile[] = [] 63bea4f105Sopenharmony_ci return new Promise(async (resolve) => { 64bea4f105Sopenharmony_ci if (this.fileAssets) { 65bea4f105Sopenharmony_ci logger.info('fileAsset.length :' + this.fileAssets.length); 66bea4f105Sopenharmony_ci for (let fileAsset of this.fileAssets) { 67bea4f105Sopenharmony_ci let uri = fileAsset.uri || '' 68bea4f105Sopenharmony_ci let name = fileAsset.get(userFileManager.AudioKey.TITLE.toString()).toString() || fileAsset.displayName 69bea4f105Sopenharmony_ci let artist = fileAsset.get(userFileManager.AudioKey.ARTIST.toString()).toString() || '' 70bea4f105Sopenharmony_ci let album = fileAsset.get(userFileManager.AudioKey.AUDIOALBUM.toString()).toString() || '' 71bea4f105Sopenharmony_ci let displayName = fileAsset.displayName 72bea4f105Sopenharmony_ci let getThumbnail: image.PixelMap | Resource 73bea4f105Sopenharmony_ci try { 74bea4f105Sopenharmony_ci getThumbnail = await fileAsset.getThumbnail() 75bea4f105Sopenharmony_ci } catch (err) { 76bea4f105Sopenharmony_ci getThumbnail = $r('app.media.default') 77bea4f105Sopenharmony_ci logger.error('transferGetThumbnail failed: ' + err) 78bea4f105Sopenharmony_ci } 79bea4f105Sopenharmony_ci if (uri) { 80bea4f105Sopenharmony_ci let localAudioFileItem: LocalAudioFile = new LocalAudioFile(uri, name, artist, album, 81bea4f105Sopenharmony_ci displayName, getThumbnail) 82bea4f105Sopenharmony_ci localAudioFile.push(localAudioFileItem) 83bea4f105Sopenharmony_ci logger.info(`localAudioFileItem: uri: ${uri}, name: ${name}, artist: ${artist}, album: ${album}, getThumbnail: ${JSON.stringify(getThumbnail)}}`) 84bea4f105Sopenharmony_ci } else { 85bea4f105Sopenharmony_ci logger.error('transferFileAssetsToLocalAudioFiles failed!') 86bea4f105Sopenharmony_ci } 87bea4f105Sopenharmony_ci } 88bea4f105Sopenharmony_ci } 89bea4f105Sopenharmony_ci resolve(localAudioFile) 90bea4f105Sopenharmony_ci }) 91bea4f105Sopenharmony_ci } 92bea4f105Sopenharmony_ci}