鸿蒙开源组件——lingorecorder

jacksky
发布于 2021-12-24 17:03
浏览
0收藏

lingorecorder

本项目是基于开源项目lingochamp/LingoRecorder进行适用OHOS的移植,可以通过项目标签以及 github地址https://github.com/lingochamp/LingoRecorder 追踪到原项目

移植版本:Branches/master

1. 项目介绍

项目名称:lingorecorder

所属系列:OHOS的第三方组件适配移植

功能:

  1. 多线程机制保证处理能力和 PCM 数据的完整性;
  2. 抽象出 AudioProcessor 来注入 Recorder 中以支持录音和处理的分离;
  3. 提供 WavFileRecorder 以支持以文件来替代录音器生成录音数据;
  4. 提供idl接口方便在另一个进程中处理录音数据。

项目移植状态:

  1. 主要功能已经移植;
  2. demo中调用默认播放器播放wav文件功能暂不可用,非主要功能;

原项目Doc地址:https://github.com/lingochamp/LingoRecorder

编程语言:java

2. 集成指引

方式一:

  1. 下载或自行编译生成lingo_recorder-for-ohos的.har文件,文件路径为:./entry/libs/lingo_recorder-debug.har。
  2. 自行编译时,需要注意要自行添加签名。
  3. 导入你的OHOS项目模块的**./libs**中。
  4. 在模块下的build.gradle中确认依赖**./libs**下的.har包,implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
  5. 在代码中使用。

方式二:

  1. 在根目录的build.gradle文件中添加mavenCentral()

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    ...
    buildscript {
        repositories {
            ...
            mavenCentral()
        }
        ...
    }
    
    allprojects {
        repositories {
            ...
            mavenCentral()
        }
    }
  2. 在module目录下的build.gradle文件中添加``
...
dependencies {
    ...
    implementation 'com.gitee.ts_ohos:silicompressor-for-ohos:1.0.1'
}

3. 使用说明

* 参考原项目说明文档 LingoRecorder的使用

lingoRecorder = new LingoRecorder();
lingoRecorder.setOnRecordStopListener(new LingoRecorder.OnRecordStopListener() {
    @Override
    public void onRecordStop(Throwable throwable, Result result) {
        //any execeptions occur during recording will be received at here.
        //you can get duration and output file from Result.
    }
});
lingoRecorder.setOnProcessStopListener(new LingoRecorder.OnProcessStopListener() {
    @Override
    public void onProcessStop(Throwable throwable, Map<String, AudioProcessor> map) {
        //any execeptions occur during processing will be received at here.
        //you can get any processors you inject into recorder.
        //the callback will be invoked after "onRecordStop".
    }
});

自定义AudioProcessor的使用 可以实现 AudioProcessor 来自定义自己的处理器:

public interface AudioProcessor {

    void start() throws Exception;

    void flow(byte[] bytes, int size) throws Exception;

    boolean needExit();

    void end() throws Exception;

    void release();
}

将AudioProcessor运行在一个独立的进程 LingoRecorder 提供了 aidl 接口以支持在一个独立的进程中运行 AudioProcessor。示例中自定义了一个 LocalScorerProcessor 运行在 "score" 进程中。

Flac Encoder 示例中也演示了一个使用 Codec 进行 Flac 编码的 AudioProcessor。此示例是为了向有硬编码需求的用户提供一个样例。

计算/监听音量 设置音量监听器:

//只设置 OnVolumeListener 的时候,计算音量的方式使用的是一个默认的内部实现
//内部默认实现的返回值是[0, 90]的分贝值
lingoRecorder.setOnVolumeListener(new OnVolumeListener() {
	@Override
	public void onVolume(double volume) {

	}
});
//也可以提供自己的计算音量的实现
lingoRecorder.setOnVolumeListener(new OnVolumeListener() {
	@Override
	public void onVolume(double volume) {

	}
}, new IVolumeCalculator() {
	@Override
	public double onAudioChunk(byte[] chunk, int size, int bitsPerSample) {
		return 0;
	}
});

4. 效果演示鸿蒙开源组件——lingorecorder-鸿蒙开发者社区

 

音量效果:监听音量变化,并且绘制曲线

其他效果:请参考demo实例,请参考/data/data/com.liulishuo.engzo.lingorecorderforohos.demo/outputs/路径中的录音文件。

5. 版本迭代

  • v1.0.0 项目初次提交与发布。
  • v1.0.1 更新OHOS API到5。

6. 版本和许可信息

  • Apache License 2.0
  • https://gitee.com/ts_ohos/lingorecorder-for-ohos/blob/ohos_main/LICENSE.txt
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
       http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

 

 

lingorecorder-for-ohos-ohos_main.zip 943.53K 6次下载
已于2021-12-24 17:03:47修改
收藏
回复
举报
回复
    相关推荐