Android语音录制和播放

介绍

由于最近项目需要,需要做语音录制和播放功能模块,所以做了一个轻量级的语音录制和播放库,方便调用
这是一款参考环信的语音录制和播放库,轻量级,自带语音录制时动画效果,根据声音大小进行动画展示:

具体看图和gif动画效果:

voicerecorder.gif
voicerecorder.gif
2.jpg
2.jpg

使用步骤:

注意,目前还在开发阶段,有bug记得提issues哈

注意,6.0以上系统需要运行时授权读取sd卡和音频

1.添加Jcenter仓库 Gradle依赖:
compile 'com.ilike:voicerecorder:1.0.0'

或者

Maven
<dependency>
  <groupId>com.ilike</groupId>
  <artifactId>voicerecorder</artifactId>
  <version>1.0.0</version>
  <type>pom</type>
</dependency>
2.在Activity中添加如下代码:
/**
* 设置文件存放目录,存放路径如:/Android/data/包名/chat/voice/
* 默认不设置,路径存放为:/Android/data/包名/chat/voice/
*/
PathUtil.getInstance().createDirs("chat", "voice", appContext);
/**
* 自定义命名文件
* 默认不设置是用时间戳
*/
voiceRecorderView.setCustomNamingFile(true,"语音命名.mp3");
/**
* 自定义语音录制过程中,声音大小的动画,默认使用库文件中的动画,
* 目前默认需要设置15张图片,以后更新自定义动画帧数
*/
voiceRecorderView.setDrawableAnimation(Drawable[] animationDrawable)
/**
* 设置停止播放语音时,显示的静态icon
*/
VoicePlayClickListener.setStopPlayIcon(R.drawable.ease_chatto_voice_playing)
/**
* 设置播放语音的帧动画,
*/
VoicePlayClickListener.setPlayingIconDrawableResoure(R.drawable.voice_to_icon)

-------------------------------------------------------
比如这样的动画:
<?xml version="1.0" encoding="utf-8"?>
   <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
       android:oneshot="false">
       <item
           android:drawable="@drawable/ease_chatto_voice_playing_f1"
           android:duration="200" />
       <item
           android:drawable="@drawable/ease_chatto_voice_playing_f2"
           android:duration="200" />
       <item
           android:drawable="@drawable/ease_chatto_voice_playing_f3"
           android:duration="200" />
</animation-list>
-------------------------------------------------------
/**
* 录制语音
*/
tvRecorder.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return  voiceRecorderView.onPressToSpeakBtnTouch(v, event, new VoiceRecorderView.EaseVoiceRecorderCallback() {

                    @Override
                    public void onVoiceRecordComplete(String voiceFilePath, int voiceTimeLength) {
                        Log.e("voiceFilePath=", voiceFilePath + "  time = " + voiceTimeLength);
                        /**
                        *voiceFilePath 为录音文件存放在sd的路径
                        * voiceTimeLength 录音文件的时长
                        */
                    }
                });
            }
        });
/**
* 播放SD卡本地语音路径
* imageView显示动画
* Context,上下文
* path ,语音路径
*/
new VoicePlayClickListener(imageView, Context).playVoice(path);


/**
* 播放网络语音路径
* imageView显示动画
* Context,上下文
* path ,语音路径
*/
new VoicePlayClickListener(imageView, Context).playUrlVoice(path);
3.在布局文件xml中添加如下:
<com.ilike.voicerecorder.widget.VoiceRecorderView
          android:id="@+id/voice_recorder"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          android:visibility="invisible" />
recordingView.jpg
recordingView.jpg

最后奉上github地址:https://github.com/wangshanhai/VoiceRecorder

转载于:https://my.oschina.net/u/1463200/blog/1560865

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值