当前位置:   article > 正文

uniapp 小程序 录音功能_uniapp 微信小程序 webview 远程链接,录音

uniapp 微信小程序 webview 远程链接,录音

修改了代码uniapp可适用(数据赋值方式)
代码如下:

<template>
    <view class="sound-recording">
        <view class="time">{{status==0?'录音时长':(status==3?'录音已完成':'正在录音中')}}{{time}}</view>
        <view class="btn">
            <view :class="status==3?'show':'hide'" @tap="reset" hover-class="jump-hover">重新录制</view>
            <view :class="status==3 && playStatus==0?'show':'hide'" @tap="bofang" hover-class="jump-hover">{{playStatus==1?'录音播放中':'播放录音'}}</view>
        </view>
        <view class="progress">
            <text class="txt">最大录音时长({{duration/1000}}= {{duration/60000}}分钟)</text>
            <progress :percent="time*(100/(duration/1000))" border-radius="10" color="green" stroke-width="10" backgroundColor="#fff" />
        </view>
        <view class="anniu">
            <view :class="status==0?'row':'no-clicking'" @click="kaishi" hover-class="jump-hover">开始</view>
            <view :class="status==1?'row':'no-clicking'" @click="zanting" hover-class="jump-hover">暂停</view>
            <view :class="status==2?'row':'no-clicking'" @tap="jixu" hover-class="jump-hover">继续</view>
            <view :class="status==1 || status==2?'row':'no-clicking'" @tap="tingzhi" hover-class="jump-hover">停止</view>
        </view>
    </view>
</template>

<script>
    const recorderManager = uni.getRecorderManager()
    const innerAudioContext = uni.createInnerAudioContext()
    var init
    export default {
        data() {
            return {
                time: 0, //录音时长
                duration: 600000, //录音最大值ms 600000/10分钟
                tempFilePath: "", //音频路径
                status: 0, //录音状态 0:未开始录音 1:正在录音 2:暂停录音 3:已完成录音
                playStatus: 0, //录音播放状态 0:未播放 1:正在播放
            }
        },
        methods: {
            kaishi: function() {
                clearInterval(init) //清除定时器
                //监听录音自动结束事件(如果不加,录音时间到最大值自动结束后,没获取到录音路径将无法正常进行播放)
                recorderManager.onStop((res) => {
                    console.log('recorder stop', res)
                    this.tempFilePath = res.tempFilePath
                    this.status = 3
                    this.recordingTimer(this.time)
                })

                const options = {
                    duration: this.duration, //指定录音的时长,单位 ms
                    sampleRate: 16000, //采样率
                    numberOfChannels: 1, //录音通道数
                    encodeBitRate: 96000, //编码码率
                    format: 'mp3', //音频格式,有效值 aac/mp3
                    frameSize: 10, //指定帧大小,单位 KB
                }
                this.recordingTimer()
                recorderManager.start(options)
                // 监听音频开始事件
                recorderManager.onStart((res) => {
                    console.log('recorder start')
                    this.status = 1
                })
                console.log(this.status);
            },

            /**
             * 暂停录音
             */
            zanting: function() {
                console.log('zanting');
                recorderManager.onPause(() => {
                    console.log('recorder pause')
                    this.status = 2
                })
                this.recordingTimer(this.time)
                recorderManager.pause()
            },

            /**
             * 继续录音
             */
            jixu: function() {
                this.status = 1
                this.recordingTimer()
                recorderManager.resume()
            },

            /**
             * 停止录音
             */
            tingzhi: function() {
                recorderManager.onStop((res) => {
                    console.log('recorder stop', res)
                    this.tempFilePath = res.tempFilePath
                    this.status = 3
                })
                this.recordingTimer(this.time)
                recorderManager.stop()

            },

            /**
             * 播放录音
             */
            bofang: function() {
                //音频地址
                console.log(this.tempFilePath);
                innerAudioContext.src = this.tempFilePath
                //在ios下静音时播放没有声音,默认为true,改为false就好了。
                // innerAudioContext.obeyMuteSwitch = false

                //点击播放
                if (this.playStatus == 0) {
                    this.playStatus = 1
                    innerAudioContext.play()
                }
                // //播放结束
                innerAudioContext.onEnded(() => {
                    innerAudioContext.stop()
                    this.playStatus = 0
                })
            },
            recordingTimer: function(time) {
                var that = this
                if (time == undefined) {
                    //将计时器赋值给init
                    init = setInterval(function() {
                        var time = that.time + 1;
                        that.time = time
                    }, 1000);
                } else {
                    clearInterval(init)
                    console.log("暂停计时")
                }
            },

            /**
             * 重新录制
             */
            reset: function() {
                var that = this
                wx.showModal({
                    title: "重新录音",
                    content: "是否重新录制?",
                    success(res) {
                        if (res.confirm) {
                            that.time = 0
                            that.tempFilePath = ''
                            that.status = 0
                            that.playStatus = 0
                            innerAudioContext.stop()
                        }
                    }
                })
            }
        }
    }
</script>

<style>
    .sound-recording {
        background-color: rgb(234, 234, 234);
        margin: 10rpx 30rpx;
        border-radius: 20rpx;
        padding: 5rpx 0rpx;
    }

    .btn {
        margin: 0rpx 100rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .btn .show {
        padding: 10rpx;
        width: 200rpx;
        font-size: 25rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: rgb(178, 228, 228);
        border-radius: 20rpx;
        border: 5rpx solid rgb(127, 204, 214);
    }

    .btn .hide {
        padding: 10rpx;
        width: 200rpx;
        font-size: 25rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 20rpx;
        border: 5rpx solid #eee;
        pointer-events: none;
        background-color: rgba(167, 162, 162, 0.445);
    }

    .time {
        line-height: 70rpx;
        text-align: center;
        font-size: 30rpx;
    }

    .progress {
        margin: 20rpx;
    }

    .play {
        margin: 0rpx 20rpx;
    }

    .txt {
        display: flex;
        justify-content: center;
        line-height: 60rpx;
        font-size: 25rpx;
    }

    .anniu {
        margin: 10rpx 50rpx;
        display: flex;
        justify-content: space-between;
    }

    .row {
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
        font-size: 25rpx;
        width: 80rpx;
        height: 80rpx;
        background-color: rgb(178, 228, 228);
        border: 5rpx solid rgb(127, 204, 214);
    }

    .jump-hover {
        transform: scale(0.9);
    }

    /*禁止点击*/

    .anniu .no-clicking {
        pointer-events: none;
        background-color: rgba(167, 162, 162, 0.445);
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
        font-size: 25rpx;
        width: 80rpx;
        height: 80rpx;
        border: 5rpx solid rgb(241, 244, 245);
    }
</style>
  • 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
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号