提交 cce475d5 编写于 作者: J jiao_yanlin

File acquisition method modification

Signed-off-by: Njiao_yanlin <jiaoyanlin@huawei.com>
上级 2862065a
...@@ -84,7 +84,7 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可 ...@@ -84,7 +84,7 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可
将需要播放的音频数据读入缓冲区,重复调用write()方法写入。 将需要播放的音频数据读入缓冲区,重复调用write()方法写入。
```js ```js
import fileio from '@ohos.fileio'; import fs from '@ohos.file.fs';
import audio from '@ohos.multimedia.audio'; import audio from '@ohos.multimedia.audio';
async function writeBuffer(buf) { async function writeBuffer(buf) {
...@@ -103,35 +103,40 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可 ...@@ -103,35 +103,40 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可
// 此处是渲染器的合理的最小缓冲区大小(也可以选择其它大小的缓冲区) // 此处是渲染器的合理的最小缓冲区大小(也可以选择其它大小的缓冲区)
const bufferSize = await audioRenderer.getBufferSize(); const bufferSize = await audioRenderer.getBufferSize();
let dir = globalThis.fileDir; //不可直接访问,没权限,切记!!!一定要使用沙箱路径 let dir = globalThis.fileDir; //不可直接访问,没权限,切记!!!一定要使用沙箱路径
const path = dir + '/file_example_WAV_2MG.wav'; // 需要渲染的音乐文件 实际路径为:/data/storage/el2/base/haps/entry/files/file_example_WAV_2MG.wav const filePath = dir + '/file_example_WAV_2MG.wav'; // 需要渲染的音乐文件 实际路径为:/data/storage/el2/base/haps/entry/files/file_example_WAV_2MG.wav
console.info(`file path: ${ path}`); console.info(`file filePath: ${ filePath}`);
let ss = fileio.createStreamSync(path, 'r');
const totalSize = fileio.statSync(path).size; // 音乐文件大小 let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY);
let discardHeader = new ArrayBuffer(bufferSize); let stat = await fs.stat(filePath); //音乐文件信息
ss.readSync(discardHeader); let buf = new ArrayBuffer(bufferSize);
let rlen = 0; let len = stat.size % this.bufferSize == 0 ? Math.floor(stat.size / this.bufferSize) : Math.floor(stat.size / this.bufferSize + 1);
rlen += bufferSize; for (let i = 0;i < len; i++) {
let options = {
let id = setInterval(() => { 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)
}
})
})
if (audioRenderer.state == audio.AudioState.STATE_RELEASED) { // 如果渲染器状态为release,停止渲染 if (audioRenderer.state == audio.AudioState.STATE_RELEASED) { // 如果渲染器状态为release,停止渲染
ss.closeSync(); fs.close(file)
await audioRenderer.stop(); await audioRenderer.stop();
clearInterval(id);
} }
if (audioRenderer.state == audio.AudioState.STATE_RUNNING) { if (audioRenderer.state == audio.AudioState.STATE_RUNNING) {
if (rlen >= totalSize) { // 如果音频文件已经被读取完,停止渲染 if (i == len - 1) { // 如果音频文件已经被读取完,停止渲染
ss.closeSync(); fs.close(file)
await audioRenderer.stop(); 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');
} }
}, 30); // 定时器区间根据音频格式设置,单位为毫秒 }
``` ```
4. (可选)调用pause()方法或stop()方法暂停/停止渲染音频数据。 4. (可选)调用pause()方法或stop()方法暂停/停止渲染音频数据。
...@@ -186,7 +191,6 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可 ...@@ -186,7 +191,6 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可
} }
await audioRenderer.drain(); await audioRenderer.drain();
state = audioRenderer.state; state = audioRenderer.state;
} }
``` ```
...@@ -203,7 +207,6 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可 ...@@ -203,7 +207,6 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可
console.info('Renderer already released'); console.info('Renderer already released');
return; return;
} }
await audioRenderer.release(); await audioRenderer.release();
state = audioRenderer.state; state = audioRenderer.state;
...@@ -416,35 +419,38 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可 ...@@ -416,35 +419,38 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可
let dir = globalThis.fileDir; //不可直接访问,没权限,切记!!!一定要使用沙箱路径 let dir = globalThis.fileDir; //不可直接访问,没权限,切记!!!一定要使用沙箱路径
const path1 = dir + '/music001_48000_32_1.wav'; // 需要渲染的音乐文件 实际路径为:/data/storage/el2/base/haps/entry/files/music001_48000_32_1.wav 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}`); console.info(`audioRender1 file path: ${ path1}`);
let ss1 = await fileio.createStream(path1,'r'); let file1 = fs.openSync(path1, fs.OpenMode.READ_ONLY);
const totalSize1 = fileio.statSync(path1).size; // 音乐文件大小 let stat = await fs.stat(path1); //音乐文件信息
console.info(`totalSize1 -------: ${totalSize1}`); let buf = new ArrayBuffer(bufferSize);
let discardHeader = new ArrayBuffer(bufferSize); let len = stat.size % this.bufferSize == 0 ? Math.floor(stat.size / this.bufferSize) : Math.floor(stat.size / this.bufferSize + 1);
ss1.readSync(discardHeader);
let rlen = 0; //1.7 通过audioRender对缓存区的原始音频数据进行渲染
rlen += bufferSize; for (let i = 0;i < len; i++) {
let options = {
//2.7 通过audioRender对缓存区的原始音频数据进行渲染 offset: i * this.bufferSize,
let id = setInterval(async () => { length: this.bufferSize
}
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)
}
})
})
if (audioRenderer1.state == audio.AudioState.STATE_RELEASED) { // 如果渲染器状态为release,停止渲染 if (audioRenderer1.state == audio.AudioState.STATE_RELEASED) { // 如果渲染器状态为release,停止渲染
ss1.closeSync(); fs.close(file1)
audioRenderer1.stop(); await audioRenderer1.stop();
clearInterval(id);
} }
if (audioRenderer1.state == audio.AudioState.STATE_RUNNING) { if (audioRenderer1.state == audio.AudioState.STATE_RUNNING) {
if (rlen >= totalSize1) { // 如果音频文件已经被读取完,停止渲染 if (i == len - 1) { // 如果音频文件已经被读取完,停止渲染
ss1.closeSync(); fs.close(file1)
await audioRenderer1.stop(); 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');
} }
}, 30); // 定时器区间根据音频格式设置,单位为毫秒 }
} }
async runningAudioRender2(){ async runningAudioRender2(){
...@@ -488,39 +494,42 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可 ...@@ -488,39 +494,42 @@ AudioRenderer提供了渲染音频文件和控制播放的接口,开发者可
const bufferSize = await audioRenderer2.getBufferSize(); const bufferSize = await audioRenderer2.getBufferSize();
console.info(`audio bufferSize: ${bufferSize}`); console.info(`audio bufferSize: ${bufferSize}`);
//2.6 取原始音频数据文件 //2.6 取原始音频数据文件
let dir = globalThis.fileDir; //不可直接访问,没权限,切记!!!一定要使用沙箱路径 let dir = globalThis.fileDir; //不可直接访问,没权限,切记!!!一定要使用沙箱路径
const path2 = dir + '/music002_48000_32_1.wav'; // 需要渲染的音乐文件 实际路径为:/data/storage/el2/base/haps/entry/files/music002_48000_32_1.wav 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}`); console.info(`audioRender2 file path: ${ path2}`);
let ss2 = await fileio.createStream(path2,'r'); let file2 = fs.openSync(path2, fs.OpenMode.READ_ONLY);
const totalSize2 = fileio.statSync(path2).size; // 音乐文件大小 let stat = await fs.stat(path2); //音乐文件信息
console.error(`totalSize2 -------: ${totalSize2}`); let buf = new ArrayBuffer(bufferSize);
let discardHeader2 = new ArrayBuffer(bufferSize); let len = stat.size % this.bufferSize == 0 ? Math.floor(stat.size / this.bufferSize) : Math.floor(stat.size / this.bufferSize + 1);
ss2.readSync(discardHeader2);
let rlen = 0;
rlen += bufferSize;
//2.7 通过audioRender对缓存区的原始音频数据进行渲染 //2.7 通过audioRender对缓存区的原始音频数据进行渲染
let id = setInterval(async () => { 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.audioRenderer2.write(buf,(err,writeSize)=>{
if(err){
reject(err)
}else{
resolve(writeSize)
}
})
})
if (audioRenderer2.state == audio.AudioState.STATE_RELEASED) { // 如果渲染器状态为release,停止渲染 if (audioRenderer2.state == audio.AudioState.STATE_RELEASED) { // 如果渲染器状态为release,停止渲染
ss2.closeSync(); fs.close(file2)
that.audioRenderer2.stop(); await audioRenderer2.stop();
clearInterval(id);
} }
if (audioRenderer1.state == audio.AudioState.STATE_RUNNING) { if (audioRenderer2.state == audio.AudioState.STATE_RUNNING) {
if (rlen >= totalSize2) { // 如果音频文件已经被读取完,停止渲染 if (i == len - 1) { // 如果音频文件已经被读取完,停止渲染
ss2.closeSync(); fs.close(file2)
await audioRenderer2.stop(); await audioRenderer1.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); // 定时器区间根据音频格式设置,单位为毫秒 }
} }
async writeBuffer(buf, audioRender) { async writeBuffer(buf, audioRender) {
......
...@@ -4536,16 +4536,27 @@ async function getCacheDir(){ ...@@ -4536,16 +4536,27 @@ async function getCacheDir(){
path = await context.getCacheDir(); path = await context.getCacheDir();
} }
let filePath = path + '/StarWars10s-2C-48000-4SW.wav'; let filePath = path + '/StarWars10s-2C-48000-4SW.wav';
let ss = fs.openSync(filePath, fs.OpenMode.READ_ONLY); let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY);
let stat = await fs.stat(path);
let buf = new ArrayBuffer(bufferSize); let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf); let len = stat.size % this.bufferSize == 0 ? Math.floor(stat.size / this.bufferSize) : Math.floor(stat.size / this.bufferSize + 1);
audioRenderer.write(buf, (err, writtenbytes) => { for (let i = 0;i < len; i++) {
if (writtenbytes < 0) { let options = {
console.error('write failed.'); offset: i * this.bufferSize,
} else { length: this.bufferSize
console.info(`Actual written bytes: ${writtenbytes}`); }
} 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> ### write<sup>8+</sup>
...@@ -4579,18 +4590,22 @@ async function getCacheDir(){ ...@@ -4579,18 +4590,22 @@ async function getCacheDir(){
path = await context.getCacheDir(); path = await context.getCacheDir();
} }
let filePath = path + '/StarWars10s-2C-48000-4SW.wav'; let filePath = path + '/StarWars10s-2C-48000-4SW.wav';
let ss = fs.openSync(filePath, fs.OpenMode.READ_ONLY); let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY);
let stat = await fs.stat(path);
let buf = new ArrayBuffer(bufferSize); let buf = new ArrayBuffer(bufferSize);
ss.readSync(buf); let len = stat.size % this.bufferSize == 0 ? Math.floor(stat.size / this.bufferSize) : Math.floor(stat.size / this.bufferSize + 1);
audioRenderer.write(buf).then((writtenbytes) => { for (let i = 0;i < len; i++) {
if (writtenbytes < 0) { let options = {
console.error('write failed.'); offset: i * this.bufferSize,
} else { length: this.bufferSize
console.info(`Actual written bytes: ${writtenbytes}`); }
} let readsize = await fs.read(file.fd, buf, options)
}).catch((err) => { try{
console.error(`ERROR: ${err}`); let writeSize = await this.audioRenderer.write(buf);
}); } catch(err) {
console.error(`audioRenderer.write err: ${err}`);
}
}
``` ```
### getAudioTime<sup>8+</sup> ### getAudioTime<sup>8+</sup>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册