From e02cc3fd24792e31caf0aa5eaffa1c0f2761b897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E5=9D=9A=E6=9E=9C?= <753610399@qq.com> Date: Fri, 30 Aug 2019 23:24:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1JsSDK=E5=BD=95=E9=9F=B3?= =?UTF-8?q?=E6=9D=83=E9=99=90=E6=A3=80=E6=B5=8B=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/app-support/app.js | 2 +- src/app-support/app-ios-weixin-support.js | 59 +++++++++++++++++------ 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/dist/app-support/app.js b/dist/app-support/app.js index 86bda8f..289f071 100644 --- a/dist/app-support/app.js +++ b/dist/app-support/app.js @@ -3,4 +3,4 @@ https://github.com/xiangyuecn/Recorder src: app-support/app.js,app-support/app-ios-weixin-support.js,app-support/app-native-support.js */ -!function(){"use strict";var n=/MicroMessenger/i.test(navigator.userAgent),e=window.RecordAppBaseFolder||"/Recorder/dist/",t=window.OnRecordAppInstalled,u=[{Key:"Native",Support:function(e){l.AlwaysAppUseJS?e(!1):o.Config.IsApp(e)},CanProcess:function(){return!0},Config:{IsApp:function(e){e(!1)},JsBridgeRequestPermission:function(e,n){n("JsBridgeRequestPermission未实现")},JsBridgeStart:function(e,n,t){t("JsBridgeStart未实现")},JsBridgeStop:function(e,n){n("JsBridgeStop未实现")},paths:[{url:e+"app-support/app-native-support.js",check:function(){return!o.IsInit}}]},ExtendDefault:!0},{Key:"IOS-Weixin",Support:function(e){l.AlwaysUseWeixinJS||!Recorder.Support()?e(n):e(!1)},CanProcess:function(){return!1},Config:{WxReady:function(e){e(null,"未实现IOS-Weixin.Config.WxReady")},DownWxMedia:function(e,n,t){t("下载素材接口DownWxMedia未实现")},paths:[{url:e+"app-support/app-ios-weixin-support.js",check:function(){return!r.IsInit}},{url:e+"engine/beta-amr.js",check:function(){return!Recorder.prototype.amr}}]},ExtendDefault:!0},{Key:"Default",Support:function(e){e(!0)},CanProcess:function(){return!0},Config:{paths:[{url:e+"recorder-core.js",check:function(){return!window.Recorder}},{url:e+"engine/mp3.js",check:function(){return!Recorder.prototype.mp3}}]}}],o=u[0],r=u[1],f=u[2];f.RequestPermission=function(e,n){var t=Recorder();t.open(function(){t.close(),e()},n)},f.Start=function(e,n,t){var o=l.__Rec;null!=o&&o.close(),l.__Rec=o=Recorder({type:e.type||"mp3",sampleRate:e.sampleRate||16e3,bitRate:e.bitRate||16,onProcess:e.onProcess||function(){}}),o.appSet=e,o.open(function(){o.start(),n()},function(e){t(e)})},f.Stop=function(t,n){var o=l.__Rec;if(o){var r=function(){for(var e in o.close(),o.set)o.appSet[e]=o.set[e];l.__Rec=null};o.stop(function(e,n){r(),t(e,n)},function(e){r(),n(e)})}else n("未开始录音")};var l={LM:"2019-8-29 11:20:04",Current:0,IsWx:n,BaseFolder:e,AlwaysUseWeixinJS:!1,AlwaysAppUseJS:!1,Platforms:{Native:o,Weixin:r,Default:f},Js:function(r,i,s,e){var a=(e=e||window).document,c=function(e){if(e>=r.length)i();else{var n=r[e],t=n.url;if(!1!==n.check()){var o=a.createElement("script");o.setAttribute("type","text/javascript"),o.setAttribute("src",t),o.onload=function(){c(e+1)},o.onerror=function(e){s("请求失败:"+(e.message||"-")+","+t)},a.body.appendChild(o)}else c(e+1)}};c(0)},Install:function(t,o){var r=l.__reqs||(l.__reqs=[]);r.push({s:t,f:o}),t=function(){i("s",arguments)},o=function(e,n){i("f",arguments)};var i=function(e,n){for(var t=0;t=r.length)return v.decodeTime=Date.now()-u,void function(){a||(a=Date.now());for(var e=[],n=0;n=f.length)r();else{var e=f[t];g.DownWxMedia({mediaId:e,transform_mediaIds:f.join(","),transform_type:d.type,transform_bitRate:d.bitRate,transform_sampleRate:d.sampleRate},function(e){v.list.push(e),e.duration?r():/amr/i.test(e.mime)?(t++,i()):p("微信服务器返回了未知音频类型:"+e.mime)},function(e){p("下载音频失败:"+e)})}},s=0,a=function(){if(s>=n.length)return v.uploadTime=Date.now()-c,void i();var e=n[s];console.log("微信录音片段"+s+" wx.playVoice({localId:'"+e+"'})"),wx.uploadVoice({localId:e,isShowProgressTips:0,fail:p,success:function(e){var n=e.serverId;console.log("serverId:"+n),f.push(n),s++,a()}})},c=Date.now();a()},m=a.timeout;if(a.err)return console.error(a.err,m),void p("录制失败,已录制"+m.length+"分钟,但后面出错:"+a.err);if(m.length&&Date.now()-m[m.length-1].time<900)return c(),void t();s=0,a.wx.stopRecord({fail:p,success:function(e){var n=Date.now();m.push({res:e,duration:n-a.startTime,time:n}),t()}})}else p("未开始录音")}}(),function(){"use strict";var e=RecordApp.Platforms.Native,i=e.Config;e.IsInit=!0;var f=window.NativeRecordReceivePCM=window.top.NativeRecordReceivePCM=function(e,n){var t=f.rec;if(t){t._appStart||t.envStart(1,n),t._appStart=1;for(var o,r=atob(e),i=r.length,s=new Int16Array(i/2),a=0,c=0,u=0;u+2<=i;c++,u+=2)o=(r.charCodeAt(u)|r.charCodeAt(u+1)<<8)<<16>>16,s[c]=o,a+=Math.abs(o);t.envIn(s,a)}else console.error("未开始录音,但收到Native PCM数据")};e.RequestPermission=function(e,n){i.JsBridgeRequestPermission(e,n)},e.Start=function(e,n,t){f.param=e,f.rec=Recorder(e),i.JsBridgeStart(e,n,t)},e.Stop=function(o,r){i.JsBridgeStop(function(){var n=f.rec;if(f.rec=null,n){console.log("rec encode start: pcm:"+n.recSize+" src:"+n.srcSampleRate+" set:"+JSON.stringify(f.param));var t=function(){for(var e in n.set)f.param[e]=n.set[e]};n.stop(function(e,n){console.log("rec encode end"),t(),o(e,n)},function(e){t(),r(e)})}else r("未开始录音")},r)}}(); \ No newline at end of file +!function(){"use strict";var n=/MicroMessenger/i.test(navigator.userAgent),e=window.RecordAppBaseFolder||"/Recorder/dist/",t=window.OnRecordAppInstalled,u=[{Key:"Native",Support:function(e){l.AlwaysAppUseJS?e(!1):o.Config.IsApp(e)},CanProcess:function(){return!0},Config:{IsApp:function(e){e(!1)},JsBridgeRequestPermission:function(e,n){n("JsBridgeRequestPermission未实现")},JsBridgeStart:function(e,n,t){t("JsBridgeStart未实现")},JsBridgeStop:function(e,n){n("JsBridgeStop未实现")},paths:[{url:e+"app-support/app-native-support.js",check:function(){return!o.IsInit}}]},ExtendDefault:!0},{Key:"IOS-Weixin",Support:function(e){l.AlwaysUseWeixinJS||!Recorder.Support()?e(n):e(!1)},CanProcess:function(){return!1},Config:{WxReady:function(e){e(null,"未实现IOS-Weixin.Config.WxReady")},DownWxMedia:function(e,n,t){t("下载素材接口DownWxMedia未实现")},paths:[{url:e+"app-support/app-ios-weixin-support.js",check:function(){return!r.IsInit}},{url:e+"engine/beta-amr.js",check:function(){return!Recorder.prototype.amr}}]},ExtendDefault:!0},{Key:"Default",Support:function(e){e(!0)},CanProcess:function(){return!0},Config:{paths:[{url:e+"recorder-core.js",check:function(){return!window.Recorder}},{url:e+"engine/mp3.js",check:function(){return!Recorder.prototype.mp3}}]}}],o=u[0],r=u[1],f=u[2];f.RequestPermission=function(e,n){var t=Recorder();t.open(function(){t.close(),e()},n)},f.Start=function(e,n,t){var o=l.__Rec;null!=o&&o.close(),l.__Rec=o=Recorder({type:e.type||"mp3",sampleRate:e.sampleRate||16e3,bitRate:e.bitRate||16,onProcess:e.onProcess||function(){}}),o.appSet=e,o.open(function(){o.start(),n()},function(e){t(e)})},f.Stop=function(t,n){var o=l.__Rec;if(o){var r=function(){for(var e in o.close(),o.set)o.appSet[e]=o.set[e];l.__Rec=null};o.stop(function(e,n){r(),t(e,n)},function(e){r(),n(e)})}else n("未开始录音")};var l={LM:"2019-8-29 11:20:04",Current:0,IsWx:n,BaseFolder:e,AlwaysUseWeixinJS:!1,AlwaysAppUseJS:!1,Platforms:{Native:o,Weixin:r,Default:f},Js:function(r,i,s,e){var c=(e=e||window).document,a=function(e){if(e>=r.length)i();else{var n=r[e],t=n.url;if(!1!==n.check()){var o=c.createElement("script");o.setAttribute("type","text/javascript"),o.setAttribute("src",t),o.onload=function(){a(e+1)},o.onerror=function(e){s("请求失败:"+(e.message||"-")+","+t)},c.body.appendChild(o)}else a(e+1)}};a(0)},Install:function(t,o){var r=l.__reqs||(l.__reqs=[]);r.push({s:t,f:o}),t=function(){i("s",arguments)},o=function(e,n){i("f",arguments)};var i=function(e,n){for(var t=0;t=r.length)return v.decodeTime=Date.now()-u,void function(){c||(c=Date.now());for(var e=[],n=0;n=f.length)r();else{var e=f[t];g.DownWxMedia({mediaId:e,transform_mediaIds:f.join(","),transform_type:d.type,transform_bitRate:d.bitRate,transform_sampleRate:d.sampleRate},function(e){v.list.push(e),e.duration?r():/amr/i.test(e.mime)?(t++,i()):p("微信服务器返回了未知音频类型:"+e.mime)},function(e){p("下载音频失败:"+e)})}},s=0,c=function(){if(s>=n.length)return v.uploadTime=Date.now()-a,void i();var e=n[s];console.log("微信录音片段"+s+" wx.playVoice({localId:'"+e+"'})"),wx.uploadVoice({localId:e,isShowProgressTips:0,fail:p,success:function(e){var n=e.serverId;console.log("serverId:"+n),f.push(n),s++,c()}})},a=Date.now();c()},m=a.timeout;if(a.err)return console.error(a.err,m),void p("录制失败,已录制"+m.length+"分钟,但后面出错:"+a.err);if(m.length&&Date.now()-m[m.length-1].time<900)return R(),void t();c=0,a.wx.stopRecord({fail:p,success:function(e){var n=Date.now();m.push({res:e,duration:n-a.startTime,time:n}),t()}})}else p("未开始录音")}}(),function(){"use strict";var e=RecordApp.Platforms.Native,i=e.Config;e.IsInit=!0;var f=window.NativeRecordReceivePCM=window.top.NativeRecordReceivePCM=function(e,n){var t=f.rec;if(t){t._appStart||t.envStart(1,n),t._appStart=1;for(var o,r=atob(e),i=r.length,s=new Int16Array(i/2),c=0,a=0,u=0;u+2<=i;a++,u+=2)o=(r.charCodeAt(u)|r.charCodeAt(u+1)<<8)<<16>>16,s[a]=o,c+=Math.abs(o);t.envIn(s,c)}else console.error("未开始录音,但收到Native PCM数据")};e.RequestPermission=function(e,n){i.JsBridgeRequestPermission(e,n)},e.Start=function(e,n,t){f.param=e,f.rec=Recorder(e),i.JsBridgeStart(e,n,t)},e.Stop=function(o,r){i.JsBridgeStop(function(){var n=f.rec;if(f.rec=null,n){console.log("rec encode start: pcm:"+n.recSize+" src:"+n.srcSampleRate+" set:"+JSON.stringify(f.param));var t=function(){for(var e in n.set)f.param[e]=n.set[e]};n.stop(function(e,n){console.log("rec encode end"),t(),o(e,n)},function(e){t(),r(e)})}else r("未开始录音")},r)}}(); \ No newline at end of file diff --git a/src/app-support/app-ios-weixin-support.js b/src/app-support/app-ios-weixin-support.js index 4d71550..a7c8fe4 100644 --- a/src/app-support/app-ios-weixin-support.js +++ b/src/app-support/app-ios-weixin-support.js @@ -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; @@ -41,12 +67,18 @@ 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){ @@ -54,11 +86,8 @@ platform.Start=function(set,success,fail){ return; }; if(isStart){ - console.log("正在录音,正在结束后重试"); - stopNow(function(){ - setTimeout(function(){ - platform.Start(set,success,fail); - },300); + killStart(function(){ + platform.Start(set,success,fail); }); return; }; @@ -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; @@ -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 }); //监听超时自动停止后接续录音