PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > HBuilderX教程 > 正文

    uniapp的录音与播放

    作者:admin来源:网络浏览:时间:2021-08-27 16:33:16我要评论
    导读:uniapp的录音与播放
    录音

    1. onLoad(options) { 
    2.     let that=this 
    3.     this.recorderManager = uni.getRecorderManager(); //创建录音管理 
    4. }, 
    5.  
    6. methods:{ 
    7.     //开始录音 
    8.     startRecord(index){ 
    9.     let that=this 
    10.     uni.authorize({ 
    11.         scope:"scope.record"
    12.         success(){ 
    13.         uni.showToast({ 
    14.             icon:"none"
    15.             title:"开始录音" 
    16.         }) 
    17.         that.recorderManager.start(); 
    18.         that.recorderManager.onStart() 
    19.         console.log("录音执行"
    20.         }, 
    21.         fail() { 
    22.         uni.showModal({ 
    23.             title: '提示'
    24.             content: '您未授权录音,功能将无法使用'
    25.             showCancel: true
    26.             confirmText: "授权"
    27.             confirmColor: "#52a2d8"
    28.             success(res) { 
    29.                 if(res.confirm){ 
    30.                 uni.openSetting({ 
    31.                  
    32.                             }) 
    33.             }else
    34.                                      
    35.             } 
    36.             } 
    37.         }) 
    38.         } 
    39.     }) 
    40.     }, 
    41.  
    42.  
    43.     //结束录音 
    44.     endRecord(index){ 
    45.     let that=this 
    46.     uni.stopRecord({ 
    47.         success:function(e){ 
    48.         that.recorderManager.stop(); 
    49.             that.recorderManager.onStop((res=>{ 
    50.             that.audioTempFile=res.tempFilePath 
    51.             that.uploadAudio(index) 
    52.         })) 
    53.         } 
    54.     }) 
    55.     } 

    播放

     

    1. onLoad(options) { 
    2.     let innerAudioContext = uni.createInnerAudioContext();    //创建音频文件 
    3.     innerAudioContext.autoplay = false;    //不自动播放 
    4.     innerAudioContext.src = this.audioSrc;    //音频文件链接 
    5.     this.innerAudioContext=innerAudioContext 
    6. }, 
    7.  
    8. methods:{ 
    9.     //开始音频文件 
    10.     startAudio(){ 
    11.         this.innerAudioContext.play(() => { 
    12.     console.log('开始播放'); 
    13.         }); 
    14.     this.innerAudioContext.onError((res) => { 
    15.         console.log(res.errMsg); 
    16.         console.log(res.errCode); 
    17.     }); 
    18.     }, 
    19.  
    20.     //结束音频 
    21.     endAudio(){ 
    22.     let that=this 
    23.     uni.showToast({ 
    24.         icon:"none"
    25.         title:"已结束" 
    26.     }) 
    27.     this.innerAudioContext.stop(() => { 
    28.         console.log('已暂停'); 
    29.     }); 
    30.     this.innerAudioContext.onError((res) => { 
    31.         console.log(res.errMsg); 
    32.         console.log(res.errCode); 
    33.     }); 
    34.     }, 
    35.  
    36.     //暂停音频文件 
    37.     pauseAudio(desc){ 
    38.         let that=this 
    39.     uni.showToast({ 
    40.         icon:"none"
    41.         title:desc 
    42.     }) 
    43.     this.innerAudioContext.pause(() => { 
    44.         console.log('已暂停'); 
    45.     }); 
    46.     this.innerAudioContext.onError((res) => { 
    47.         console.log(res.errMsg); 
    48.          console.log(res.errCode); 
    49.     }); 
    50.     } 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-146-6608-1.html
    相关热词搜索: uniapp录音 uniapp播放