美文网首页Android开发
Android开发长按录音

Android开发长按录音

作者: 你的益达233 | 来源:发表于2022-07-05 09:56 被阅读0次

    一、前言:长按录音不算太常见功能,万一你刚好需要呢。

    二、效果图:

    长按录音.png

    三、解决思路

    主要用的是MediaRecorder,可能你百度MediaRecorder,就知道怎么做了,不过有些细节需要自己处理。

    四、给长按的全部代码

    private val mOnVoiceBtnTouchListener =
        OnTouchListener { v, event ->
            if (mBinding.tvClickPlay.visibility == View.VISIBLE){
                return@OnTouchListener false
            }
            val mOffsetLimit = 70 * v.context.resources.displayMetrics.density
            //val permissions = arrayOf(Manifest.permission.RECORD_AUDIO)
            if (!PermissionCheckUtil.checkPermissions(v.context, PermissionAppUtils.Group.RECORD_AUDIO_STORAGE)
                && event.action == MotionEvent.ACTION_DOWN
            ) {
                DialogUtils.okAndCancel(mContext, getString(R.string.record_audio_permission_tip) , ok = getString(R.string.go_open), okListener = {
                        PermissionAppUtils.requestCombinedPermission(mContext,
                            PermissionAppUtils.Group.RECORD_AUDIO_STORAGE) {
                            if (it.granted){
    
                            } else  {
                                if (it.shouldShowRequestPermissionRationale){
                                    //禁止,不做操作
    
                                } else {
                                    //永久禁止,需求说只弹前面的弹框,永久禁止就直接跳设置
                                    CUtils.startAppSettings(mContext)
                                }
                            }
                        }
    
                    })
    
                return@OnTouchListener true
            }
            if (event.action == MotionEvent.ACTION_DOWN) {
                if (MyAudioPlayManager.getInstance().isPlaying) {
                    MyAudioPlayManager.getInstance().stopPlay()
                }
                // 判断正在视频通话和语音通话中不能进行语音录制
                if (EasyFloat.appFloatIsShow(SingleCallActivity.video_float_tag) || EasyFloat.appFloatIsShow(SingleCallActivity.audio_float_tag)
                    || ActivityUtil.isActivityExist(SingleCallActivity::class.java)) {
                        mContext.show(R.string.rc_voip_occupying)
                    return@OnTouchListener true
                }
                mBinding.ivStartRecordAudio.setImageResource(R.mipmap.ic_record_audio_press)
                //开始录音,录之前先删掉之前的本地录的
                MyAudioRecordManager.getInstance().deleteAudioFile()
                currentRecordTime = 0
                isStartRecording = true
                MyAudioRecordManager.getInstance().startRec(mContext)
    
                mLastTouchY = event.y
                mUpDirection = false
                if (disposableRecordTime == null || disposableRecordTime!!.isDisposed){
                    disposableRecordTime = Observable.interval(1,TimeUnit.SECONDS,AndroidSchedulers.mainThread())
                        .take(recordMaxTime)
                        .subscribe{
                            Log.i("currentRecordTime",it.toString())
                            currentRecordTime = it+1
                            dealCurrentTimeShow(currentRecordTime,true)
                            //录够15s停止录制
                            if (currentRecordTime == recordMaxTime){
                                MyAudioRecordManager.getInstance().stopRec()
                                showRecordAfter()
                            }
                        }
                }
    
            } else if (event.action == MotionEvent.ACTION_MOVE) {
                mBinding.ivStartRecordAudio.setImageResource(R.mipmap.ic_record_audio_press)
    
            } else if (event.action == MotionEvent.ACTION_UP || event.action == MotionEvent.ACTION_CANCEL) {
                mBinding.ivStartRecordAudio.setImageResource(R.mipmap.ic_record_audio)
                //松手停止录制
                if (isStartRecording){
                    MyAudioRecordManager.getInstance().stopRec()
                    if (currentRecordTime < 3){
                        show(R.string.record_audio_not_enough_tip)
                        currentRecordTime = 0
                        dealCurrentTimeShow(currentRecordTime,true)
                        MyAudioRecordManager.getInstance().deleteAudioFile()
                    } else {
                        showRecordAfter()
                    }
                    isStartRecording = false
                }
    
                disposableRecordTime?.run {
                    if (!isDisposed){
                        dispose()
                    }
                }
            }
    
            true
        }
    

    需要完整代码的可私信我

    相关文章

      网友评论

        本文标题:Android开发长按录音

        本文链接:https://www.haomeiwen.com/subject/rifsbrtx.html