• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Obtaining Audio/Video Metadata (ArkTS)
2
3You can use **AVMetadataExtractor** to obtain metadata from a raw media asset. This topc walks you through on how to obtain the metadata of an audio asset. The process of obtaining the metadata of a video asset is similar. The only difference is that the process of obtaining the album cover is not required for a video asset, because no album cover is available in the video asset.
4
5The full process of obtaining the metadata of an audio asset includes creating an **AVMetadataExtractor** instance, setting resources, obtaining the metadata, obtaining the album cover, and releasing the instance.
6
7## How to Develop
8
9Read [AVMetadataExtractor](../reference/apis-media-kit/js-apis-media.md#avmetadataextractor11) for the API reference.
10
111. Call **createAVMetadataExtractor()** to create an **AVMetadataExtractor** instance.
12
132. Set resources. Specifically, set the **fdSrc** attribute (indicating the file descriptor) or **dataSrc** attribute (indicating the data source descriptor).
14   > **NOTE**
15   >
16   > You need to check the resource validity and set either attribute based on the actual situation.
17   >
18   > - To set **fdSrc**, use **ResourceManager.getRawFd** to obtain the file descriptor of the resource file packed in the HAP. For details, see [ResourceManager API Reference](../reference/apis-localization-kit/js-apis-resource-manager.md#getrawfd9).
19   >
20   > - To set **dataSrc**, set **callback** in **dataSrc** to ensure that the corresponding resource can be correctly read when the callback is invoked, and use the application sandbox directory to access the resource. For details, see [Obtaining Application File Paths](../application-models/application-context-stage.md#obtaining-application-file-paths). For details about the application sandbox and how to push files to the application sandbox directory, see [File Management](../file-management/app-sandbox-directory.md).
21
223. Obtain the metadata. Specifically, call **fetchMetadata()** to obtain an **AVMetadata** object, the attributes of which are the metadata of the media asset.
23
244. (Optional) Call **fetchAlbumCover()** to obtain the album cover.
25
265. Call **release()** to release the **AVMetadataExtractor** instance.
27
28## Sample Code
29
30Refer to the sample code below to set the file descriptor and obtain the metadata and album cover of an audio asset.
31
32```ts
33import media from '@ohos.multimedia.media'
34import image from '@ohos.multimedia.image'
35import type common from '@ohos.app.ability.common';
36import fs from '@ohos.file.fs';
37
38const TAG = 'MetadataDemo'
39@Entry
40@Component
41struct Index {
42  @State message: string = 'Hello World'
43
44  // Declare a pixelMap object, which is used for image display.
45  @State pixelMap: image.PixelMap | undefined = undefined;
46
47  build() {
48    Row() {
49      Column() {
50        Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)
51        Button() {
52          Text('TestButton')
53            .fontSize(30)
54            .fontWeight(FontWeight.Bold)
55        }
56        .type(ButtonType.Capsule)
57        .margin({
58          top: 20
59        })
60        .backgroundColor('#0D9FFB')
61        .width('60%')
62        .height('5%')
63        .onClick(() => {
64          // Set fdSrc and obtain the audio metadata and album cover (callback mode).
65          this.testFetchMetadataFromFdSrcByCallback()
66          // Set fdSrc and obtain the audio metadata and album cover (promise mode).
67          this.testFetchMetadataFromFdSrcByPromise()
68          // Set dataSrc and obtain the audio metadata and album cover.
69          this.testFetchMetadataFromDataSrc()
70        })
71        Image(this.pixelMap).width(300).height(300)
72          .margin({
73            top: 20
74          })
75      }
76      .width('100%')
77    }
78    .height('100%')
79  }
80
81  // The demo below uses the resource manager API to obtain the audio file packed in the HAP. By setting fdSrc, it obtains and displays the audio metadata,
82  // obtains the audio album cover, and displays it on the screen through the Image component. This demo calls APIs in callback mode.
83  async testFetchMetadataFromFdSrcByCallback() {
84    // Create an AVMetadataExtractor instance.
85    let avMetadataExtractor: media.AVMetadataExtractor = await media.createAVMetadataExtractor()
86
87    // Set the fdSrc attribute.
88    avMetadataExtractor.fdSrc = await getContext(this).resourceManager.getRawFd('cover.mp3');
89
90    // Obtain the metadata (callback mode).
91    avMetadataExtractor.fetchMetadata((error, metadata) => {
92      if (error) {
93        console.error(TAG, `fetchMetadata callback failed, err = ${JSON.stringify(error)}`)
94        return
95      }
96      console.info(TAG, `fetchMetadata callback success, genre: ${metadata.genre}`)
97    })
98
99    // Obtain the album cover (callback mode).
100    avMetadataExtractor.fetchAlbumCover((err, pixelMap) => {
101      if (err) {
102        console.error(TAG, `fetchAlbumCover callback failed, err = ${JSON.stringify(err)}`)
103        return
104      }
105      this.pixelMap = pixelMap
106
107      // Release the instance (callback mode).
108      avMetadataExtractor.release((error) => {
109        if (error) {
110          console.error(TAG, `release failed, err = ${JSON.stringify(error)}`)
111          return
112        }
113        console.info(TAG, `release success.`)
114      })
115    })
116  }
117
118  // The demo below uses the resource manager API to obtain the audio file packed in the HAP. By setting fdSrc, it obtains and displays the audio metadata,
119  // obtains the audio album cover, and displays it on the screen through the Image component. This demo calls APIs in promise mode.
120  async testFetchMetadataFromFdSrcByPromise() {
121    // Create an AVMetadataExtractor instance.
122    let avMetadataExtractor: media.AVMetadataExtractor = await media.createAVMetadataExtractor()
123    // Set the fdSrc attribute.
124    avMetadataExtractor.fdSrc = await getContext(this).resourceManager.getRawFd('cover.mp3');
125
126    // Obtain the metadata (promise mode).
127    let metadata = await avMetadataExtractor.fetchMetadata()
128    console.info(TAG, `get meta data, hasAudio: ${metadata.hasAudio}`)
129
130    // Obtain the album cover (promise mode).
131    this.pixelMap = await avMetadataExtractor.fetchAlbumCover()
132
133    // Release the instance (promise mode).
134    avMetadataExtractor.release()
135    console.info(TAG, `release success.`)
136  }
137
138  // The demo below uses the fs API to open the sandbox directory and obtain the audio file address. By setting dataSrc, it obtains and displays the audio metadata,
139  // obtains the audio album cover, and displays it on the screen through the Image component.
140  async testFetchMetadataFromDataSrc() {
141    let context = getContext(this) as common.UIAbilityContext
142    // Obtain the sandbox address filesDir through UIAbilityContext. The stage model is used as an example.
143    let filePath: string = context.filesDir + '/cover.mp3';
144    let fd: number = fs.openSync(filePath, 0o0).fd;
145    let fileSize: number = fs.statSync(filePath).size;
146    // Set the dataSrc descriptor, obtain resources from the file through a callback, and write the resources to the buffer.
147    let dataSrc: media.AVDataSrcDescriptor = {
148      fileSize: fileSize,
149      callback: (buffer, len, pos) => {
150        if (buffer == undefined || len == undefined || pos == undefined) {
151          console.error(TAG, `dataSrc callback param invalid`)
152          return -1
153        }
154
155        class Option {
156          offset: number | undefined = 0;
157          length: number | undefined = len;
158          position: number | undefined = pos;
159        }
160        let options = new Option();
161        let num = fs.readSync(fd, buffer, options)
162        console.info(TAG, 'readAt end, num: ' + num)
163        if (num > 0 && fileSize >= pos) {
164          return num;
165        }
166        return -1;
167      }
168    }
169
170    // Create an AVMetadataExtractor instance.
171    let avMetadataExtractor = await media.createAVMetadataExtractor()
172    // Set the dataSrc attribute.
173    avMetadataExtractor.dataSrc = dataSrc;
174
175    // Obtain the metadata (promise mode).
176    let metadata = await avMetadataExtractor.fetchMetadata()
177    console.info(TAG, `get meta data, mimeType: ${metadata.mimeType}`)
178
179    // Obtain the album cover (promise mode).
180    this.pixelMap = await avMetadataExtractor.fetchAlbumCover()
181
182    // Release the instance (promise mode).
183    avMetadataExtractor.release()
184    console.info(TAG, `release data source success.`)
185  }
186}
187```
188