Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
da3d2642
D
Docs
项目概览
OpenHarmony
/
Docs
大约 1 年 前同步成功
通知
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,发现更多精彩内容 >>
提交
da3d2642
编写于
2月 14, 2023
作者:
J
jiao_yanlin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
File acquisition method modification
Signed-off-by:
N
jiao_yanlin
<
jiaoyanlin@huawei.com
>
上级
88b46404
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
112 addition
and
108 deletion
+112
-108
zh-cn/application-dev/media/audio-renderer.md
zh-cn/application-dev/media/audio-renderer.md
+76
-86
zh-cn/application-dev/reference/apis/js-apis-audio.md
zh-cn/application-dev/reference/apis/js-apis-audio.md
+36
-22
未找到文件。
zh-cn/application-dev/media/audio-renderer.md
浏览文件 @
da3d2642
...
...
@@ -84,7 +84,7 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可
将需要播放的音频数据读入缓冲区,重复调用write()方法写入。
```
js
import
f
ileio
from
'
@ohos.fileio
'
;
import
f
s
from
'
@ohos.file.fs
'
;
import
audio
from
'
@ohos.multimedia.audio
'
;
async
function
writeBuffer
(
buf
)
{
...
...
@@ -103,35 +103,33 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可
// 此处是渲染器的合理的最小缓冲区大小(也可以选择其它大小的缓冲区)
const
bufferSize
=
await
audioRenderer
.
getBufferSize
();
let
dir
=
globalThis
.
fileDir
;
//不可直接访问,没权限,切记!!!一定要使用沙箱路径
const
path
=
dir
+
'
/file_example_WAV_2MG.wav
'
;
// 需要渲染的音乐文件 实际路径为:/data/storage/el2/base/haps/entry/files/file_example_WAV_2MG.wav
console
.
info
(
`file path:
${
path
}
`
);
let
ss
=
fileio
.
createStreamSync
(
path
,
'
r
'
);
const
totalSize
=
fileio
.
statSync
(
path
).
size
;
// 音乐文件大小
let
discardHeader
=
new
ArrayBuffer
(
bufferSize
);
ss
.
readSync
(
discardHeader
);
let
rlen
=
0
;
rlen
+=
bufferSize
;
let
id
=
setInterval
(()
=>
{
if
(
audioRenderer
.
state
==
audio
.
AudioState
.
STATE_RELEASED
)
{
// 如果渲染器状态为release,停止渲染
ss
.
closeSync
();
await
audioRenderer
.
stop
();
clearInterval
(
id
);
}
if
(
audioRenderer
.
state
==
audio
.
AudioState
.
STATE_RUNNING
)
{
if
(
rlen
>=
totalSize
)
{
// 如果音频文件已经被读取完,停止渲染
ss
.
closeSync
();
await
audioRenderer
.
stop
();
clearInterval
(
id
);
}
let
buf
=
new
ArrayBuffer
(
bufferSize
);
rlen
+=
ss
.
readSync
(
buf
);
console
.
info
(
`Total bytes read from file:
${
rlen
}
`
);
writeBuffer
(
buf
);
}
else
{
console
.
info
(
'
check after next interval
'
);
const
filePath
=
dir
+
'
/file_example_WAV_2MG.wav
'
;
// 需要渲染的音乐文件 实际路径为:/data/storage/el2/base/haps/entry/files/file_example_WAV_2MG.wav
console
.
info
(
`file filePath:
${
filePath
}
`
);
let
file
=
fs
.
openSync
(
filePath
,
fs
.
OpenMode
.
READ_ONLY
);
let
stat
=
await
fs
.
stat
(
filePath
);
//音乐文件信息
let
buf
=
new
ArrayBuffer
(
bufferSize
);
let
len
=
stat
.
size
%
this
.
bufferSize
==
0
?
Math
.
floor
(
stat
.
size
/
this
.
bufferSize
)
:
Math
.
floor
(
stat
.
size
/
this
.
bufferSize
+
1
);
for
(
let
i
=
0
;
i
<
len
;
i
++
)
{
let
options
=
{
offset
:
i
*
this
.
bufferSize
,
length
:
this
.
bufferSize
}
},
30
);
// 定时器区间根据音频格式设置,单位为毫秒
let
readsize
=
await
fs
.
read
(
file
.
fd
,
buf
,
options
)
let
writeSize
=
await
new
Promise
((
resolve
,
reject
)
=>
{
this
.
audioRenderer
.
write
(
buf
,(
err
,
writeSize
)
=>
{
if
(
err
){
reject
(
err
)
}
else
{
resolve
(
writeSize
)
}
})
})
}
fs
.
close
(
file
)
await
audioRenderer
.
stop
();
//停止渲染
await
audioRenderer
.
release
();
//释放资源
```
4.
(可选)调用pause()方法或stop()方法暂停/停止渲染音频数据。
...
...
@@ -416,35 +414,31 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可
let
dir
=
globalThis
.
fileDir
;
//不可直接访问,没权限,切记!!!一定要使用沙箱路径
const
path1
=
dir
+
'
/music001_48000_32_1.wav
'
;
// 需要渲染的音乐文件 实际路径为:/data/storage/el2/base/haps/entry/files/music001_48000_32_1.wav
console
.
info
(
`audioRender1 file path:
${
path1
}
`
);
let
ss1
=
await
fileio
.
createStream
(
path1
,
'
r
'
);
const
totalSize1
=
fileio
.
statSync
(
path1
).
size
;
// 音乐文件大小
console
.
info
(
`totalSize1 -------:
${
totalSize1
}
`
);
let
discardHeader
=
new
ArrayBuffer
(
bufferSize
);
ss1
.
readSync
(
discardHeader
);
let
rlen
=
0
;
rlen
+=
bufferSize
;
//2.7 通过audioRender对缓存区的原始音频数据进行渲染
let
id
=
setInterval
(
async
()
=>
{
if
(
audioRenderer1
.
state
==
audio
.
AudioState
.
STATE_RELEASED
)
{
// 如果渲染器状态为release,停止渲染
ss1
.
closeSync
();
audioRenderer1
.
stop
();
clearInterval
(
id
);
}
if
(
audioRenderer1
.
state
==
audio
.
AudioState
.
STATE_RUNNING
)
{
if
(
rlen
>=
totalSize1
)
{
// 如果音频文件已经被读取完,停止渲染
ss1
.
closeSync
();
await
audioRenderer1
.
stop
();
clearInterval
(
id
);
}
let
buf
=
new
ArrayBuffer
(
bufferSize
);
rlen
+=
ss1
.
readSync
(
buf
);
console
.
info
(
`Total bytes read from file:
${
rlen
}
`
);
await
writeBuffer
(
buf
,
that
.
audioRenderer1
);
}
else
{
console
.
info
(
'
check after next interval
'
);
let
file1
=
fs
.
openSync
(
path1
,
fs
.
OpenMode
.
READ_ONLY
);
let
stat
=
await
fs
.
stat
(
path1
);
//音乐文件信息
let
buf
=
new
ArrayBuffer
(
bufferSize
);
let
len
=
stat
.
size
%
this
.
bufferSize
==
0
?
Math
.
floor
(
stat
.
size
/
this
.
bufferSize
)
:
Math
.
floor
(
stat
.
size
/
this
.
bufferSize
+
1
);
//1.7 通过audioRender对缓存区的原始音频数据进行渲染
for
(
let
i
=
0
;
i
<
len
;
i
++
)
{
let
options
=
{
offset
:
i
*
this
.
bufferSize
,
length
:
this
.
bufferSize
}
},
30
);
// 定时器区间根据音频格式设置,单位为毫秒
let
readsize
=
await
fs
.
read
(
file
.
fd
,
buf
,
options
)
let
writeSize
=
await
new
Promise
((
resolve
,
reject
)
=>
{
this
.
audioRenderer1
.
write
(
buf
,(
err
,
writeSize
)
=>
{
if
(
err
){
reject
(
err
)
}
else
{
resolve
(
writeSize
)
}
})
})
}
fs
.
close
(
file1
)
await
audioRenderer1
.
stop
();
//停止渲染
await
audioRenderer1
.
release
();
//释放资源
}
async
runningAudioRender2
(){
...
...
@@ -488,39 +482,35 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可
const
bufferSize
=
await
audioRenderer2
.
getBufferSize
();
console
.
info
(
`audio bufferSize:
${
bufferSize
}
`
);
//2.6
读
取原始音频数据文件
//2.6
获
取原始音频数据文件
let
dir
=
globalThis
.
fileDir
;
//不可直接访问,没权限,切记!!!一定要使用沙箱路径
const
path2
=
dir
+
'
/music002_48000_32_1.wav
'
;
// 需要渲染的音乐文件 实际路径为:/data/storage/el2/base/haps/entry/files/music002_48000_32_1.wav
console
.
error
(
`audioRender1 file path:
${
path2
}
`
);
let
ss2
=
await
fileio
.
createStream
(
path2
,
'
r
'
);
const
totalSize2
=
fileio
.
statSync
(
path2
).
size
;
// 音乐文件大小
console
.
error
(
`totalSize2 -------:
${
totalSize2
}
`
);
let
discardHeader2
=
new
ArrayBuffer
(
bufferSize
);
ss2
.
readSync
(
discardHeader2
);
let
rlen
=
0
;
rlen
+=
bufferSize
;
console
.
info
(
`audioRender2 file path:
${
path2
}
`
);
let
file2
=
fs
.
openSync
(
path2
,
fs
.
OpenMode
.
READ_ONLY
);
let
stat
=
await
fs
.
stat
(
path2
);
//音乐文件信息
let
buf
=
new
ArrayBuffer
(
bufferSize
);
let
len
=
stat
.
size
%
this
.
bufferSize
==
0
?
Math
.
floor
(
stat
.
size
/
this
.
bufferSize
)
:
Math
.
floor
(
stat
.
size
/
this
.
bufferSize
+
1
);
//2.7 通过audioRender对缓存区的原始音频数据进行渲染
let
id
=
setInterval
(
async
()
=>
{
if
(
audioRenderer2
.
state
==
audio
.
AudioState
.
STATE_RELEASED
)
{
// 如果渲染器状态为release,停止渲染
ss2
.
closeSync
();
that
.
audioRenderer2
.
stop
();
clearInterval
(
id
);
for
(
let
i
=
0
;
i
<
len
;
i
++
)
{
let
options
=
{
offset
:
i
*
this
.
bufferSize
,
length
:
this
.
bufferSize
}
if
(
audioRenderer1
.
state
==
audio
.
AudioState
.
STATE_RUNNING
)
{
if
(
rlen
>=
totalSize2
)
{
// 如果音频文件已经被读取完,停止渲染
ss2
.
closeSync
();
await
audioRenderer2
.
stop
();
clearInterval
(
id
);
}
let
buf
=
new
ArrayBuffer
(
bufferSize
);
rlen
+=
ss2
.
readSync
(
buf
);
console
.
info
(
`Total bytes read from file:
${
rlen
}
`
);
await
writeBuffer
(
buf
,
that
.
audioRenderer2
);
}
else
{
console
.
info
(
'
check after next interval
'
);
}
},
30
);
// 定时器区间根据音频格式设置,单位为毫秒
let
readsize
=
await
fs
.
read
(
file
.
fd
,
buf
,
options
)
let
writeSize
=
await
new
Promise
((
resolve
,
reject
)
=>
{
this
.
audioRenderer2
.
write
(
buf
,(
err
,
writeSize
)
=>
{
if
(
err
){
reject
(
err
)
}
else
{
resolve
(
writeSize
)
}
})
})
}
fs
.
close
(
file2
)
await
audioRenderer2
.
stop
();
//停止渲染
await
audioRenderer2
.
release
();
//释放资源
}
async
writeBuffer
(
buf
,
audioRender
)
{
...
...
zh-cn/application-dev/reference/apis/js-apis-audio.md
浏览文件 @
da3d2642
...
...
@@ -74,7 +74,7 @@ createAudioRenderer(options: AudioRendererOptions, callback: AsyncCallback\<Audi
```
js
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
import
f
ileio
from
'
@ohos.fileio
'
;
import
f
s
from
'
@ohos.file.fs
'
;
import
audio
from
'
@ohos.multimedia.audio
'
;
let
audioStreamInfo
=
{
...
...
@@ -129,7 +129,7 @@ createAudioRenderer(options: AudioRendererOptions): Promise<AudioRenderer\>
```
js
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
import
f
ileio
from
'
@ohos.fileio
'
;
import
f
s
from
'
@ohos.file.fs
'
;
import
audio
from
'
@ohos.multimedia.audio
'
;
let
audioStreamInfo
=
{
...
...
@@ -4530,16 +4530,26 @@ async function getCacheDir(){
path
=
await
context
.
getCacheDir
();
}
let
filePath
=
path
+
'
/StarWars10s-2C-48000-4SW.wav
'
;
let
ss
=
fileio
.
createStreamSync
(
filePath
,
'
r
'
);
let
file
=
fs
.
openSync
(
filePath
,
fs
.
OpenMode
.
READ_ONLY
);
let
stat
=
await
fs
.
stat
(
path
);
let
buf
=
new
ArrayBuffer
(
bufferSize
);
ss
.
readSync
(
buf
);
audioRenderer
.
write
(
buf
,
(
err
,
writtenbytes
)
=>
{
if
(
writtenbytes
<
0
)
{
console
.
error
(
'
write failed.
'
);
}
else
{
console
.
info
(
`Actual written bytes:
${
writtenbytes
}
`
);
}
});
let
len
=
stat
.
size
%
this
.
bufferSize
==
0
?
Math
.
floor
(
stat
.
size
/
this
.
bufferSize
)
:
Math
.
floor
(
stat
.
size
/
this
.
bufferSize
+
1
);
for
(
let
i
=
0
;
i
<
len
;
i
++
)
{
let
options
=
{
offset
:
i
*
this
.
bufferSize
,
length
:
this
.
bufferSize
}
let
readsize
=
await
fs
.
read
(
file
.
fd
,
buf
,
options
)
let
writeSize
=
await
new
Promise
((
resolve
,
reject
)
=>
{
this
.
audioRenderer
.
write
(
buf
,(
err
,
writeSize
)
=>
{
if
(
err
){
reject
(
err
)
}
else
{
resolve
(
writeSize
)
}
})
})
}
```
### write<sup>8+</sup>
...
...
@@ -4573,18 +4583,22 @@ async function getCacheDir(){
path
=
await
context
.
getCacheDir
();
}
let
filePath
=
path
+
'
/StarWars10s-2C-48000-4SW.wav
'
;
let
ss
=
fileio
.
createStreamSync
(
filePath
,
'
r
'
);
let
file
=
fs
.
openSync
(
filePath
,
fs
.
OpenMode
.
READ_ONLY
);
let
stat
=
await
fs
.
stat
(
path
);
let
buf
=
new
ArrayBuffer
(
bufferSize
);
ss
.
readSync
(
buf
);
audioRenderer
.
write
(
buf
).
then
((
writtenbytes
)
=>
{
if
(
writtenbytes
<
0
)
{
console
.
error
(
'
write failed.
'
);
}
else
{
console
.
info
(
`Actual written bytes:
${
writtenbytes
}
`
);
}
}).
catch
((
err
)
=>
{
console
.
error
(
`ERROR:
${
err
}
`
);
});
let
len
=
stat
.
size
%
this
.
bufferSize
==
0
?
Math
.
floor
(
stat
.
size
/
this
.
bufferSize
)
:
Math
.
floor
(
stat
.
size
/
this
.
bufferSize
+
1
);
for
(
let
i
=
0
;
i
<
len
;
i
++
)
{
let
options
=
{
offset
:
i
*
this
.
bufferSize
,
length
:
this
.
bufferSize
}
let
readsize
=
await
fs
.
read
(
file
.
fd
,
buf
,
options
)
try
{
let
writeSize
=
await
this
.
audioRenderer
.
write
(
buf
);
}
catch
(
err
)
{
console
.
error
(
`audioRenderer.write err:
${
err
}
`
);
}
}
```
### getAudioTime<sup>8+</sup>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录