Commit 6169f278bdf34ac822d2cb19eb9444ed6d44c721

Authored by 商艳涛
1 parent fc980668

修复浏览器无法自动播放语音

src/gxb-ide/CodingEditer.js
... ... @@ -94,7 +94,7 @@
94 94 * 播放代码
95 95 * Created by Kira on 2017-02-08
96 96 */
97   - playCoding: function(){
  97 + playCoding: function(e){
98 98 CodingEditer.editable(1);
99 99 CodingEditer.flag = false;
100 100 if (CodingEditer.firstPlay == 0) {
... ... @@ -115,30 +115,36 @@
115 115 var playTotalTime = 0;
116 116  
117 117 $('#recordertab a:eq(1)').tab('show');
  118 + // 处理浏览器无法自动播放语音时候,编辑器显示空内容
  119 + if (e && !e.status) {
  120 + var timeoutcontroller = null;
  121 + CodingEditer.recordtimeoutcontrolls.push(timeoutcontroller);
  122 + } else {
  123 + for (var i = 0; i < playbackrecord.length; i++) {
  124 + if (playbackrecord[i].trigger === 1) {
  125 + playTotalTime += parseInt(playbackrecord[i].intervaltime);
  126 + }else if (playbackrecord[i].trigger === 0) {
  127 + var timeoutcontroller = null;
  128 + var replayinterval = playbackrecord[i].intervaltime;
  129 +
  130 + if (!CodingEditer.isPause) {
  131 + starttime = replayinterval - (CodingEditer.audioTime - playTotalTime);
  132 + CodingEditer.isPause = true;
  133 + }else{
  134 + // if (i == 0) {
  135 + // starttime = replayinterval;
  136 + // }else{
  137 + // starttime = parseInt(starttime) + parseInt(replayinterval);
  138 + // }
  139 + starttime = parseInt(starttime) + parseInt(replayinterval);
  140 + }
118 141  
119   - for (var i = 0; i < playbackrecord.length; i++) {
120   - if (playbackrecord[i].trigger === 1) {
121   - playTotalTime += parseInt(playbackrecord[i].intervaltime);
122   - }else if (playbackrecord[i].trigger === 0) {
123   - var timeoutcontroller = null;
124   - var replayinterval = playbackrecord[i].intervaltime;
125   -
126   - if (!CodingEditer.isPause) {
127   - starttime = replayinterval - (CodingEditer.audioTime - playTotalTime);
128   - CodingEditer.isPause = true;
129   - }else{
130   - // if (i == 0) {
131   - // starttime = replayinterval;
132   - // }else{
133   - // starttime = parseInt(starttime) + parseInt(replayinterval);
134   - // }
135   - starttime = parseInt(starttime) + parseInt(replayinterval);
  142 + timeoutcontroller = setTimeout(changeValueCallback(playbackrecord[i]), starttime/CodingEditer.speed);
  143 + CodingEditer.recordtimeoutcontrolls.push(timeoutcontroller);
136 144 }
137   -
138   - timeoutcontroller = setTimeout(changeValueCallback(playbackrecord[i]), starttime/CodingEditer.speed);
139   - CodingEditer.recordtimeoutcontrolls.push(timeoutcontroller);
140 145 }
141 146 }
  147 +
142 148 console.log(CodingEditer.recordtimeoutcontrolls)
143 149 },
144 150 pauseCode: function(){
... ...
src/gxb-ide/Dir.js
... ... @@ -647,9 +647,9 @@
647 647 imagePath: '../js/audioPlayer/image',
648 648 debuggers: false,
649 649 allowSeek: false,
650   - playCallback: function() { // 播放
  650 + playCallback: function(e) { // 播放
651 651 console.log('++++++++');
652   - CodingEditer.playCoding();
  652 + CodingEditer.playCoding(e);
653 653 },
654 654 pauseCallback: function() { // 暂停
655 655 CodingEditer.audioTime = parseInt(player.elements.audioDom.currentTime*1000);
... ...
src/js/audioPlayer/jQuery.AudioPlayer.js
... ... @@ -319,13 +319,20 @@
319 319 // Set the playback / 设置播放
320 320 ,play: function(o) {
321 321 var _this = this;
322   - _this.elements.audioDom.play();
323   - if (typeof o == 'function') {
324   - o({'status': _this.elements.audioDom.play});
325   - }
  322 + var canPlay = _this.elements.audioDom.play();
  323 + // 处理浏览器无法自动播放语音
  324 + if (canPlay !== undefined) {
  325 + canPlay.then(_ => {
  326 + if (typeof o == 'function') {
  327 + o({'status': true});
  328 + }
326 329  
327   - if(typeof _this.options.playCallback == 'function'){
328   - _this.options.playCallback({'status': _this.elements.audioDom.play});
  330 + if(typeof _this.options.playCallback == 'function'){
  331 + _this.options.playCallback({'status': true});
  332 + }
  333 + }).catch(error => {
  334 + _this.options.playCallback({'status': false});
  335 + });
329 336 }
330 337 }
331 338 // Set the suspend / 设置暂停
... ...