wx.startRecord调用中IOS的兼容问题
发布于 5 年前 作者 liangxiulan 4138 次浏览 来自 问答

我点击一个按钮跳转到一个新页面,在页面初始化的时候,调取wx.startRecord({})安卓是没有问题的,而苹果只有第一次进入的时候会进入success,之后进入都是fail: function (res) {

          //录音失败   如果我在页面添加一个按钮点击按钮录音的话,无论多少次都没有问题。

点击话筒进入

苹果第一次进的时候进入success之后进入都是失败

代码:    onLoad: function () {


      var that = this;


      wx.getStorage({


        key: ‘search’,


        success: function (res) {


          if (res.data) {


            that.setData({


              history_text: res.data,


              history_empty: false


            })


          }


        },


        fail: function () {//没有缓存 不显示


          that.setData({


            history_empty: true


          })


        }


      })


      wx.getStorage({


        key: ‘userInfo’,


        success: function (res) {


          if (res.data.userId) {


            that.setData({


              userId: res.data.userId


            })


          }


        },


        fail: function () {//没有缓存 不显示


          that.setData({


            userId: ‘’


          })


        }


      })


    },


    onShow: function () {   


      this.mytouchstart();


    },


    onHide: function () {


      // 页面关闭


      wx.stopRecord()


    },


    onUnload: function () {


      // 页面关闭


      wx.stopRecord()


    },


    mytouchstart: function () {


      console.log(1)


      var that = this


      //开始录音。当主动调用wx.stopRecord,


      //或者录音超过1分钟时自动结束录音,返回录音文件的临时文件路径。


      //当用户离开小程序时,此接口无法调用。


      wx.stopRecord()


      wx.startRecord({


        success: function (res) {


          console.log(‘录音成功’ + JSON.stringify(res)); 


          that.setData({


            voiceButtonName: ‘语音识别’,


            wavetoword: true,


            rewavetoword: true,


            recordingfailed: true,


            unrecognized: true,


            acousticwave: true,


            ensure: true,


            uploadfailed: true,


            wavewait: false,


            voicePlayButtonName: ‘开始播放’,


            tempFilePath: res.tempFilePath


          })


          //上传语音文件至服务器


          wx.uploadFile({


            url: ‘http://test2’,


            filePath: res.tempFilePath,


            name: ‘file’,


            // header: {}, // 设置请求的 header


            formData: {


              ‘msg’: ‘voice’


            }, // HTTP 请求中其他额外的 form data


            success: function (res) {


              // success


              console.log(‘begin’);


              console.log(res.data);


              var json = JSON.parse(res.data);


              console.log(json.result);


              var jsonResult = json.result;


              console.log(jsonResult);


              var item = jsonResult


              if (!item) {


                that.setData({


                  acousticwave: true,


                  wavewait: true,


                  wavetoword: true,


                  recordingfailed: true,


                  ensure: true,


                  uploadfailed: true,


                  rewavetoword: false,


                  unrecognized: false


                })


              }


              else if (item) {


                that.setData({


                  val: item,


                  acousticwave: true,


                  recordingfailed: true,


                  unrecognized: true,


                  ensure: false,


                  wavewait: true,


                  uploadfailed: true,


                  rewavetoword: false,


                  wavetoword: false


                })


              }


            },


            fail: function (err) {


              that.setData({


                val: item,


                acousticwave: true,


                recordingfailed: true,


                ensure: true,


                unrecognized: true,


                wavewait: true,


                uploadfailed: false,


                rewavetoword: false,


                wavetoword: true


              })


              // fail


              console.log(err);


            },


            onVoiceRecordEnd: function (res) {


                console.log(21)


            },


            complete: function () {


              // complete


            }


          })



        },


        fail: function (res) {


          //录音失败


          that.setData({


            voiceButtonName: ‘语音识别’,


            acousticwave: true,


            wavewait: true,


            wavetoword: true,


            rewavetoword: false,


            uploadfailed: true,


            ensure: true,


            recordingfailed: false,


            unrecognized: true


          })


          console.log(‘录音失败’ + JSON.stringify(res));


        }


      })


      setTimeout(function () {


        //结束录音


        wx.stopRecord()


      }, 5000)



    },


    mytouchend: function () {


      wx.stopRecord()


    }

1 回复

简单的说就是IOS下如果在页面初始化时候调用 wx.startRecord ,只有第一次进来时会成功,之后进来都是录音失败

回到顶部