• 微信小程序-语音输入(录音并播放)


    xml

            <view class="other-des">
                <view class="other-des-list">
                    <view wx:for="{{imgFiles}}" wx:key="index">
                        <view class="des-name" hidden="{{item.fileType!='mp3'}}">语音</view>
                        <view>
                            <view bindtap="playVoice" data-url="{{item.url}}" class="voice" hidden="{{item.fileType!='mp3'}}">{{item.duration}}</view>
                        </view>
                    </view>
                </view>
                <view class="action">
                    <view bindtouchstart="audiostart" bindtouchend="audioend">
                        <image class="action-icon" src="../images/icon_voice.png" mode="" />
                    </view>
                </view>
            </view>
       <view bindtouchstart="audiostart" bindtouchend="audioend">
          <image class="action-icon" src="../images/icon_voice.png" mode="" />
       </view>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    .js

    // expertConsultations/addQuestion/index/index.js
    const httpUtils = require("../../../utils/httpUtils")
    const stringUtils = require("../../../utils/stringUtils")
    const app = getApp()
    //定义一个全局变量,调用微信官方定义语音管理对象
    const recorderManager = wx.getRecorderManager();
    Page({
    
        /**
         * 页面的初始数据
         */
        data: {
            phone: '',
            questionTypeIndex: '',
            questionTypeOptions: [],
            title: "",
            info: "",
            biaoQian: "",
            imgFiles: [],
            zid: '',
        },
    
        /**
         * 生命周期函数--监听页面加载
         */
        onLoad(options) {
            this.setData({
                zid: options.id
            })
        },
    
        onInput() {},
        cancelTap() {
            wx.navigateBack()
        },
    
        //语音开始
        audiostart: function () {
            wx.showToast({
                title: '正在录音...',
                icon: 'loading',
                duration: 10000
            });
            recorderManager.start({
                format: 'mp3'
            });
    
        },
    
        //语音结束
        audioend: function () {
            //弹窗消失
            wx.hideToast({
                title: '正在录音...',
                icon: 'loading'
            });
            var path = "";
            var that = this;
            recorderManager.stop();
            recorderManager.onStop(function (res) {
                path = res.tempFilePath;
                console.log("结束", res)
                console.log(res.tempFilePath)
                if (res.duration < 1000) {
                    wx.showModal({
                        title: '录音时长太短,请重新录音',
                        content: '',
                    })
                } else {
                    if (res.tempFilePath != "") {
                        console.log(that.data.imgFiles);
                        that.setData({
                            imgFiles: that.data.imgFiles.concat([{
                                url: res.tempFilePath,
                                tempFilePath: res.tempFilePath,
                                fileType: 'mp3',
                                duration: parseInt(res.duration / 1000)
                            }])
                        });
                        console.log(2, that.data.imgFiles);
    
                        //将语音文件保存至后台
                        // that.saveVoiceInfo(res.tempFilePath);
                    } else {
    
                    }
                }
    
            });
        },
        playVoice() {
            // 获取innerAudioContext实例
            const innerAudioContext = wx.createInnerAudioContext()
            // 是否自动播放
            innerAudioContext.autoplay = true
            // 设置音频文件的路径
            innerAudioContext.src = this.data.tempFilePath;
            // 播放音频文件
            innerAudioContext.onPlay(() => {
                console.log('开始播放')
            });
            // 监听音频播放错误事件
            innerAudioContext.onError((res) => {
                console.log(res.errMsg)
            })
        },
        //保存至后台
        saveVoiceInfo: function (path) {
            var that = this;
        },
    
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112

    css

    .other-des {
        width: 100%;
        min-height: 100rpx;
        background: rgba(217, 217, 217, 0);
        border-radius: 10rpx;
        border: 2rpx solid #D9D9D9;
        position: relative;
        padding: 20rpx;
        box-sizing: border-box;
    }
    .other-des-list {
        margin-bottom: 30rpx;
    }
    .image {
        width: 300rpx;
        height: auto;
        background: #E7E7E7;
        border-radius: 10rpx;
    }
    
    .des-name {
        font-size: 26rpx;
        font-family: Source Han Sans CN-Regular, Source Han Sans CN;
        font-weight: 400;
        color: #666666;
        line-height: 30rpx;
        margin-bottom: 10rpx;
        margin-top: 20rpx;
    }
    .action {
        position: absolute;
        right: 0;
        bottom: 0;
        display: flex;
        align-items: center;
        justify-content: flex-end;
    }
    
    .action-icon {
        width: 50rpx;
        height: 50rpx;
        margin-left: 30rpx;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    springboot+vue网上学生评教系统java
    [MAUI]在.NET MAUI中实现可拖拽排序列表
    【单片机】11-步进电机和直流电机
    Unity基于C#事件委托机制
    vue.js中使用三元运算符设置动态样式
    visual studio code导入自定义模块(pycharm中能够运行的文件,vs code报错:未找到指定模块)
    Diffusion-VITS:VITS与Grad-TTS的融合
    Nuxt.js详解(二)
    数据结构六:线性表之顺序栈的设计
    解锁网络世界的利器:代理IP与Socks5代理
  • 原文地址:https://blog.csdn.net/m0_45857808/article/details/137270955