diff --git a/zh-cn/application-dev/media/audio-recorder.md b/zh-cn/application-dev/media/audio-recorder.md index 4e6a88455f0d51b25fa552f388e46c1e3a8aa355..6ce349669def897a154a0b582662d066f543608e 100644 --- a/zh-cn/application-dev/media/audio-recorder.md +++ b/zh-cn/application-dev/media/audio-recorder.md @@ -18,10 +18,6 @@ 详细API含义可参考:[媒体服务API文档AudioRecorder](../reference/apis/js-apis-media.md) -### 开发者示例 - -端到端开发者示例请参考:https://gitee.com/openharmony/app_samples/blob/master/media/Recorder/entry/src/main/ets/MainAbility/pages/RecordPage.ets - ### 全流程场景 包含流程:创建实例,设置录制参数,录制音频,暂停录制,恢复录制,停止录制,释放资源等流程。 @@ -29,46 +25,49 @@ ```js import media from '@ohos.multimedia.media' import mediaLibrary from '@ohos.multimedia.mediaLibrary' - -let testFdNumber; - -function SetCallBack(audioRecorder) { - audioRecorder.on('prepare', () => { // 设置'prepare'事件回调 - console.log('prepare success'); - // 录制界面可切换至已准备好,可点击录制按钮进行录制 +export class AudioRecorderDemo { + private testFdNumber; // 用于保存fd地址 + + // 设置音频录制相关回调函数 + setCallBack(audioRecorder) { + audioRecorder.on('prepare', () => { // 设置'prepare'事件回调 + console.log('prepare success'); + audioRecorder.start(); // 调用start方法开始录制,并触发start回调 }); - audioRecorder.on('start', () => { // 设置'start'事件回调 - console.log('audio recorder start success'); - // 将录制按钮切换至可暂停状态 + audioRecorder.on('start', () => { // 设置'start'事件回调 + console.log('audio recorder start success'); + audioRecorder.pause(); // 调用pause方法暂停录制,并触发pause回调 }); - audioRecorder.on('pause', () => { // 设置'pause'事件回调 - console.log('audio recorder pause success'); - // 将录制按钮切换至可录制状态 + audioRecorder.on('pause', () => { // 设置'pause'事件回调 + console.log('audio recorder pause success'); + audioRecorder.resume(); // 调用resume方法恢复录制,并触发resume回调 }); - audioRecorder.on('resume', () => { // 设置'resume'事件回调 - console.log('audio recorder resume success'); - // 将录制按钮切换至可暂停状态 + audioRecorder.on('resume', () => { // 设置'resume'事件回调 + console.log('audio recorder resume success'); + audioRecorder.stop(); // 调用stop方法停止录制,并触发stop回调 }); - audioRecorder.on('stop', () => { // 设置'stop'事件回调 - console.log('audio recorder stop success'); + audioRecorder.on('stop', () => { // 设置'stop'事件回调 + console.log('audio recorder stop success'); + audioRecorder.reset(); // 调用reset方法重置录制,并触发reset回调 }); - audioRecorder.on('release', () => { // 设置'release'事件回调 - console.log('audio recorder release success'); + audioRecorder.on('reset', () => { // 设置'reset'事件回调 + console.log('audio recorder reset success'); + audioRecorder.release(); // 调用release方法,释放资源,并触发release回调 }); - audioRecorder.on('reset', () => { // 设置'reset'事件回调 - console.log('audio recorder reset success'); - // 需要重新设置录制参数才能再次录制 + audioRecorder.on('release', () => { // 设置'release'事件回调 + console.log('audio recorder release success'); + audioRecorder = undefined; }); - audioRecorder.on('error', (error) => { // 设置'error'事件回调 - console.info(`audio error called, errName is ${error.name}`); - console.info(`audio error called, errCode is ${error.code}`); - console.info(`audio error called, errMessage is ${error.message}`); + audioRecorder.on('error', (error) => { // 设置'error'事件回调 + console.info(`audio error called, errName is ${error.name}`); + console.info(`audio error called, errCode is ${error.code}`); + console.info(`audio error called, errMessage is ${error.message}`); }); -} + } -// pathName是传入的录制文件名,例如:01.mp3,生成后的文件地址:/storage/media/100/local/files/Movies/01.mp3 -// 使用mediaLibrary需要添加以下权限, ohos.permission.MEDIA_LOCATION、ohos.permission.WRITE_MEDIA、ohos.permission.READ_MEDIA -async function getFd(pathName) { + // pathName是传入的录制文件名,例如:01.mp3,生成后的文件地址:/storage/media/100/local/files/Video/01.mp3 + // 使用mediaLibrary需要添加以下权限, ohos.permission.MEDIA_LOCATION、ohos.permission.WRITE_MEDIA、ohos.permission.READ_MEDIA + async getFd(pathName) { let displayName = pathName; const mediaTest = mediaLibrary.getMediaLibrary(); let fileKeyObj = mediaLibrary.FileKey; @@ -76,49 +75,37 @@ async function getFd(pathName) { let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO); let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath); if (dataUri != undefined) { - let args = dataUri.id.toString(); - let fetchOp = { - selections : fileKeyObj.ID + "=?", - selectionArgs : [args], - } - let fetchFileResult = await mediaTest.getFileAssets(fetchOp); - let fileAsset = await fetchFileResult.getAllObject(); - let fdNumber = await fileAsset[0].open('Rw'); - fdNumber = "fd://" + fdNumber.toString(); - testFdNumber = fdNumber; + let args = dataUri.id.toString(); + let fetchOp = { + selections : fileKeyObj.ID + "=?", + selectionArgs : [args], + } + let fetchFileResult = await mediaTest.getFileAssets(fetchOp); + let fileAsset = await fetchFileResult.getAllObject(); + let fdNumber = await fileAsset[0].open('Rw'); + this.testFdNumber = "fd://" + fdNumber.toString(); } + } + + async audioRecorderDemo() { + // 1.创建实例 + let audioRecorder = media.createAudioRecorder(); + // 2.设置回调 + this.setCallBack(audioRecorder); + await this.getFd('01.mp3'); // 调用getFd方法获取需要录制文件的fd地址 + // 3.设置录制参数 + let audioRecorderConfig = { + audioEncodeBitRate : 22050, + audioSampleRate : 22050, + numberOfChannels : 2, + uri : this.testFdNumber, // testFdNumber由getFd生成 + location : { latitude : 30, longitude : 130}, + audioEncoderMime : media.CodecMimeType.AUDIO_AAC, + fileFormat : media.ContainerFormatType.CFT_MPEG_4A, + } + audioRecorder.prepare(audioRecorderConfig); // 调用prepare方法,触发prepare回调函数 + } } - -await getFd('01.mp3'); - -// 1.创建实例 -let audioRecorder = media.createAudioRecorder(); -// 2.设置回调 -SetCallBack(audioRecorder); -// 3.设置录制参数 -let audioRecorderConfig = { - audioEncoder : media.AudioEncoder.AAC_LC , - audioEncodeBitRate : 22050, - audioSampleRate : 22050, - numberOfChannels : 2, - format : media.AudioOutputFormat.AAC_ADTS, - uri : testFdNumber, // testFdNumber由getFd生成 - location : { latitude : 30, longitude : 130}, -} -audioRecorder.prepare(audioRecorderConfig); -// 4.开始录制 -audioRecorder.start(); // 需等待'prepare'事件回调完成后,才可调用start进行录制,触发'start'事件回调 -// 5.暂停录制 -audioRecorder.pause(); // 需等待'start'事件回调完成后,才可调用pause进行暂停,触发'pause'事件回调 -// 6.恢复录制 -audioRecorder.resume(); // 需等待'pause'事件回调完成后,才可调用resume进行录制,触发'resume'事件回调 -// 7.停止录制 -audioRecorder.stop(); // 需等待'start'或'resume'事件回调完成后,才可调用stop进行暂停,触发'stop'事件回调 -// 8.重置录制 -audioRecorder.reset(); // 触发'reset'事件回调后,重新进行prepare,才可重新录制 -// 9.释放资源 -audioRecorder.release(); // audioRecorder资源被销毁 -audioRecorder = undefined; ``` ### 正常录制场景 @@ -128,29 +115,37 @@ audioRecorder = undefined; ```js import media from '@ohos.multimedia.media' import mediaLibrary from '@ohos.multimedia.mediaLibrary' - -let testFdNumber; - -function SetCallBack(audioRecorder) { - audioRecorder.on('prepare', () => { // 设置'prepare'事件回调 - console.log('prepare success'); - // 录制界面可切换至已准备好,可点击录制按钮进行录制 +export class AudioRecorderDemo { + private testFdNumber; // 用于保存fd地址 + + // 设置音频录制相关回调函数 + setCallBack(audioRecorder) { + audioRecorder.on('prepare', () => { // 设置'prepare'事件回调 + console.log('prepare success'); + audioRecorder.start(); // 调用start方法开始录制,并触发start回调 }); - audioRecorder.on('start', () => { // 设置'start'事件回调 - console.log('audio recorder start success'); - // 将录制按钮切换至可暂停状态 - }); - audioRecorder.on('stop', () => { // 设置'stop'事件回调 - console.log('audio recorder stop success'); - }); - audioRecorder.on('release', () => { // 设置'release'事件回调 - console.log('audio recorder release success'); - }); -} + audioRecorder.on('start', () => { // 设置'start'事件回调 + console.log('audio recorder start success'); + audioRecorder.stop(); // 调用stop方法停止录制,并触发stop回调 + }); + audioRecorder.on('stop', () => { // 设置'stop'事件回调 + console.log('audio recorder stop success'); + audioRecorder.release(); // 调用release方法,释放资源,并触发release回调 + }); + audioRecorder.on('release', () => { // 设置'release'事件回调 + console.log('audio recorder release success'); + audioRecorder = undefined; + }); + audioRecorder.on('error', (error) => { // 设置'error'事件回调 + console.info(`audio error called, errName is ${error.name}`); + console.info(`audio error called, errCode is ${error.code}`); + console.info(`audio error called, errMessage is ${error.message}`); + }); + } -// pathName是传入的录制文件名,例如:01.mp3,生成后的文件地址:/storage/media/100/local/files/Movies/01.mp3 -// 使用mediaLibrary需要添加以下权限, ohos.permission.MEDIA_LOCATION、ohos.permission.WRITE_MEDIA、ohos.permission.READ_MEDIA -async function getFd(pathName) { + // pathName是传入的录制文件名,例如:01.mp3,生成后的文件地址:/storage/media/100/local/files/Video/01.mp3 + // 使用mediaLibrary需要添加以下权限, ohos.permission.MEDIA_LOCATION、ohos.permission.WRITE_MEDIA、ohos.permission.READ_MEDIA + async getFd(pathName) { let displayName = pathName; const mediaTest = mediaLibrary.getMediaLibrary(); let fileKeyObj = mediaLibrary.FileKey; @@ -158,43 +153,37 @@ async function getFd(pathName) { let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO); let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath); if (dataUri != undefined) { - let args = dataUri.id.toString(); - let fetchOp = { - selections : fileKeyObj.ID + "=?", - selectionArgs : [args], - } - let fetchFileResult = await mediaTest.getFileAssets(fetchOp); - let fileAsset = await fetchFileResult.getAllObject(); - let fdNumber = await fileAsset[0].open('Rw'); - fdNumber = "fd://" + fdNumber.toString(); - testFdNumber = fdNumber; + let args = dataUri.id.toString(); + let fetchOp = { + selections : fileKeyObj.ID + "=?", + selectionArgs : [args], + } + let fetchFileResult = await mediaTest.getFileAssets(fetchOp); + let fileAsset = await fetchFileResult.getAllObject(); + let fdNumber = await fileAsset[0].open('Rw'); + this.testFdNumber = "fd://" + fdNumber.toString(); } + } + + async audioRecorderDemo() { + // 1.创建实例 + let audioRecorder = media.createAudioRecorder(); + // 2.设置回调 + this.setCallBack(audioRecorder); + await this.getFd('01.mp3'); // 调用getFd方法获取需要录制文件的fd地址 + // 3.设置录制参数 + let audioRecorderConfig = { + audioEncodeBitRate : 22050, + audioSampleRate : 22050, + numberOfChannels : 2, + uri : this.testFdNumber, // testFdNumber由getFd生成 + location : { latitude : 30, longitude : 130}, + audioEncoderMime : media.CodecMimeType.AUDIO_AAC, + fileFormat : media.ContainerFormatType.CFT_MPEG_4A, + } + audioRecorder.prepare(audioRecorderConfig); // 调用prepare方法,触发prepare回调函数 + } } - -await getFd('01.mp3'); - -// 1.创建实例 -let audioRecorder = media.createAudioRecorder(); -// 2.设置回调 -SetCallBack(audioRecorder); -// 3.设置录制参数 -let audioRecorderConfig = { - audioEncoder : media.AudioEncoder.AAC_LC , - audioEncodeBitRate : 22050, - audioSampleRate : 22050, - numberOfChannels : 2, - format : media.AudioOutputFormat.AAC_ADTS, - uri : testFdNumber, // testFdNumber由getFd生成 - location : { latitude : 30, longitude : 130}, -} -audioRecorder.prepare(audioRecorderConfig) -// 4.开始录制 -audioRecorder.start(); // 需等待'prepare'事件回调完成后,才可调用start进行录制,触发'start'事件回调 -// 5.停止录制 -audioRecorder.stop(); // 需等待'start'或'resume'事件回调完成后,才可调用stop进行暂停,触发'stop'事件回调 -// 6.释放资源 -audioRecorder.release(); // audioRecorder资源被销毁 -audioRecorder = undefined; ``` ## 相关实例 diff --git a/zh-cn/application-dev/media/video-playback.md b/zh-cn/application-dev/media/video-playback.md index d78ae06985f9b3dbf36e6359660b89f56f76f0a3..198b33914a07297f1278285ea5da1b9c5d53b26c 100644 --- a/zh-cn/application-dev/media/video-playback.md +++ b/zh-cn/application-dev/media/video-playback.md @@ -43,141 +43,135 @@ VideoPlayer支持的url媒体源输入类型可参考:[url属性说明](../reference/apis/js-apis-media.md#videoplayer_属性) -Xcomponent创建方法可参考:[Xcomponent创建方法](#xcomponent创建方法) +Xcomponent创建方法可参考:[Xcomponent创建方法](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/arkui-ts/ts-basic-components-xcomponent.md) *注意:SetSurface需要在设置url和Prepare之间 ```js import media from '@ohos.multimedia.media' import fileIO from '@ohos.fileio' - -let videoPlayer = undefined; // 用于保存createVideoPlayer创建的对象 -let surfaceID = undefined; // 用于保存Xcomponent接口返回的surfaceID - -// 调用Xcomponent的接口用于获取surfaceID,并保存在surfaceID变量中,该接口由XComponent组件默认加载,非主动调用 -LoadXcomponent() { - surfaceID = this.$element('Xcomponent').getXComponentSurfaceId(); - console.info('LoadXcomponent surfaceID is' + surfaceID); -} - -// 函数调用发生错误时用于上报错误信息 -function failureCallback(error) { +export class VideoPlayerDemo { + // 函数调用发生错误时用于上报错误信息 + failureCallback(error) { console.info(`error happened,error Name is ${error.name}`); console.info(`error happened,error Code is ${error.code}`); console.info(`error happened,error Message is ${error.message}`); -} + } -// 当函数调用发生异常时用于上报错误信息 -function catchCallback(error) { + // 当函数调用发生异常时用于上报错误信息 + catchCallback(error) { console.info(`catch error happened,error Name is ${error.name}`); console.info(`catch error happened,error Code is ${error.code}`); console.info(`catch error happened,error Message is ${error.message}`); -} + } -// 用于打印视频轨道信息 -function printfDescription(obj) { + // 用于打印视频轨道信息 + printfDescription(obj) { for (let item in obj) { - let property = obj[item]; - console.info('key is ' + item); - console.info('value is ' + property); + let property = obj[item]; + console.info('key is ' + item); + console.info('value is ' + property); } -} - -// 调用createVideoPlayer接口返回videoPlayer实例对象 -await media.createVideoPlayer().then((video) => { - if (typeof (video) != 'undefined') { + } + + async videoPlayerDemo() { + let videoPlayer = undefined; + let surfaceID = 'test' // surfaceID用于播放画面显示,具体的值需要通过Xcomponent接口获取,相关文档链接见上面Xcomponent创建方法 + let fdPath = 'fd://' + // path路径的码流可通过"hdc file send D:\xxx\H264_AAC.mp4 /data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上 + let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile/H264_AAC.mp4'; + await fileIO.open(path).then((fdNumber) => { + fdPath = fdPath + '' + fdNumber; + console.info('open fd sucess fd is' + fdPath); + }, (err) => { + console.info('open fd failed err is' + err); + }).catch((err) => { + console.info('open fd failed err is' + err); + }); + // 调用createVideoPlayer接口返回videoPlayer实例对象 + await media.createVideoPlayer().then((video) => { + if (typeof (video) != 'undefined') { console.info('createVideoPlayer success!'); videoPlayer = video; - } else { + } else { console.info('createVideoPlayer fail!'); - } -}, failureCallback).catch(catchCallback); - -// 用户选择视频设置fd(本地播放) -let fdPath = 'fd://' -// path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/accounts/account_0/appdata" 命令,将其推送到设备上 -let path = '/data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp4'; -await fileIO.open(path).then(fdNumber) => { - fdPath = fdPath + '' + fdNumber; - console.info('open fd sucess fd is' + fdPath); -}, (err) => { - console.info('open fd failed err is' + err); -}),catch((err) => { - console.info('open fd failed err is' + err); -}); - -videoPlayer.url = fdPath; - -// 设置surfaceID用于显示视频画面 -await videoPlayer.setDisplaySurface(surfaceID).then(() => { - console.info('setDisplaySurface success'); -}, failureCallback).catch(catchCallback); - -// 调用prepare完成播放前准备工作 -await videoPlayer.prepare().then(() => { - console.info('prepare success'); -}, failureCallback).catch(catchCallback); - -// 调用play接口正式开始播放 -await videoPlayer.play().then(() => { - console.info('play success'); -}, failureCallback).catch(catchCallback); - -// 暂停播放 -await videoPlayer.pause().then(() => { - console.info('pause success'); -}, failureCallback).catch(catchCallback); - -// 通过promise回调方式获取视频轨道信息 -let arrayDescription; -await videoPlayer.getTrackDescription().then((arrlist) => { - if (typeof (arrlist) != 'undefined') { + } + }, this.failureCallback).catch(this.catchCallback); + // 设置播放源 + videoPlayer.url = fdPath; + + // 设置surfaceID用于显示视频画面 + await videoPlayer.setDisplaySurface(surfaceID).then(() => { + console.info('setDisplaySurface success'); + }, this.failureCallback).catch(this.catchCallback); + + // 调用prepare完成播放前准备工作 + await videoPlayer.prepare().then(() => { + console.info('prepare success'); + }, this.failureCallback).catch(this.catchCallback); + + // 调用play接口正式开始播放 + await videoPlayer.play().then(() => { + console.info('play success'); + }, this.failureCallback).catch(this.catchCallback); + + // 暂停播放 + await videoPlayer.pause().then(() => { + console.info('pause success'); + }, this.failureCallback).catch(this.catchCallback); + + // 通过promise回调方式获取视频轨道信息 + let arrayDescription; + await videoPlayer.getTrackDescription().then((arrlist) => { + if (typeof (arrlist) != 'undefined') { arrayDescription = arrlist; - } else { + } else { console.log('video getTrackDescription fail'); + } + }, this.failureCallback).catch(this.catchCallback); + + for (let i = 0; i < arrayDescription.length; i++) { + this.printfDescription(arrayDescription[i]); } -}, failureCallback).catch(catchCallback); -for (let i = 0; i < arrayDescription.length; i++) { - printfDescription(arrayDescription[i]); + // 跳转播放时间到50s位置,具体入参意义请参考接口文档 + let seekTime = 50000; + await videoPlayer.seek(seekTime, media.SeekMode.SEEK_NEXT_SYNC).then((seekDoneTime) => { + console.info('seek success'); + }, this.failureCallback).catch(this.catchCallback); + + // 音量设置接口,具体入参意义请参考接口文档 + let volume = 0.5; + await videoPlayer.setVolume(volume).then(() => { + console.info('setVolume success'); + }, this.failureCallback).catch(this.catchCallback); + + // 倍速设置接口,具体入参意义请参考接口文档 + let speed = media.PlaybackSpeed.SPEED_FORWARD_2_00_X; + await videoPlayer.setSpeed(speed).then(() => { + console.info('setSpeed success'); + }, this.failureCallback).catch(this.catchCallback); + + // 结束播放 + await videoPlayer.stop().then(() => { + console.info('stop success'); + }, this.failureCallback).catch(this.catchCallback); + + // 重置播放配置 + await videoPlayer.reset().then(() => { + console.info('reset success'); + }, this.failureCallback).catch(this.catchCallback); + + // 释放播放资源 + await videoPlayer.release().then(() => { + console.info('release success'); + }, this.failureCallback).catch(this.catchCallback); + + // 相关对象置undefined + videoPlayer = undefined; + surfaceID = undefined; + } } - -// 跳转播放时间到50s位置,具体入参意义请参考接口文档 -let seekTime = 50000; -await videoPlayer.seek(seekTime, media.SeekMode._NEXT_SYNC).then((seekDoneTime) => { - console.info('seek success'); -}, failureCallback).catch(catchCallback); - -// 音量设置接口,具体入参意义请参考接口文档 -let volume = 0.5; -await videoPlayer.setVolume(volume).then(() => { - console.info('setVolume success'); -}, failureCallback).catch(catchCallback); - -// 倍速设置接口,具体入参意义请参考接口文档 -let speed = media.PlaybackRateMode.SPEED_FORWARD_2_00_X; -await videoPlayer.setSpeed(speed).then(() => { - console.info('setSpeed success'); -}, failureCallback).catch(catchCallback); - -// 结束播放 -await videoPlayer.stop().then(() => { - console.info('stop success'); -}, failureCallback).catch(catchCallback); - -// 重置播放配置 -await videoPlayer.reset().then(() => { - console.info('reset success'); -}, failureCallback).catch(catchCallback); - -// 释放播放资源 -await videoPlayer.release().then(() => { - console.info('release success'); -}, failureCallback).catch(catchCallback); - -// 相关对象置undefined -videoPlayer = undefined; -surfaceID = undefined; ``` ### 正常播放场景 @@ -185,86 +179,86 @@ surfaceID = undefined; ```js import media from '@ohos.multimedia.media' import fileIO from '@ohos.fileio' - -let videoPlayer = undefined; // 用于保存createVideoPlayer创建的对象 -let surfaceID = undefined; // 用于保存Xcomponent接口返回的surfaceID - -// 调用Xcomponent的接口用于获取surfaceID,并保存在surfaceID变量中,该接口由XComponent组件默认加载,非主动调用 -LoadXcomponent() { - surfaceID = this.$element('Xcomponent').getXComponentSurfaceId(); - console.info('LoadXcomponent surfaceID is' + surfaceID); -} - -// 函数调用发生错误时用于上报错误信息 -function failureCallback(error) { +export class VideoPlayerDemo { + // 函数调用发生错误时用于上报错误信息 + failureCallback(error) { console.info(`error happened,error Name is ${error.name}`); console.info(`error happened,error Code is ${error.code}`); console.info(`error happened,error Message is ${error.message}`); -} + } -// 当函数调用发生异常时用于上报错误信息 -function catchCallback(error) { + // 当函数调用发生异常时用于上报错误信息 + catchCallback(error) { console.info(`catch error happened,error Name is ${error.name}`); console.info(`catch error happened,error Code is ${error.code}`); console.info(`catch error happened,error Message is ${error.message}`); -} + } -// 设置'playbackCompleted'事件回调,播放完成触发 -function SetCallBack(videoPlayer) { - videoPlayer.on('playbackCompleted', () => { - console.info('video play finish'); - - await videoPlayer.release().then(() => { - console.info('release success'); - }, failureCallback).catch(catchCallback); - - videoPlayer = undefined; - surfaceID = undefined; + // 用于打印视频轨道信息 + printfDescription(obj) { + for (let item in obj) { + let property = obj[item]; + console.info('key is ' + item); + console.info('value is ' + property); + } + } + + async videoPlayerDemo() { + let videoPlayer = undefined; + let surfaceID = 'test' // surfaceID用于播放画面显示,具体的值需要通过Xcomponent接口获取,相关文档链接: + let fdPath = 'fd://' + // path路径的码流可通过"hdc file send D:\xxx\H264_AAC.mp4 /data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上 + let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile/H264_AAC.mp4'; + await fileIO.open(path).then((fdNumber) => { + fdPath = fdPath + '' + fdNumber; + console.info('open fd sucess fd is' + fdPath); + }, (err) => { + console.info('open fd failed err is' + err); + }).catch((err) => { + console.info('open fd failed err is' + err); }); -} - -// 调用createVideoPlayer接口返回videoPlayer实例对象 -await media.createVideoPlayer().then((video) => { - if (typeof (video) != 'undefined') { + // 调用createVideoPlayer接口返回videoPlayer实例对象 + await media.createVideoPlayer().then((video) => { + if (typeof (video) != 'undefined') { console.info('createVideoPlayer success!'); videoPlayer = video; - } else { + } else { console.info('createVideoPlayer fail!'); - } -}, failureCallback).catch(catchCallback); - -// 设置事件回调 -SetCallBack(videoPlayer); - -// 用户选择视频设置fd(本地播放) -let fdPath = 'fd://' -// path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/accounts/account_0/appdata" 命令,将其推送到设备上 -let path = '/data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp4'; -await fileIO.open(path).then(fdNumber) => { - fdPath = fdPath + '' + fdNumber; - console.info('open fd sucess fd is' + fdPath); -}, (err) => { - console.info('open fd failed err is' + err); -}),catch((err) => { - console.info('open fd failed err is' + err); -}); - -videoPlayer.url = fdPath; - -// 设置surfaceID用于显示视频画面 -await videoPlayer.setDisplaySurface(surfaceID).then(() => { - console.info('setDisplaySurface success'); -}, failureCallback).catch(catchCallback); - -// 调用prepare完成播放前准备工作 -await videoPlayer.prepare().then(() => { - console.info('prepare success'); -}, failureCallback).catch(catchCallback); - -// 调用play接口正式开始播放 -await videoPlayer.play().then(() => { - console.info('play success'); -}, failureCallback).catch(catchCallback); + } + }, this.failureCallback).catch(this.catchCallback); + // 设置播放源 + videoPlayer.url = fdPath; + + // 设置surfaceID用于显示视频画面 + await videoPlayer.setDisplaySurface(surfaceID).then(() => { + console.info('setDisplaySurface success'); + }, this.failureCallback).catch(this.catchCallback); + + // 调用prepare完成播放前准备工作 + await videoPlayer.prepare().then(() => { + console.info('prepare success'); + }, this.failureCallback).catch(this.catchCallback); + + // 调用play接口正式开始播放 + await videoPlayer.play().then(() => { + console.info('play success'); + }, this.failureCallback).catch(this.catchCallback); + + // 结束播放 + await videoPlayer.stop().then(() => { + console.info('stop success'); + }, this.failureCallback).catch(this.catchCallback); + + // 释放播放资源 + await videoPlayer.release().then(() => { + console.info('release success'); + }, this.failureCallback).catch(this.catchCallback); + + // 相关对象置undefined + videoPlayer = undefined; + surfaceID = undefined; + } +} ``` ### 切视频场景 @@ -272,122 +266,110 @@ await videoPlayer.play().then(() => { ```js import media from '@ohos.multimedia.media' import fileIO from '@ohos.fileio' - -let videoPlayer = undefined; // 用于保存createVideoPlayer创建的对象 -let surfaceID = undefined; // 用于保存Xcomponent接口返回的surfaceID - -// 调用Xcomponent的接口用于获取surfaceID,并保存在surfaceID变量中,该接口由XComponent组件默认加载,非主动调用 -LoadXcomponent() { - surfaceID = this.$element('Xcomponent').getXComponentSurfaceId(); - console.info('LoadXcomponent surfaceID is' + surfaceID); -} - -// 函数调用发生错误时用于上报错误信息 -function failureCallback(error) { +export class VideoPlayerDemo { + // 函数调用发生错误时用于上报错误信息 + failureCallback(error) { console.info(`error happened,error Name is ${error.name}`); console.info(`error happened,error Code is ${error.code}`); console.info(`error happened,error Message is ${error.message}`); -} + } -// 当函数调用发生异常时用于上报错误信息 -function catchCallback(error) { + // 当函数调用发生异常时用于上报错误信息 + catchCallback(error) { console.info(`catch error happened,error Name is ${error.name}`); console.info(`catch error happened,error Code is ${error.code}`); console.info(`catch error happened,error Message is ${error.message}`); -} + } -// 设置'playbackCompleted'事件回调,播放完成触发 -function SetCallBack(videoPlayer) { - videoPlayer.on('playbackCompleted', () => { - console.info('video play finish'); - - await videoPlayer.release().then(() => { - console.info('release success'); - }, failureCallback).catch(catchCallback); - - videoPlayer = undefined; - surfaceID = undefined; + // 用于打印视频轨道信息 + printfDescription(obj) { + for (let item in obj) { + let property = obj[item]; + console.info('key is ' + item); + console.info('value is ' + property); + } + } + + async videoPlayerDemo() { + let videoPlayer = undefined; + let surfaceID = 'test' // surfaceID用于播放画面显示,具体的值需要通过Xcomponent接口获取,相关文档链接: + let fdPath = 'fd://' + // path路径的码流可通过"hdc file send D:\xxx\H264_AAC.mp4 /data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上 + let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile/H264_AAC.mp4'; + let nextPath = '/data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile/MP4_AAC.mp4'; + await fileIO.open(path).then((fdNumber) => { + fdPath = fdPath + '' + fdNumber; + console.info('open fd sucess fd is' + fdPath); + }, (err) => { + console.info('open fd failed err is' + err); + }).catch((err) => { + console.info('open fd failed err is' + err); }); -} - -// 调用createVideoPlayer接口返回videoPlayer实例对象 -await media.createVideoPlayer().then((video) => { - if (typeof (video) != 'undefined') { + // 调用createVideoPlayer接口返回videoPlayer实例对象 + await media.createVideoPlayer().then((video) => { + if (typeof (video) != 'undefined') { console.info('createVideoPlayer success!'); videoPlayer = video; - } else { + } else { console.info('createVideoPlayer fail!'); - } -}, failureCallback).catch(catchCallback); - -// 设置事件回调 -SetCallBack(videoPlayer); - -// 用户选择视频设置fd(本地播放) -let fdPath = 'fd://' -// path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/accounts/account_0/appdata" 命令,将其推送到设备上 -let path = '/data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp4'; -await fileIO.open(path).then(fdNumber) => { - fdPath = fdPath + '' + fdNumber; - console.info('open fd sucess fd is' + fdPath); -}, (err) => { - console.info('open fd failed err is' + err); -}),catch((err) => { - console.info('open fd failed err is' + err); -}); - -videoPlayer.url = fdPath; - -// 设置surfaceID用于显示视频画面 -await videoPlayer.setDisplaySurface(surfaceID).then(() => { - console.info('setDisplaySurface success'); -}, failureCallback).catch(catchCallback); - -// 调用prepare完成播放前准备工作 -await videoPlayer.prepare().then(() => { - console.info('prepare success'); -}, failureCallback).catch(catchCallback); - -// 调用play接口正式开始播放 -await videoPlayer.play().then(() => { - console.info('play success'); -}, failureCallback).catch(catchCallback); - -// 播放一段时间后,下发切视频指令 -// 重置播放配置 -await videoPlayer.reset().then(() => { - console.info('reset success'); -}, failureCallback).catch(catchCallback); - -// 用户选择视频设置fd(本地播放) -let fdNextPath = 'fd://' -// path路径的码流可通过"hdc file send D:\xxx\02.mp3 /data/accounts/account_0/appdata" 命令,将其推送到设备上 -let nextPath = '/data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/02.mp4'; -await fileIO.open(nextPath).then(fdNumber) => { - fdNextPath = fdNextPath + '' + fdNumber; - console.info('open fd sucess fd is' + fdNextPath); -}, (err) => { - console.info('open fd failed err is' + err); -}),catch((err) => { - console.info('open fd failed err is' + err); -}); - -videoPlayer.url = fdNextPath; - -// 设置surfaceID用于显示视频画面 -await videoPlayer.setDisplaySurface(surfaceID).then(() => { - console.info('setDisplaySurface success'); -}, failureCallback).catch(catchCallback); - -// 调用prepare完成播放前准备工作 -await videoPlayer.prepare().then(() => { - console.info('prepare success'); -}, failureCallback).catch(catchCallback); - -// 调用play接口正式开始播放 -await videoPlayer.play().then(() => { - console.info('play success'); -}, failureCallback).catch(catchCallback); + } + }, this.failureCallback).catch(this.catchCallback); + // 设置播放源 + videoPlayer.url = fdPath; + + // 设置surfaceID用于显示视频画面 + await videoPlayer.setDisplaySurface(surfaceID).then(() => { + console.info('setDisplaySurface success'); + }, this.failureCallback).catch(this.catchCallback); + + // 调用prepare完成播放前准备工作 + await videoPlayer.prepare().then(() => { + console.info('prepare success'); + }, this.failureCallback).catch(this.catchCallback); + + // 调用play接口正式开始播放 + await videoPlayer.play().then(() => { + console.info('play success'); + }, this.failureCallback).catch(this.catchCallback); + + // 重置播放配置 + await videoPlayer.reset().then(() => { + console.info('reset success'); + }, this.failureCallback).catch(this.catchCallback); + + // 获取下一个视频fd地址 + fdPath = 'fd://' + await fileIO.open(nextPath).then((fdNumber) => { + fdPath = fdPath + '' + fdNumber; + console.info('open fd sucess fd is' + fdPath); + }, (err) => { + console.info('open fd failed err is' + err); + }).catch((err) => { + console.info('open fd failed err is' + err); + }); + // 设置第二个视频播放源 + videoPlayer.url = fdPath; + + // 调用prepare完成播放前准备工作 + await videoPlayer.prepare().then(() => { + console.info('prepare success'); + }, this.failureCallback).catch(this.catchCallback); + + // 调用play接口正式开始播放 + await videoPlayer.play().then(() => { + console.info('play success'); + }, this.failureCallback).catch(this.catchCallback); + + // 释放播放资源 + await videoPlayer.release().then(() => { + console.info('release success'); + }, this.failureCallback).catch(this.catchCallback); + + // 相关对象置undefined + videoPlayer = undefined; + surfaceID = undefined; + } +} ``` ### 单个视频循环场景 @@ -395,100 +377,88 @@ await videoPlayer.play().then(() => { ```js import media from '@ohos.multimedia.media' import fileIO from '@ohos.fileio' - -let videoPlayer = undefined; // 用于保存createVideoPlayer创建的对象 -let surfaceID = undefined; // 用于保存Xcomponent接口返回的surfaceID - -// 调用Xcomponent的接口用于获取surfaceID,并保存在surfaceID变量中,该接口由XComponent组件默认加载,非主动调用 -LoadXcomponent() { - surfaceID = this.$element('Xcomponent').getXComponentSurfaceId(); - console.info('LoadXcomponent surfaceID is' + surfaceID); -} - -// 函数调用发生错误时用于上报错误信息 -function failureCallback(error) { +export class VideoPlayerDemo { + // 函数调用发生错误时用于上报错误信息 + failureCallback(error) { console.info(`error happened,error Name is ${error.name}`); console.info(`error happened,error Code is ${error.code}`); console.info(`error happened,error Message is ${error.message}`); -} + } -// 当函数调用发生异常时用于上报错误信息 -function catchCallback(error) { + // 当函数调用发生异常时用于上报错误信息 + catchCallback(error) { console.info(`catch error happened,error Name is ${error.name}`); console.info(`catch error happened,error Code is ${error.code}`); console.info(`catch error happened,error Message is ${error.message}`); -} + } -// 设置'playbackCompleted'事件回调,播放完成触发 -function SetCallBack(videoPlayer) { - videoPlayer.on('playbackCompleted', () => { - console.info('video play finish'); - - await videoPlayer.release().then(() => { - console.info('release success'); - }, failureCallback).catch(catchCallback); - - videoPlayer = undefined; - surfaceID = undefined; + // 用于打印视频轨道信息 + printfDescription(obj) { + for (let item in obj) { + let property = obj[item]; + console.info('key is ' + item); + console.info('value is ' + property); + } + } + + sleep(time) { + for(let t = Date.now(); Date.now() - t <= time;); + } + + async videoPlayerDemo() { + let videoPlayer = undefined; + let surfaceID = 'test' // surfaceID用于播放画面显示,具体的值需要通过Xcomponent接口获取,相关文档链接: + let fdPath = 'fd://' + // path路径的码流可通过"hdc file send D:\xxx\H264_AAC.mp4 /data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上 + let path = '/data/app/el1/bundle/public/ohos.acts.multimedia.video.videoplayer/ohos.acts.multimedia.video.videoplayer/assets/entry/resources/rawfile/H264_AAC.mp4'; + await fileIO.open(path).then((fdNumber) => { + fdPath = fdPath + '' + fdNumber; + console.info('open fd sucess fd is' + fdPath); + }, (err) => { + console.info('open fd failed err is' + err); + }).catch((err) => { + console.info('open fd failed err is' + err); }); -} - -// 调用createVideoPlayer接口返回videoPlayer实例对象 -await media.createVideoPlayer().then((video) => { - if (typeof (video) != 'undefined') { + // 调用createVideoPlayer接口返回videoPlayer实例对象 + await media.createVideoPlayer().then((video) => { + if (typeof (video) != 'undefined') { console.info('createVideoPlayer success!'); videoPlayer = video; - } else { + } else { console.info('createVideoPlayer fail!'); - } -}, failureCallback).catch(catchCallback); - -// 设置事件回调 -SetCallBack(videoPlayer); - -// 用户选择视频设置fd(本地播放) -let fdPath = 'fd://' -// path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/accounts/account_0/appdata" 命令,将其推送到设备上 -let path = '/data/accounts/account_0/appdata/ohos.xxx.xxx.xxx/01.mp4'; -await fileIO.open(path).then(fdNumber) => { - fdPath = fdPath + '' + fdNumber; - console.info('open fd sucess fd is' + fdPath); -}, (err) => { - console.info('open fd failed err is' + err); -}),catch((err) => { - console.info('open fd failed err is' + err); -}); - -videoPlayer.url = fdPath; - -// 设置surfaceID用于显示视频画面 -await videoPlayer.setDisplaySurface(surfaceID).then(() => { - console.info('setDisplaySurface success'); -}, failureCallback).catch(catchCallback); - -// 调用prepare完成播放前准备工作 -await videoPlayer.prepare().then(() => { - console.info('prepare success'); -}, failureCallback).catch(catchCallback); - -// 设置循环播放属性 -videoPlayer.loop = true; - -// 调用play接口正式开始播放 -await videoPlayer.play().then(() => { - console.info('play success'); -}, failureCallback).catch(catchCallback); -``` - -### Xcomponent创建方法 - -播放视频中获取surfaceID依赖了Xcomponent,需要创建一个和xxx.js同名的xxx.hml文件,xxx.hml里面需要添加如下代码: - -```js - // 设置窗口宽高等属性 - + } + }, this.failureCallback).catch(this.catchCallback); + // 设置播放源 + videoPlayer.url = fdPath; + + // 设置surfaceID用于显示视频画面 + await videoPlayer.setDisplaySurface(surfaceID).then(() => { + console.info('setDisplaySurface success'); + }, this.failureCallback).catch(this.catchCallback); + + // 调用prepare完成播放前准备工作 + await videoPlayer.prepare().then(() => { + console.info('prepare success'); + }, this.failureCallback).catch(this.catchCallback); + // 设置循环播放属性 + videoPlayer.loop = true; + // 调用play接口正式开始播放 + await videoPlayer.play().then(() => { + console.info('play success'); + }, this.failureCallback).catch(this.catchCallback); + // 进度条执行到末尾后,在播放放3秒钟,因为设置了循环播放,所以当进度条执行到末尾后会从头开始播放 + await videoPlayer.seek(videoPlayer.duration, media.SeekMode.SEEK_NEXT_SYNC).then((seekDoneTime) => { + console.info('seek duration success'); + }, this.failureCallback).catch(this.catchCallback); + this.sleep(3000); + // 释放播放资源 + await videoPlayer.release().then(() => { + console.info('release success'); + }, this.failureCallback).catch(this.catchCallback); + + // 相关对象置undefined + videoPlayer = undefined; + surfaceID = undefined; + } +} ``` diff --git a/zh-cn/application-dev/media/video-recorder.md b/zh-cn/application-dev/media/video-recorder.md index a088db0ec3258f50b7815aefbe81077c4d1a6ad4..e25375a5a471355af28060e20dcfc9988d618e52 100644 --- a/zh-cn/application-dev/media/video-recorder.md +++ b/zh-cn/application-dev/media/video-recorder.md @@ -25,12 +25,11 @@ ```js import media from '@ohos.multimedia.media' import mediaLibrary from '@ohos.multimedia.mediaLibrary' - -let testFdNumber; - -// pathName是传入的录制文件名,例如:01.mp4,生成后的文件地址:/storage/media/100/local/files/Movies/01.mp4 -// 使用mediaLibrary需要添加以下权限, ohos.permission.MEDIA_LOCATION、ohos.permission.WRITE_MEDIA、ohos.permission.READ_MEDIA -async function getFd(pathName) { +export class VideoRecorderDemo { + private testFdNumber; // 用于保存fd地址 + // pathName是传入的录制文件名,例如:01.mp3,生成后的文件地址:/storage/media/100/local/files/Video/01.mp4 + // 使用mediaLibrary需要添加以下权限, ohos.permission.MEDIA_LOCATION、ohos.permission.WRITE_MEDIA、ohos.permission.READ_MEDIA + async getFd(pathName) { let displayName = pathName; const mediaTest = mediaLibrary.getMediaLibrary(); let fileKeyObj = mediaLibrary.FileKey; @@ -38,111 +37,116 @@ async function getFd(pathName) { let publicPath = await mediaTest.getPublicDirectory(mediaLibrary.DirectoryType.DIR_VIDEO); let dataUri = await mediaTest.createAsset(mediaType, displayName, publicPath); if (dataUri != undefined) { - let args = dataUri.id.toString(); - let fetchOp = { - selections : fileKeyObj.ID + "=?", - selectionArgs : [args], - } - let fetchFileResult = await mediaTest.getFileAssets(fetchOp); - let fileAsset = await fetchFileResult.getAllObject(); - let fdNumber = await fileAsset[0].open('Rw'); - fdNumber = "fd://" + fdNumber.toString(); - testFdNumber = fdNumber; + let args = dataUri.id.toString(); + let fetchOp = { + selections : fileKeyObj.ID + "=?", + selectionArgs : [args], + } + let fetchFileResult = await mediaTest.getFileAssets(fetchOp); + let fileAsset = await fetchFileResult.getAllObject(); + let fdNumber = await fileAsset[0].open('Rw'); + this.testFdNumber = "fd://" + fdNumber.toString(); + } + } + + // 当发生错误上上报的错误回调接口 + failureCallback(error) { + console.info('error happened, error name is ' + error.name); + console.info('error happened, error code is ' + error.code); + console.info('error happened, error message is ' + error.message); + } + + // 当发生异常时,系统调用的错误回调接口 + catchCallback(error) { + console.info('catch error happened, error name is ' + error.name); + console.info('catch error happened, error code is ' + error.code); + console.info('catch error happened, error message is ' + error.message); + } + + async videoRecorderDemo() { + let videoRecorder = null; // videoRecorder空对象在createVideoRecorder成功后赋值 + let surfaceID = null; // 用于保存getInputSurface返回的surfaceID + // 获取需要录制的视频的fd地址 + await this.getFd('01.mp4'); + // 录制相关参数配置 + let videoProfile = { + audioBitrate : 48000, + audioChannels : 2, + audioCodec : 'audio/mp4a-latm', + audioSampleRate : 48000, + fileFormat : 'mp4', + videoBitrate : 48000, + videoCodec : 'video/mp4v-es', + videoFrameWidth : 640, + videoFrameHeight : 480, + videoFrameRate : 30 } -} - -await getFd('01.mp4'); - -let videoProfile = { - audioBitrate : 48000, - audioChannels : 2, - audioCodec : 'audio/mp4a-latm', - audioSampleRate : 48000, - fileFormat : 'mp4', - videoBitrate : 48000, - videoCodec : 'video/mp4v-es', - videoFrameWidth : 640, - videoFrameHeight : 480, - videoFrameRate : 30 -} -let videoConfig = { - audioSourceType : 1, - videoSourceType : 0, - profile : videoProfile, - url : testFdNumber, // testFdNumber由getFd生成 - orientationHint : 0, - location : { latitude : 30, longitude : 130 }, -} - -// 当发生错误上上报的错误回调接口 -function failureCallback(error) { - console.info('error happened, error name is ' + error.name); - console.info('error happened, error code is ' + error.code); - console.info('error happened, error message is ' + error.message); -} - -// 当发生异常时,系统调用的错误回调接口 -function catchCallback(error) { - console.info('catch error happened, error name is ' + error.name); - console.info('catch error happened, error code is ' + error.code); - console.info('catch error happened, error message is ' + error.message); -} - -let videoRecorder = null; // videoRecorder空对象在createVideoRecorder成功后赋值 -let surfaceID = null; // 用于保存getInputSurface返回的surfaceID - -// 创建videoRecorder对象 -await media.createVideoRecorder().then((recorder) => { - console.info('case createVideoRecorder called'); - if (typeof (recorder) != 'undefined') { + let videoConfig = { + audioSourceType : 1, + videoSourceType : 0, + profile : videoProfile, + url : this.testFdNumber, // testFdNumber由getFd生成 + orientationHint : 0, + location : { latitude : 30, longitude : 130 }, + } + // 创建videoRecorder对象 + await media.createVideoRecorder().then((recorder) => { + console.info('case createVideoRecorder called'); + if (typeof (recorder) != 'undefined') { videoRecorder = recorder; console.info('createVideoRecorder success'); - } else { + } else { console.info('createVideoRecorder failed'); - } -}, failureCallback).catch(catchCallback); - -// 获取surfaceID并保存下来传递给camera相关接口 -await videoRecorder.getInputSurface().then((surface) => { - console.info('getInputSurface success'); - surfaceID = surface; -}, failureCallback).catch(catchCallback); - -// 视频录制依赖相机相关接口,以下需要先调用相机起流接口后才能继续执行 - -// 视频录制启动接口 -await videoRecorder.start().then(() => { - console.info('start success'); -}, failureCallback).catch(catchCallback); - -// 调用pause接口时需要暂停camera出流 -await videoRecorder.pause().then(() => { - console.info('pause success'); -}, failureCallback).catch(catchCallback); - -// 调用resume接口时需要恢复camera出流 -await videoRecorder.resume().then(() => { - console.info('resume success'); -}, failureCallback).catch(catchCallback); - -// 停止camera出流后,停止视频录制 -await videoRecorder.stop().then(() => { - console.info('stop success'); -}, failureCallback).catch(catchCallback); - -// 重置录制相关配置 -await videoRecorder.reset().then(() => { - console.info('reset success'); -}, failureCallback).catch(catchCallback); - -// 释放视频录制相关资源并释放camera对象相关资源 -await videoRecorder.release().then(() => { - console.info('release success'); -}, failureCallback).catch(catchCallback); - -// 相关对象置null -videoRecorder = null; -surfaceID = null; + } + }, this.failureCallback).catch(this.catchCallback); + + // 调用prepare完成视频录制前的准备工作 + await videoRecorder.prepare(videoConfig).then(() => { + console.info('prepare success'); + }, this.failureCallback).catch(this.catchCallback); + + // 获取surfaceID并保存下来传递给camera相关接口 + await videoRecorder.getInputSurface().then((surface) => { + console.info('getInputSurface success'); + surfaceID = surface; + }, this.failureCallback).catch(this.catchCallback); + + // 视频录制依赖相机相关接口,以下需要先调用相机起流接口后才能继续执行,具体的相机接口调用请参考sample用例 + // 视频录制启动接口 + await videoRecorder.start().then(() => { + console.info('start success'); + }, this.failureCallback).catch(this.catchCallback); + + // 调用pause接口时需要暂停camera出流 + await videoRecorder.pause().then(() => { + console.info('pause success'); + }, this.failureCallback).catch(this.catchCallback); + + // 调用resume接口时需要恢复camera出流 + await videoRecorder.resume().then(() => { + console.info('resume success'); + }, this.failureCallback).catch(this.catchCallback); + + // 停止camera出流后,停止视频录制 + await videoRecorder.stop().then(() => { + console.info('stop success'); + }, this.failureCallback).catch(this.catchCallback); + + // 重置录制相关配置 + await videoRecorder.reset().then(() => { + console.info('reset success'); + }, this.failureCallback).catch(this.catchCallback); + + // 释放视频录制相关资源并释放camera对象相关资源 + await videoRecorder.release().then(() => { + console.info('release success'); + }, this.failureCallback).catch(this.catchCallback); + + // 相关对象置null + videoRecorder = undefined; + surfaceID = undefined; + } +} ```