Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
b676aa7f
D
Docs
项目概览
OpenHarmony
/
Docs
12 个月 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
b676aa7f
编写于
4月 21, 2022
作者:
O
openharmony_ci
提交者:
Gitee
4月 21, 2022
浏览文件
操作
浏览文件
下载
差异文件
!3383 音频播放demo修改
Merge pull request !3383 from hwgaizw/ziliao
上级
798c8522
ee4a9432
变更
4
展开全部
隐藏空白更改
内联
并排
Showing
4 changed file
with
719 addition
and
759 deletion
+719
-759
zh-cn/application-dev/media/audio-playback.md
zh-cn/application-dev/media/audio-playback.md
+149
-156
zh-cn/application-dev/media/audio-recorder.md
zh-cn/application-dev/media/audio-recorder.md
+122
-129
zh-cn/application-dev/media/video-playback.md
zh-cn/application-dev/media/video-playback.md
+337
-367
zh-cn/application-dev/media/video-recorder.md
zh-cn/application-dev/media/video-recorder.md
+111
-107
未找到文件。
zh-cn/application-dev/media/audio-playback.md
浏览文件 @
b676aa7f
...
...
@@ -28,26 +28,37 @@ AudioPlayer支持的src媒体源输入类型可参考:[src属性说明](../ref
import
media
from
'
@ohos.multimedia.media
'
import
fileIO
from
'
@ohos.fileio
'
function
SetCallBack
(
audioPlayer
)
{
//打印码流轨道信息
function
printfDescription
(
obj
)
{
for
(
let
item
in
obj
)
{
let
property
=
obj
[
item
];
console
.
info
(
'
audio key is
'
+
item
);
console
.
info
(
'
audio value is
'
+
property
);
}
}
//设置播放器回调函数
function
setCallBack
(
audioPlayer
)
{
audioPlayer
.
on
(
'
dataLoad
'
,
()
=>
{
//设置'dataLoad'事件回调,src属性设置成功后,触发此回调
console
.
info
(
'
audio set source success
'
);
//播放界面可切换至已准备好,可点击播放按钮进行播放状态
audioPlayer
.
play
();
//需等待'dataLoad'事件回调完成后,才可调用play进行播放,触发'play'事件回调
});
audioPlayer
.
on
(
'
play
'
,
()
=>
{
//设置'play'事件回调
console
.
info
(
'
audio play success
'
);
//将播放按钮切换至可暂停状态
audioPlayer
.
pause
();
//触发'pause'事件回调,暂停播放
});
audioPlayer
.
on
(
'
pause
'
,
()
=>
{
//设置'pause'事件回调
console
.
info
(
'
audio pause success
'
);
//将播放按钮切换至可播放状态
audioPlayer
.
seek
(
5000
);
//触发'timeUpdate'事件回调,seek到5000ms处播放
});
audioPlayer
.
on
(
'
stop
'
,
()
=>
{
//设置'stop'事件回调
console
.
info
(
'
audio stop success
'
);
//播放停止,播放进度条归零,播放按钮切换至可播放状态
audioPlayer
.
reset
();
//触发'reset'事件回调后,重新设置src属性,可完成切歌
});
audioPlayer
.
on
(
'
reset
'
,
()
=>
{
//设置'reset'事件回调
console
.
info
(
'
audio reset success
'
);
//需重新设置src属性后,可继续播放其他音频
audioPlayer
.
release
();
//audioPlayer资源被销毁
audioPlayer
=
undefined
;
});
audioPlayer
.
on
(
'
timeUpdate
'
,
(
seekDoneTime
)
=>
{
//设置'timeUpdate'事件回调
if
(
typeof
(
seekDoneTime
)
==
'
undefined
'
)
{
...
...
@@ -55,72 +66,49 @@ function SetCallBack(audioPlayer) {
return
;
}
console
.
info
(
'
audio seek success, and seek time is
'
+
seekDoneTime
);
//播放进度条更新到seek对应的位置
audioPlayer
.
setVolume
(
0.5
);
//触发'volumeChange'事件回调
});
audioPlayer
.
on
(
'
volumeChange
'
,
()
=>
{
//设置'volumeChange'事件回调
console
.
info
(
'
audio volumeChange success
'
);
//更新音量显示
audioPlayer
.
getTrackDescription
((
error
,
arrlist
)
=>
{
//通过回调方式获取音频轨道信息
if
(
typeof
(
arrlist
)
!=
'
undefined
'
)
{
for
(
let
i
=
0
;
i
<
arrlist
.
length
;
i
++
)
{
printfDescription
(
arrlist
[
i
]);
}
}
else
{
console
.
log
(
`audio getTrackDescription fail, error:
${
error
.
message
}
`
);
}
audioPlayer
.
stop
();
//触发'stop'事件回调,停止播放
});
});
audioPlayer
.
on
(
'
finish
'
,
()
=>
{
//设置'finish'事件回调,播放完成触发
audioPlayer
.
on
(
'
finish
'
,
()
=>
{
//设置'finish'事件回调,播放完成触发
console
.
info
(
'
audio play finish
'
);
});
audioPlayer
.
on
(
'
error
'
,
(
error
)
=>
{
//设置'error'事件回调
audioPlayer
.
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
}
`
);
});
}
function
printfDescription
(
obj
)
{
for
(
let
item
in
obj
)
{
let
property
=
obj
[
item
];
console
.
info
(
'
audio key is
'
+
item
);
console
.
info
(
'
audio value is
'
+
property
);
}
async
function
audioPlayerDemo
()
{
// 1. 创建实例
let
audioPlayer
=
media
.
createAudioPlayer
();
setCallBack
(
audioPlayer
);
//设置事件回调
//2. 用户选择音频,设置uri
let
fdPath
=
'
fd://
'
// path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上
let
path
=
'
/data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile/01.mp3
'
;
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
);
});
audioPlayer
.
src
=
fdPath
;
//设置src属性,并触发'dataLoad'事件回调
}
//1. 创建实例
let
audioPlayer
=
media
.
createAudioPlayer
();
SetCallBack
(
audioPlayer
);
//设置事件回调
//2. 用户选择音频,设置uri
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.mp3
'
;
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
);
});
audioPlayer
.
src
=
fdPath
;
//设置src属性,并触发'dataLoad'事件回调
//3. 播放音频
audioPlayer
.
play
();
//需等待'dataLoad'事件回调完成后,才可调用play进行播放,触发'play'事件回调
//4. 跳转播放位置
audioPlayer
.
seek
(
30000
);
//触发'timeUpdate'事件回调,seek到30000ms处播放
//5. 设置音量
audioPlayer
.
setVolume
(
0.5
);
//触发'volumeChange'事件回调
//6. 暂停播放
audioPlayer
.
pause
();
//触发'pause'事件回调,暂停播放
//7. 获取轨道信息
audioPlayer
.
getTrackDescription
((
error
,
arrlist
)
=>
{
//通过回调方式获取音频轨道信息
if
(
typeof
(
arrlist
)
!=
'
undefined
'
)
{
for
(
let
i
=
0
;
i
<
arrlist
.
length
;
i
++
)
{
printfDescription
(
arrlist
[
i
]);
}
}
else
{
console
.
log
(
`audio getTrackDescription fail, error:
${
error
.
message
}
`
);
}
});
//8. 停止播放
audioPlayer
.
stop
();
//触发'stop'事件回调
//9. 重置播放资源
audioPlayer
.
reset
();
//触发'reset'事件回调后,重新设置src属性,可完成切歌
//10. 释放资源
audioPlayer
.
release
();
//audioPlayer资源被销毁
audioPlayer
=
undefined
;
```
### 正常播放场景
...
...
@@ -128,38 +116,40 @@ audioPlayer = undefined;
```
js
import
media
from
'
@ohos.multimedia.media
'
import
fileIO
from
'
@ohos.fileio
'
function
SetCallBack
(
audioPlayer
)
{
export
class
AudioDemo
{
// 设置播放器回调函数
setCallBack
(
audioPlayer
)
{
audioPlayer
.
on
(
'
dataLoad
'
,
()
=>
{
//设置'dataLoad'事件回调,src属性设置成功后,触发此回调
console
.
info
(
'
audio set source success
'
);
audioPlayer
.
play
();
//调用play方法开始播放,触发'play'事件回调
console
.
info
(
'
audio set source success
'
);
audioPlayer
.
play
();
//调用play方法开始播放,触发'play'事件回调
});
audioPlayer
.
on
(
'
play
'
,
()
=>
{
//设置'play'事件回调
console
.
info
(
'
audio play success
'
);
console
.
info
(
'
audio play success
'
);
});
audioPlayer
.
on
(
'
finish
'
,
()
=>
{
//设置'finish'事件回调,播放完成触发
console
.
info
(
'
audio play finish
'
);
audioPlayer
.
release
();
//audioPlayer资源被销毁
audioPlayer
=
undefined
;
console
.
info
(
'
audio play finish
'
);
audioPlayer
.
release
();
//audioPlayer资源被销毁
audioPlayer
=
undefined
;
});
}
async
audioPlayerDemo
()
{
let
audioPlayer
=
media
.
createAudioPlayer
();
//创建一个音频播放实例
this
.
setCallBack
(
audioPlayer
);
//设置事件回调
let
fdPath
=
'
fd://
'
// path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上
let
path
=
'
/data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile/01.mp3
'
;
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
);
});
audioPlayer
.
src
=
fdPath
;
//设置src属性,并触发'dataLoad'事件回调
}
}
let
audioPlayer
=
media
.
createAudioPlayer
();
//创建一个音频播放实例
SetCallBack
(
audioPlayer
);
//设置事件回调
/* 用户选择音频设置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.mp3
'
;
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
);
});
audioPlayer
.
src
=
fdPath
;
//设置src属性,并触发'dataLoad'事件回调
```
### 切歌场景
...
...
@@ -167,54 +157,62 @@ audioPlayer.src = fdPath; //设置src属性,并触
```
js
import
media
from
'
@ohos.multimedia.media
'
import
fileIO
from
'
@ohos.fileio
'
function
SetCallBack
(
audioPlayer
)
{
export
class
AudioDemo
{
// 设置播放器回调函数
private
isNextMusic
=
false
;
setCallBack
(
audioPlayer
)
{
audioPlayer
.
on
(
'
dataLoad
'
,
()
=>
{
//设置'dataLoad'事件回调,src属性设置成功后,触发此回调
console
.
info
(
'
audio set source success
'
);
audioPlayer
.
play
();
//调用play方法开始播放,触发'play'事件回调
console
.
info
(
'
audio set source success
'
);
audioPlayer
.
play
();
//调用play方法开始播放,触发'play'事件回调
});
audioPlayer
.
on
(
'
play
'
,
()
=>
{
//设置'play'事件回调
console
.
info
(
'
audio play success
'
);
console
.
info
(
'
audio play success
'
);
audioPlayer
.
reset
();
//调用reset方法,出发'reset'事件回调
});
audioPlayer
.
on
(
'
finish
'
,
()
=>
{
//设置'finish'事件回调,播放完成触发
console
.
info
(
'
audio play finish
'
);
audioPlayer
.
on
(
'
reset
'
,
()
=>
{
//设置'reset'事件回调
console
.
info
(
'
audio play success
'
);
if
(
!
this
.
isNextMusic
)
{
//当isNextMusic 为false时,实现切歌功能
this
.
nextMusic
(
audioPlayer
);
//实现切歌功能
}
else
{
audioPlayer
.
release
();
//audioPlayer资源被销毁
audioPlayer
=
undefined
;
audioPlayer
=
undefined
;
}
});
}
async
nextMusic
(
audioPlayer
)
{
this
.
isNextMusic
=
true
;
let
nextFdPath
=
'
fd://
'
// path路径的码流可通过"hdc file send D:\xxx\02.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上
let
nextpath
=
'
/data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile/02.mp3
'
;
await
fileIO
.
open
(
nextpath
).
then
((
fdNumber
)
=>
{
nextFdPath
=
nextFdPath
+
''
+
fdNumber
;
console
.
info
(
'
open fd sucess fd is
'
+
nextFdPath
);
},
(
err
)
=>
{
console
.
info
(
'
open fd failed err is
'
+
err
);
}).
catch
((
err
)
=>
{
console
.
info
(
'
open fd failed err is
'
+
err
);
});
audioPlayer
.
src
=
nextFdPath
;
//设置src属性,并重新触发触发'dataLoad'事件回调
}
async
audioPlayerDemo
()
{
let
audioPlayer
=
media
.
createAudioPlayer
();
//创建一个音频播放实例
this
.
setCallBack
(
audioPlayer
);
//设置事件回调
let
fdPath
=
'
fd://
'
// path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上
let
path
=
'
/data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile/01.mp3
'
;
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
);
});
audioPlayer
.
src
=
fdPath
;
//设置src属性,并触发'dataLoad'事件回调
}
}
let
audioPlayer
=
media
.
createAudioPlayer
();
//创建一个音频播放实例
SetCallBack
(
audioPlayer
);
//设置事件回调
/* 用户选择音频设置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.mp3
'
;
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
);
});
audioPlayer
.
src
=
fdPath
;
//设置src属性,并触发'dataLoad'事件回调
/* 播放一段时间后,下发切歌指令 */
audioPlayer
.
reset
();
/* 用户选择音频设置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.mp3
'
;
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
);
});
audioPlayer
.
src
=
fdNextPath
;
```
### 单曲循环场景
...
...
@@ -222,40 +220,36 @@ audioPlayer.src = fdNextPath;
```
js
import
media
from
'
@ohos.multimedia.media
'
import
fileIO
from
'
@ohos.fileio
'
function
SetCallBack
(
audioPlayer
)
{
export
class
AudioDemo
{
// 设置播放器回调函数
setCallBack
(
audioPlayer
)
{
audioPlayer
.
on
(
'
dataLoad
'
,
()
=>
{
//设置'dataLoad'事件回调,src属性设置成功后,触发此回调
console
.
info
(
'
audio set source success
'
);
audioPlayer
.
play
();
//调用play方法开始播放,触发'play'事件回调
console
.
info
(
'
audio set source success
'
);
audioPlayer
.
loop
=
true
;
//设置循环播放属性
audioPlayer
.
play
();
//调用play方法开始播放,触发'play'事件回调
});
audioPlayer
.
on
(
'
play
'
,
()
=>
{
//设置'play'事件回调
console
.
info
(
'
audio play success
'
);
audioPlayer
.
on
(
'
play
'
,
()
=>
{
//设置'play'事件回调
,开始循环播放
console
.
info
(
'
audio play success
'
);
});
audioPlayer
.
on
(
'
finish
'
,
()
=>
{
//设置'finish'事件回调,播放完成触发
console
.
info
(
'
audio play finish
'
);
audioPlayer
.
release
();
//audioPlayer资源被销毁
audioPlayer
=
undefined
;
}
async
audioPlayerDemo
()
{
let
audioPlayer
=
media
.
createAudioPlayer
();
//创建一个音频播放实例
this
.
setCallBack
(
audioPlayer
);
//设置事件回调
let
fdPath
=
'
fd://
'
// path路径的码流可通过"hdc file send D:\xxx\01.mp3 /data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile" 命令,将其推送到设备上
let
path
=
'
/data/app/el1/bundle/public/ohos.acts.multimedia.audio.audioplayer/ohos.acts.multimedia.audio.audioplayer/assets/entry/resources/rawfile/01.mp3
'
;
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
);
});
audioPlayer
.
src
=
fdPath
;
//设置src属性,并触发'dataLoad'事件回调
}
}
let
audioPlayer
=
media
.
createAudioPlayer
();
//创建一个音频播放实例
SetCallBack
(
audioPlayer
);
//设置事件回调
/* 用户选择音频设置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.mp3
'
;
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
);
});
audioPlayer
.
src
=
fdPath
;
//设置src属性,并触发'dataLoad'事件回调
audioPlayer
.
loop
=
true
;
//设置循环播放属性
```
## 相关实例
...
...
@@ -263,7 +257,6 @@ audioPlayer.loop = true; //设置循环播放属性
针对音频播放开发,有以下相关实例可供参考:
-
[
`JsDistributedMusicPlayer`:分布式音乐播放(JS)(API7)
](
https://gitee.com/openharmony/app_samples/tree/master/ability/JsDistributedMusicPlayer
)
-
[
`JsAudioPlayer`:音频播放和管理(JS)(API7)
](
https://gitee.com/openharmony/app_samples/tree/master/media/JsAudioPlayer
)
-
[
`eTsAudioPlayer`: 音频播放器(eTS)
](
https://gitee.com/openharmony/app_samples/blob/master/media/Recorder/entry/src/main/ets/MainAbility/pages/Play.ets
)
-
[
音频播放器
](
https://gitee.com/openharmony/codelabs/tree/master/Media/Audio_OH_ETS
)
\ No newline at end of file
zh-cn/application-dev/media/audio-recorder.md
浏览文件 @
b676aa7f
...
...
@@ -25,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
(
'
re
set
'
,
()
=>
{
// 设置'reset
'事件回调
console
.
log
(
'
audio recorder reset
success
'
);
// 需要重新设置录制参数才能再次录制
audioRecorder
.
on
(
'
re
lease
'
,
()
=>
{
// 设置'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
;
...
...
@@ -72,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
;
```
### 正常录制场景
...
...
@@ -124,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
;
...
...
@@ -154,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
;
```
## 相关实例
...
...
@@ -198,5 +191,5 @@ audioRecorder = undefined;
针对音频录制开发,有以下相关实例可供参考:
-
[
`Recorder`:录音机(eTS)(API8)
](
https://gitee.com/openharmony/app_samples/tree/master/media/Recorder
)
-
[
`eTsAudioPlayer`: 音频播放器(eTS)
](
https://gitee.com/openharmony/app_samples/blob/master/media/Recorder/entry/src/main/ets/MainAbility/pages/Play.ets
)
-
[
音频播放器
](
https://gitee.com/openharmony/codelabs/tree/master/Media/Audio_OH_ETS
)
zh-cn/application-dev/media/video-playback.md
浏览文件 @
b676aa7f
此差异已折叠。
点击以展开。
zh-cn/application-dev/media/video-recorder.md
浏览文件 @
b676aa7f
...
...
@@ -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
;
}
}
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录