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

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>

.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;
    },

})

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;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序是一种基于微信平台的应用的开发模式,可以快速的开发出符合用户需求的小程序。在小程序的开发中,组件是一个非常重要的概念,通过组件可以实现复用性和模块化编程思想。 组件应用是小程序开发的基础。通过组件可以将某一模块化并封装起来,使得组件可以在不同的页面间得到复用,大大提升了开发效率并减少了代码冗余。微信小程序提供了丰富的自带组件,包括文本、图片、按钮、输入框等等,开发者也可以自己开发组件来满足自己的需求。实际开发中,通过组件可以快速搭建页面框架和业务逻辑。 Demo是一个演示小程序的示例程序。在小程序的实际开发过程中,一个好的Demo非常重要。通过Demo,开发人员可以更深入的了解小程序的开发流程、组件的应用和实际的业务开发等等。在Demo中,通常会包括小程序的一些基础操作,如页面跳转、数据绑定、组件的使用等。而在实际开发中,Demo还会包括一些复杂的业务场景,如支付、登录、数据列表展示等等。Demo不仅为开发者提供了学习和实践的机会,也方便了使用者了解该小程序的功能和特点。 总之,微信小程序组件的应用和Demo的开发都是小程序开发过程中非常重要的两个部分。良好的组件应用和精心设计的Demo,可以在极短的时间内实现小程序开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值