1# Audio Recording Development 2 3## When to Use 4 5During audio recording, audio signals are captured, encoded, and saved to files. You can specify parameters such as the sampling rate, number of audio channels, encoding format, encapsulation format, and file path for audio recording. 6 7**Figure 1** Audio recording state transition 8 9 10 11 12 13**Figure 2** Layer 0 diagram of audio recording 14 15 16 17## How to Develop 18 19For details about the APIs, see [AudioRecorder in the Media API](../reference/apis/js-apis-media.md). 20 21### Full-Process Scenario 22 23The full audio recording process includes creating an instance, setting recording parameters, starting, pausing, resuming, and stopping recording, and releasing resources. 24 25```js 26import media from '@ohos.multimedia.media' 27import mediaLibrary from '@ohos.multimedia.mediaLibrary' 28export class AudioRecorderDemo { 29 private testFdNumber; // Used to save the FD address. 30 31 // Set the callbacks related to audio recording. 32 setCallBack(audioRecorder) { 33 audioRecorder.on('prepare', () => { // Set the 'prepare' event callback. 34 console.log('prepare success'); 35 audioRecorder.start(); // Call the start API to start recording and trigger the 'start' event callback. 36 }); 37 audioRecorder.on('start', () => { // Set the 'start' event callback. 38 console.log('audio recorder start success'); 39 audioRecorder.pause(); // Call the pause API to pause recording and trigger the 'pause' event callback. 40 }); 41 audioRecorder.on('pause', () => { // Set the 'pause' event callback. 42 console.log('audio recorder pause success'); 43 audioRecorder.resume(); // Call the resume API to resume recording and trigger the 'resume' event callback. 44 }); 45 audioRecorder.on('resume', () => { // Set the 'resume' event callback. 46 console.log('audio recorder resume success'); 47 audioRecorder.stop(); // Call the stop API to stop recording and trigger the 'stop' event callback. 48 }); 49 audioRecorder.on('stop', () => { // Set the 'stop' event callback. 50 console.log('audio recorder stop success'); 51 audioRecorder.reset(); // Call the reset API to reset the recorder and trigger the 'reset' event callback. 52 }); 53 audioRecorder.on('reset', () => { // Set the 'reset' event callback. 54 console.log('audio recorder reset success'); 55 audioRecorder.release(); // Call the release API to release resources and trigger the 'release' event callback. 56 }); 57 audioRecorder.on('release', () => { // Set the 'release' event callback. 58 console.log('audio recorder release success'); 59 audioRecorder = undefined; 60 }); 61 audioRecorder.on('error', (error) => { // Set the 'error' event callback. 62 console.info(`audio error called, errName is ${error.name}`); 63 console.info(`audio error called, errCode is ${error.code}`); 64 console.info(`audio error called, errMessage is ${error.message}`); 65 }); 66 } 67 68 // pathName indicates the passed recording file name, for example, 01.mp3. The generated file address is /storage/media/100/local/files/Video/01.mp3. 69 // To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA. 70 async getFd(pathName) { 71 let displayName = pathName; 72 const mediaTest = mediaLibrary.getMediaLibrary(); 73 let fileKeyObj = mediaLibrary.FileKey; 74 let mediaType = mediaLibrary.MediaType.VIDEO; 75 let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO); 76 let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath); 77 if (dataUri != undefined) { 78 let args = dataUri.id.toString(); 79 let fetchOp = { 80 selections : fileKeyObj.ID + "=?", 81 selectionArgs : [args], 82 } 83 let fetchFileResult = await mediaTest.getFileAssets(fetchOp); 84 let fileAsset = await fetchFileResult.getAllObject(); 85 let fdNumber = await fileAsset[0].open('Rw'); 86 this.testFdNumber = "fd://" + fdNumber.toString(); 87 } 88 } 89 90 async audioRecorderDemo() { 91 // 1. Create an AudioRecorder instance. 92 let audioRecorder = media.createAudioRecorder(); 93 // 2. Set the callbacks. 94 this.setCallBack(audioRecorder); 95 await this.getFd('01.mp3'); // Call the getFd method to obtain the FD address of the file to be recorded. 96 // 3. Set the recording parameters. 97 let audioRecorderConfig = { 98 audioEncodeBitRate : 22050, 99 audioSampleRate : 22050, 100 numberOfChannels : 2, 101 uri : this.testFdNumber, // testFdNumber is generated by getFd. 102 location : { latitude : 30, longitude : 130}, 103 audioEncoderMime : media.CodecMimeType.AUDIO_AAC, 104 fileFormat : media.ContainerFormatType.CFT_MPEG_4A, 105 } 106 audioRecorder.prepare(audioRecorderConfig); // Call the prepare method to trigger the 'prepare' event callback. 107 } 108} 109``` 110 111### Normal Recording Scenario 112 113Unlike the full-process scenario, the normal recording scenario does not include the process of pausing and resuming recording. 114 115```js 116import media from '@ohos.multimedia.media' 117import mediaLibrary from '@ohos.multimedia.mediaLibrary' 118export class AudioRecorderDemo { 119 private testFdNumber; // Used to save the FD address. 120 121 // Set the callbacks related to audio recording. 122 setCallBack(audioRecorder) { 123 audioRecorder.on('prepare', () => { // Set the 'prepare' event callback. 124 console.log('prepare success'); 125 audioRecorder.start(); // Call the start API to start recording and trigger the 'start' event callback. 126 }); 127 audioRecorder.on('start', () => { // Set the 'start' event callback. 128 console.log('audio recorder start success'); 129 audioRecorder.stop(); // Call the stop API to stop recording and trigger the 'stop' event callback. 130 }); 131 audioRecorder.on('stop', () => { // Set the 'stop' event callback. 132 console.log('audio recorder stop success'); 133 audioRecorder.release(); // Call the release API to release resources and trigger the 'release' event callback. 134 }); 135 audioRecorder.on('release', () => { // Set the 'release' event callback. 136 console.log('audio recorder release success'); 137 audioRecorder = undefined; 138 }); 139 audioRecorder.on('error', (error) => { // Set the 'error' event callback. 140 console.info(`audio error called, errName is ${error.name}`); 141 console.info(`audio error called, errCode is ${error.code}`); 142 console.info(`audio error called, errMessage is ${error.message}`); 143 }); 144 } 145 146 // pathName indicates the passed recording file name, for example, 01.mp3. The generated file address is /storage/media/100/local/files/Video/01.mp3. 147 // To use the media library, declare the following permissions: ohos.permission.MEDIA_LOCATION, ohos.permission.WRITE_MEDIA, and ohos.permission.READ_MEDIA. 148 async getFd(pathName) { 149 let displayName = pathName; 150 const mediaTest = mediaLibrary.getMediaLibrary(); 151 let fileKeyObj = mediaLibrary.FileKey; 152 let mediaType = mediaLibrary.MediaType.VIDEO; 153 let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO); 154 let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath); 155 if (dataUri != undefined) { 156 let args = dataUri.id.toString(); 157 let fetchOp = { 158 selections : fileKeyObj.ID + "=?", 159 selectionArgs : [args], 160 } 161 let fetchFileResult = await mediaTest.getFileAssets(fetchOp); 162 let fileAsset = await fetchFileResult.getAllObject(); 163 let fdNumber = await fileAsset[0].open('Rw'); 164 this.testFdNumber = "fd://" + fdNumber.toString(); 165 } 166 } 167 168 async audioRecorderDemo() { 169 // 1. Create an AudioRecorder instance. 170 let audioRecorder = media.createAudioRecorder(); 171 // 2. Set the callbacks. 172 this.setCallBack(audioRecorder); 173 await this.getFd('01.mp3'); // Call the getFd method to obtain the FD address of the file to be recorded. 174 // 3. Set the recording parameters. 175 let audioRecorderConfig = { 176 audioEncodeBitRate : 22050, 177 audioSampleRate : 22050, 178 numberOfChannels : 2, 179 uri : this.testFdNumber, // testFdNumber is generated by getFd. 180 location : { latitude : 30, longitude : 130}, 181 audioEncoderMime : media.CodecMimeType.AUDIO_AAC, 182 fileFormat : media.ContainerFormatType.CFT_MPEG_4A, 183 } 184 audioRecorder.prepare(audioRecorderConfig); // Call the prepare method to trigger the 'prepare' event callback. 185 } 186} 187``` 188