yangchunlong

本篇文章主要介绍一下以下功能:

用小程序实现录音功能,在本地播放,提交服务端,服务端播放。

中间遇到了一些坑,找到了一些解决方法,如果有更优的解决方案希望你能在评论留言,一起加油。

1.小程序端

图片描述

前端展示页面,小程序采用数据绑定方式,这里正在录音和正在播放的状态切换,用两张图根据录音状态和播放状态切换,这里就不多说了。

长按开始录音,触发录音事件,松手执行录音结束,将录音结果保存在本地。

    /**
     * 开始录音
     */
    startRecord: function () {
        var that = this
        that.setData({
            isRecording: true,
        });

        whisper.startRecord({
            success(result) {
                that.setData({
                    isRecording: false,
                });
                console.log(\'录音成功:\', result);
                console.log(\'录音成功:\', result.tempFilePath);
                //提交录音
                var src = result.tempFilePath;
                var name = \'record\'
                repair.uploadRecord(src, name, (res) => {

                    that.data.recordObj.record = res;
                });
            },
            fail(error) {
                console.log(\'录音失败:\', error);
            },
            process() {
                that.setData({
                    duration: whisper.getRecordDuration(),
                });
            },
            compelete() {

            },
        });
    },

    /**
     * 停止录音
     */
    stopRecord: function () {
        var that = this;
        whisper.stopRecord({
            success(result) {
                that.setData({
                    isRecording: false,
                });

                console.log(\'停止录音:\', result);
            },
            fail(error) {
                console.log(\'停止录音失败:\', error);
            }
        });

    },

    /**
     * 播放录音
     */
    play: function () {
        var that = this
        that.setData({
            isPlaying:

分类:

技术点:

相关文章: