Android语音录制和播放

2017-11-01  本文已影响0人  我like砂糖

介绍

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

具体看图和gif动画效果:

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.jpgrecordingView.jpg

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

上一篇 下一篇

猜你喜欢

热点阅读