鸿蒙开源组件——录音机

jacksky
发布于 2021-12-29 16:31
浏览
0收藏

OhosAudioRecorder

项目介绍

  • 项目名称:OhosAudioRecorder
  • 所属系列:openharmony的第三方组件适配移植
  • 功能:主要实现录音功能、暂停,播放。根据声音大小振幅有水波纹冒泡效果。
  • 项目移植状态:主功能完成
  • 调用差异:无
  • 开发版本:sdk6,DevEco Studio 2.2 Beta1
  • 基线版本:Release 0.3.0

效果演示鸿蒙开源组件——录音机-鸿蒙开发者社区

安装教程

1.在moudle级别下的build.gradle文件中添加依赖

// 添加maven仓库

repositories {
    maven {
        url 'https://s01.oss.sonatype.org/content/repositories/release/'
    }
}

// 添加依赖库

dependencies {
    implementation 'com.gitee.chinasoft_ohos:audiorecorderlibrary:1.0.0'
}

在sdk6,DevEco Studio 2.2 Beta1 下项目可直接运行 如无法运行,删除项目.gradle,.idea,build,gradle,build.gradle文件, 并依据自己的版本创建新项目,将新项目的对应文件复制到根目录下

使用说明

实现效果:录音功能、暂停,播放。根据声音大小振幅,水波纹冒泡效果。
在entry的build.gradle 引入
  dependencies { 
  implementation 'com.gitee.chinasoft_ohos:audiorecorderlibrary:1.0.0' 
  }
在audiorecorderlibrary库 build.gradle引入
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
    testImplementation 'junit:junit:4.13'
    implementation('com.gitee.chinasoft_ohos:likestarlibrary:1.0.0')
    implementation('com.gitee.chinasoft_ohos:recorder_view:1.0.0')
    implementation('com.gitee.chinasoft_ohos:om_recorder:1.0.0')
}

现在项目重写DependentLayout包含的控件如下定义:

XML中添加entry的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    xmlns:app="http://schemas.huawei.com/apk/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="vertical">

    <Text
        ohos:id="$+id:text_helloworld"
        ohos:height="56vp"
        ohos:width="match_parent"
        ohos:background_element="$color:colorPrimaryDark"
        ohos:text="$string:app_name"
        ohos:left_padding="20vp"
        ohos:text_color="#ffffff"
        ohos:text_size="18fp"/>
    <DirectionalLayout
        ohos:height="match_parent"
        ohos:width="match_parent"
        ohos:alignment="center"
        ohos:background_element="$color:colorPrimary">

        <Button
            ohos:id="$+id:btn_record"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:padding="20vp"
            ohos:text="RECORD AUDIO"
            ohos:text_color="#ffffff"
            ohos:text_size="25fp"/>
    </DirectionalLayout>

</DirectionalLayout>

点击RECORD AUDIO 按钮跳转到AudioRecorderAbility界面,布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<DependentLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    xmlns:app="http://schemas.huawei.com/apk/res/ohos"
    ohos:id="$+id:content"
    ohos:height="match_parent"
    ohos:width="match_parent">
    <com.scwang.wave.MultiWaveHeader
        ohos:id="$+id:multiWaveHeader"
        ohos:height="210vp"
        ohos:width="match_parent"
        ohos:align_parent_bottom="true"
        />
   <DependentLayout
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:align_parent_bottom="true"
        ohos:bottom_margin="160vp">

        <com.example.likestarlibrary.TCHeartLayout
            ohos:id="$+id:heart_layout"
            ohos:height="200vp"
            ohos:width="40vp"
            app:anim_duration="2000"
            app:heart_anim_bezier_factor="6"
            app:heart_anim_length="50vp"
            app:heart_anim_length_rand="20vp"
            />
        <com.example.likestarlibrary.TCHeartLayout
            ohos:id="$+id:heart_layout1"
            ohos:height="200vp"
            ohos:width="40vp"
            app:anim_duration="2000"
            app:heart_anim_bezier_factor="60"
            app:heart_anim_length="50vp"
            app:heart_anim_length_rand="20vp"
            ohos:right_of="$id:heart_layout"/>
        <com.example.likestarlibrary.TCHeartLayout
            ohos:id="$+id:heart_layout2"
            ohos:height="200vp"
            ohos:width="40vp"
            app:anim_duration="2000"
            app:heart_anim_bezier_factor="60"
            app:heart_anim_length="50vp"
            app:heart_anim_length_rand="10vp"
            ohos:right_of="$id:heart_layout1"/>
        <com.example.likestarlibrary.TCHeartLayout
            ohos:id="$+id:heart_layout3"
            ohos:height="200vp"
            ohos:width="40vp"
            app:anim_duration="2000"
            app:heart_anim_bezier_factor="60"
            app:heart_anim_length="50vp"
            app:heart_anim_length_rand="10vp"
            ohos:right_of="$id:heart_layout2"
            />
        <com.example.likestarlibrary.TCHeartLayout
            ohos:id="$+id:heart_layout4"
            ohos:height="200vp"
            ohos:width="40vp"
            app:anim_duration="2000"
            app:heart_anim_bezier_factor="60"
            app:heart_anim_length="50vp"
            app:heart_anim_length_rand="10vp"
            ohos:right_of="$id:heart_layout3"
            ohos:left_margin="50vp"
            />
        <com.example.likestarlibrary.TCHeartLayout
            ohos:id="$+id:heart_layout5"
            ohos:height="200vp"
            ohos:width="40vp"
            app:anim_duration="2000"
            app:heart_anim_bezier_factor="60"
            app:heart_anim_length="50vp"
            app:heart_anim_length_rand="10vp"
            ohos:right_of="$id:heart_layout4"
            ohos:left_margin="50vp"
            />
        <com.example.likestarlibrary.TCHeartLayout
            ohos:id="$+id:heart_layout6"
            ohos:height="200vp"
            ohos:width="40vp"
            app:anim_duration="2000"
            app:heart_anim_bezier_factor="60"
            app:heart_anim_length="50vp"
            app:heart_anim_length_rand="10vp"
            ohos:right_of="$id:heart_layout5"
            ohos:left_margin="50vp"
            />
        <com.example.likestarlibrary.TCHeartLayout
            ohos:id="$+id:heart_layout7"
            ohos:height="200vp"
            ohos:width="40vp"
            app:anim_duration="2000"
            app:heart_anim_bezier_factor="60"
            app:heart_anim_length="50vp"
            app:heart_anim_length_rand="10vp"
            ohos:right_of="$id:heart_layout6"
            ohos:left_margin="50vp"
            />
        <com.example.likestarlibrary.TCHeartLayout
            ohos:id="$+id:heart_layout8"
            ohos:height="200vp"
            ohos:width="40vp"
            app:anim_duration="2000"
            app:heart_anim_bezier_factor="60"
            app:heart_anim_length="50vp"
            app:heart_anim_length_rand="10vp"
            ohos:right_of="$id:heart_layout7"
            ohos:left_margin="50vp"
            />
    </DependentLayout>
    <DependentLayout
        ohos:id="$+id:top"
        ohos:height="56vp"
        ohos:width="match_parent"
        ohos:orientation="horizontal">

        <Image
            ohos:id="$+id:image_clear"
            ohos:height="match_parent"
            ohos:width="match_content"
            ohos:left_padding="30vp"
            ohos:right_padding="30vp"
            ohos:image_src="$media:aar_ic_clear"/>

        <Image
            ohos:id="$+id:image_check"
            ohos:height="match_parent"
            ohos:width="match_content"
            ohos:left_padding="30vp"
            ohos:right_padding="30vp"
            ohos:image_src="$media:aar_ic_check"
            ohos:align_parent_right="true"
            ohos:visibility="invisible"/>

    </DependentLayout>
    <DirectionalLayout
        ohos:id="$+id:time"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:orientation="vertical"
        ohos:horizontal_center="true"
        ohos:top_margin="106vp">

        <Text
            ohos:id="$+id:text_description"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:text_size="20fp"
            ohos:text_color="#ffffff"
            ohos:bottom_margin="-10vp"
            ohos:text=" "
            ohos:visibility="invisible"/>


        <Text
            ohos:id="$+id:text_date"
            ohos:height="match_content"
            ohos:width="match_content"
            ohos:text="00:00:00"
            ohos:text_size="60fp"
            ohos:text_color="#ffffff"/>

    </DirectionalLayout>

    <DirectionalLayout
        ohos:height="200vp"
        ohos:width="match_parent"
        ohos:align_parent_bottom="true"
        ohos:orientation="horizontal"
        ohos:alignment="center">

        <Image
            ohos:id="$+id:cancle"
            ohos:height="80vp"
            ohos:width="80vp"
            ohos:padding="10vp"
            ohos:scale_mode="stretch"
            ohos:image_src="$media:aar_ic_restart"
            ohos:visibility="invisible"/>

        <Image
            ohos:id="$+id:recorder"
            ohos:height="100vp"
            ohos:width="100vp"
            ohos:padding="10vp"
            ohos:margin="15vp"
            ohos:scale_mode="stretch"
            ohos:image_src="$media:aar_ic_rec"/>

        <Image
            ohos:id="$+id:play"
            ohos:height="80vp"
            ohos:width="80vp"
            ohos:padding="10vp"
            ohos:scale_mode="stretch"
            ohos:image_src="$media:aar_ic_play"
            ohos:visibility="invisible"/>
    </DirectionalLayout>
</DependentLayout>

一、录音界面录音、暂停录音、播放录音

初始化设置参数

    initRecorder();//初始化录音
    initCommpont();//初始化按钮等相关组件
    initWaveCommpont();//初始化波形

录音,暂停相关操作

录音按钮btn_recorder,当点击录音按钮,计时开始显示:Recording 秒表计时。当暂停录音时修改图标,时间计时器停止。当再次点击录音,会累计前面录音时间。通过系统录音方法Recorder录制。

Recorder mRecorder = OmRecorder.wav(
  new PullTransport.Default(Util.getMic(source, channel,  sampleRate), AudioRecorderAbilitySlice.this),file);

录音相关接口:

    Recorder wav(PullTransport pullTransport, File file) //录制声音
    方法名 startPlaying()//开始播放录音
           stopRecording()//停止录音
           restartRecording()//重新录音
           pauseRecording() //暂停录音

测试信息

CodeCheck代码测试无异常

CloudTest代码测试无异常

病毒安全检测通过

当前版本demo功能与原组件基本无差异

版本迭代

  • 1.0.0

版权和许可信息

The MIT License (MIT)

Copyright (c) 2016 Adriel Café

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

ohos-AudioRecorder-master.zip 1M 44次下载
已于2021-12-29 16:31:46修改
收藏
回复
举报
回复
    相关推荐