Skip to content

Commit

Permalink
微信JsSDK录音权限检测实现
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyuecn committed Aug 30, 2019
1 parent 5940583 commit e02cc3f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion dist/app-support/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 45 additions & 14 deletions src/app-support/app-ios-weixin-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,34 @@ platform.RequestPermission=function(success,fail){
};
WXRecordData.wx=wx;

//微信不能提前发起授权请求,需要等到开始录音时才会调起授权
success();
//可能已经在录音了,关掉再说
if(isStart){
killStart(function(){
platform.RequestPermission(success,fail);
});
return;
};

//微信不能提前发起授权请求,需要开始录音时才会调起授权,并且授权一次后管很久,因此开始录音然后关闭就能检测出权限
wx.startRecord({
success:function(){
setTimeout(function(){
stopNow(function(e){
if(e){
fail("清理资源出错:"+e);
}else{
success();
};
});
},100);
}
,fail:function(o){
fail("无法录音:"+o.errMsg);
}
,cancel:function(o){
fail("用户不允许录音:"+o.errMsg,true);
}
});
});
};
var isWaitStart,isStart;
Expand All @@ -41,24 +67,27 @@ var stopNow=function(call){
isWaitStart=0;
WXRecordData.wx.stopRecord({
success:function(){
call();
call&&call();
},fail:function(o){
call("无法结束:"+o.errMsg);
call&&call("无法结束录音:"+o.errMsg);
}
});
};
var killStart=function(call){
console.warn("录音中,正在kill重试");
stopNow(function(){
setTimeout(call,300);
});
};
platform.Start=function(set,success,fail){
var wx=WXRecordData.wx;
if(!wx){
fail("请先调用RequestPermission");
return;
};
if(isStart){
console.log("正在录音,正在结束后重试");
stopNow(function(){
setTimeout(function(){
platform.Start(set,success,fail);
},300);
killStart(function(){
platform.Start(set,success,fail);
});
return;
};
Expand All @@ -72,6 +101,11 @@ platform.Start=function(set,success,fail){

isStart=0;
isWaitStart=1;
var startFail=function(o){
isWaitStart=0;
fail("无法录音:"+o.errMsg);
stopNow();
};
wx.startRecord({
success:function(){
isStart=1;
Expand All @@ -80,11 +114,8 @@ platform.Start=function(set,success,fail){
WXRecordData.start=set;
success();
}
,fail:function(o){
isWaitStart=0;
fail("无法录音:"+o.errMsg);
stopNow();
}
,fail:startFail
,cancel:startFail
});

//监听超时自动停止后接续录音
Expand Down

0 comments on commit e02cc3f

Please sign in to comment.