From b2c8ab55efa0d59694a2eacb6646b8cf1ec3c006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E5=9D=9A=E6=9E=9C?= <753610399@qq.com> Date: Wed, 4 Sep 2019 13:31:39 +0800 Subject: [PATCH] =?UTF-8?q?RecordApp=20Default=E4=BF=9D=E6=8C=81Stream?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=EF=BC=8C=E5=85=8D=E5=BE=97Start=E6=97=B6?= =?UTF-8?q?=E5=86=8D=E6=AC=A1=E5=8F=91=E8=B5=B7=E6=8E=88=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app-support-sample/README.md | 4 ++-- app-support-sample/index.html | 12 +++++++----- dist/app-support/app.js | 2 +- src/app-support/app.js | 29 ++++++++++++++++++----------- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/app-support-sample/README.md b/app-support-sample/README.md index df5fcf1..eaa1339 100644 --- a/app-support-sample/README.md +++ b/app-support-sample/README.md @@ -168,7 +168,7 @@ IOS其他浏览器|| # :open_book:方法文档 ## 【静态方法】RecordApp.RequestPermission(success,fail) -请求录音权限,如果当前环境不支持录音或用户拒绝将调用错误回调,调用`RecordApp.Start`前需先至少调用一次此方法。 +请求录音权限,如果当前环境不支持录音或用户拒绝将调用错误回调;调用`RecordApp.Start`前需先至少调用一次此方法;请求权限后如果不使用了,至少要调用一次`Stop`来清理资源。 `success`: `fn()` 有权限时回调 @@ -200,7 +200,7 @@ IOS-Weixin底层会把从微信素材下载过来的原始音频信息存储在s ## 【静态方法】RecordApp.Stop(success,fail) -结束录音。 +结束录音和清理资源。 `success`: `fn(blob,duration)` 结束录音时回调,`blob:Blob` 录音数据`audio/mp3|wav...`格式,`duration`: `123` 音频持续时间。 diff --git a/app-support-sample/index.html b/app-support-sample/index.html index ad8da38..8110c31 100644 --- a/app-support-sample/index.html +++ b/app-support-sample/index.html @@ -418,14 +418,16 @@ return; }; - if(!curSet){ - call("请先开始录音"); - return; - }; var setData=curSet; curSet=null; - reclog(RecordApp.Current.Key+"正在结束"+setData.type+"..."); + if(!setData){ + //没有开始?不管,stop清理资源 + setData={}; + }else{ + reclog(RecordApp.Current.Key+"正在结束"+setData.type+"..."); + }; + RecordApp.Stop(function(blob,time){ endCall("",blob,time); diff --git a/dist/app-support/app.js b/dist/app-support/app.js index 289f071..9e56fbb 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 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 +!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=l.__Rec;t&&(t.close(),l.__Rec=null),Recorder().open(function(){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)return Recorder().close(),void n("未开始录音");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)})};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.js b/src/app-support/app.js index d335553..9831a9c 100644 --- a/src/app-support/app.js +++ b/src/app-support/app.js @@ -167,16 +167,22 @@ var Weixin=Config_SupportPlatforms[1]; var Default=Config_SupportPlatforms[2]; //给Default实现统一接口 Default.RequestPermission=function(success,fail){ + var old=App.__Rec; + if(old){ + old.close(); + App.__Rec=null; + }; + var rec=Recorder(); rec.open(function(){ - rec.close(); + //rec.close(); keep stream Stop时再关,免得Start再次请求权限 success(); },fail); }; Default.Start=function(set,success,fail){ var appRec=App.__Rec; if(appRec!=null){ - appRec.close(); + appRec.close();//stream已经被使用过了,close更好 }; App.__Rec=appRec=Recorder({ type:set.type||"mp3" @@ -196,6 +202,7 @@ Default.Start=function(set,success,fail){ Default.Stop=function(success,fail){ var appRec=App.__Rec; if(!appRec){ + Recorder().close();//不管有没有,关闭 fail("未开始录音"); return; }; @@ -363,14 +370,14 @@ fail:fn(errMsg,isUserNotAllow) 没有权限或者不能录音时回调,如果 cur.RequestPermission(function(){ console.log("录音权限请求成功"); - success(); + success&&success(); },function(errMsg,isUserNotAllow){ console.log("录音权限请求失败:"+errMsg+",isUserNotAllow:"+isUserNotAllow); - fail(errMsg,isUserNotAllow); + fail&&fail(errMsg,isUserNotAllow); }); },function(err){ console.log("Install失败",err); - fail(err); + fail&&fail(err); }); } /* @@ -389,7 +396,7 @@ fail:fn(errMsg) 开启录音出错时回调 ,Start:function(set,success,fail){ var cur=App.Current; if(!cur){ - fail("需先调用RequestPermission"); + fail&&fail("需先调用RequestPermission"); return; }; set||(set={}); @@ -404,10 +411,10 @@ fail:fn(errMsg) 开启录音出错时回调 }; cur.Start(set,function(){ console.log("开始录音",set); - success(); + success&&success(); },function(msg){ console.log("开始录音失败:"+msg); - fail(msg); + fail&&fail(msg); }); } /* @@ -422,16 +429,16 @@ fail:fn(errMsg) 录音出错时回调 ,Stop:function(success,fail){ var cur=App.Current; if(!cur){ - fail("需先调用RequestPermission"); + fail&&fail("需先调用RequestPermission"); return; }; cur.Stop(function(blob,duration){ console.log("结束录音"+blob.size+"b "+duration+"ms",blob); - success(blob, duration); + success&&success(blob, duration); },function(msg){ console.log("结束录音失败:"+msg); - fail(msg); + fail&&fail(msg); }); }