收藏
回答

wx.getRecorderManager() 使用腾讯语音 Ai 如果别的页面有了 该页面 不能

问题类型 插件 AppID 插件版本号 AppID 操作系统 微信版本 基础库版本
Bug wx3e17776051baf153 1.3.4 wxbfdecf08b3b69648 iOS 8.0.18 2.22.1




let plugin = requirePlugin("QCloudAIVoice");

plugin.setQCloudSecret(66, '5','45645645', false); //设置腾讯云账号信息,其中appid是数字,secret是字符串,

let managerCloud =plugin.getRecordRecognitionManager();  

//防抖节流 

import {

  WechatOssapiGetencypt,

  WechatProjectAudiomerge

}  from "../../common/request"

Page({

  data: {

    currentText:"",

    currentTextResult:'',

    secordRecorde:0,

    RecordetempFilePath:[],

    RecordTotaltime:0,

    RecordTimer:null,

    recorderManagerCloud:null,

    innerAudioContextCloud:null,

  },

  streamRecord() {


    managerCloud.start({

        duration:600000, 

        engine_model_type: '16k_zh'

      });



    console.log('开始录制')

  },

  streamRecordEnd() {

    // if(this.data.secordRecorde<60){

    //   uni.showToast({

    //     title: '录制时长小于1分钟',

    //     duration: 2000,

    //     icon: 'none'

    // })

    //   return

    // }

    managerCloud.stop();

    clearInterval(this.data.RecordTimer)

    this.setData({

      RecordTotaltime:this.data.secordRecorde,

      secordRecorde:0

    })

    console.log('结束录制');

  },

  onLoad (options) {

   this.setData({

    recorderManagerCloud:wx.getRecorderManager()

   })


    if (wx.setScreenBrightness{

      // 保持屏幕常亮 true / false

      wx.setKeepScreenOn({

        keepScreenOn: true

      })

  

    } else {

      // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示

      wx.showModal({

        title: '提示',

        content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'

      })

    }

    this.initRecord()

  },

  initRecord(){


    managerCloud.onRecognize ((res) => {

      console.log(res)

      let text = res.result

      this.setData({

        currentText: this.data.currentTextResult+text,

      })

    })



    managerCloud.onStart((res) => {

      console.log(res)

      if(res.msg == 'OK'{

          // 提示用户正在录音

          wx.showToast({

              title: '正在识别语音...',

              duration: 1000,

              icon: 'loading'

          })

          clearInterval(this.data.RecordTimer)

          this.data.RecordTimer=setInterval(() => {

            this.data.secordRecorde++

            this.setData({

              secordRecorde:this.data.secordRecorde,

              RecordetempFilePath:this.data.RecordetempFilePath.length>0?this.data.RecordetempFilePath:[],

              RecordTotaltime:this.data.RecordetempFilePath.length==0?0:this.data.secordRecorde

            })

          }, 1000);

      }

  })



  managerCloud.onError((res) => {

    console.log('recorder error', res.errMsg);

})

managerCloud.onStop((res) => {


      console.log(res)


      const {tempFilePath,result} = res


      clearInterval(this.data.RecordTimer)

      this.data.RecordetempFilePath.push(tempFilePath)

      this.setData({

        RecordetempFilePath:this.data.RecordetempFilePath,

        currentTextResult:this.data.currentTextResult+result

      })


      this.data.secordRecorde>0?

      this.streamRecord():""


      if(this.data.RecordetempFilePath.length>0 && this.data.secordRecorde==0){

         this.uploadFileRecorder(149,this.data.RecordTotaltime,1,this.data.RecordetempFilePath)

      }

    })

  },

  setFileName(i, type = 'mp3',RecordSceneId) {

    const  time = Math.round(new Date()/1000)

    let user_id = this.data.user_id;

    return (`${user_id}_${RecordSceneId}_${i+1}_${time}.${type}`);

  },

  uploadFileRecorder(RecordSceneId,RecordTotaltime,language,RecordetempFilePath){


      //RecordTotaltime 录制总时长

    let RecorderList =RecordetempFilePath   //视频录制列表

    const that = this


    WechatOssapiGetencypt({

      type: 4,

      expire: 3600

    }).then(res=>{

      const{

        code,

        data

      }= res

        if(code==0){

          console.log('进入这里')

            var babyData = {

              'Filename':'',

              'name': 'img',

              'key': '',

              'policy': data.policy,

              'OSSAccessKeyId': data.accessid,

              'success_action_status': '200', //让服务端返回200,不然,默认会返回204

              'signature': data.signature

            }

            data.callback?

            babyData.callback= data.callback:

            ""

            let videoCount = 0; //记录失败 测试

            let video_json=[]

            console.log(RecorderList)

            for (let k = 0; k < RecorderList.length; k++{

              let fileName = this.setFileName(k,'mp3',RecordSceneId)

              babyData.Filename = fileName

              babyData.key = data.dir + fileName

              wx.uploadFile({

                url: data.host,

                formData: babyData,

                name: 'file',

                filePath: RecorderList[k],

                header: {

                  'content-type': 'multipart/form-data'

                },

                success: e => {

                  if (e.statusCode == 200{

                    video_json.push({

                      id: k+1,

                      video_path: data.dir + fileName

                    })


                    if (video_json.length == RecorderList.length-videoCount{

                        //合并视频接口

                        WechatProjectAudiomerge(

                          {

                            video_json:video_json,

                            language,

                            scene_id:RecordSceneId,

                            time:Math.round(new Date()/1000),

                            totaltime:RecordTotaltime

                          },

                          ).then(res=>{


                        })

                        //合并视频接口

                    }


                  }

                },

                fail: failres => {

                  videoCount++ 

                  wx.showToast({

                    title: '上传文件出错',

                    icon: 'none',

                    duration: 2000

                  })

                }

              })

            }

  


        }

    })



  },

  onReady: function () {


  },

  onShow: function () {


  },

  onHide () {

    managerCloud.stop();

    clearInterval(this.data.RecordTimer)

    this.setData({

      RecordTotaltime:0,

      secordRecorde:0

    })

   this.data.recorderManagerCloud.stop();

  },

  onUnload () {

    managerCloud.stop();

    clearInterval(this.data.RecordTimer)

    this.setData({

      RecordTotaltime:0,

      secordRecorde:0

    })

   this.data.recorderManagerCloud.stop();

  },

  onPullDownRefresh: function () {


  },

  onReachBottom: function () {


  },

  onShareAppMessage: function () {


  }

})


回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容