UNPKG

unserver-unify

Version:

127 lines (126 loc) 3.57 kB
angular.module('bamboo.course').controller('PoetryChapterCtrl', function($scope, $timeout, $stateParams, ApiService) { this.cid = $stateParams.cid; console.log("--- PoetryChapterCtrl"); var self = this; var resUrl = this.resUrl = ApiService.SHOST + '/lcourse/' + this.cid + '/resources/'; this.index = 0; this.mode = 'brief'; this.init = function(chapter) { // console.log(chapter); // self.chapter = {}; // $timeout(function() { self.chapter = chapter || {}; resUrl = self.resUrl = ApiService.SHOST + '/lcourse/' + self.chapter.cid + '/resources/'; angular.forEach(self.chapter.ppts, function(item) { var prons = item.pronounce.split(' ').join(' ').split(' '); if (!prons[0]) { prons.shift(); } var chars = item.text.split(''); for (var i = 0; i < chars.length; i++) { if (chars[i] == ',' || chars[i] == ',') { prons.splice(i, 0, '') } } // console.log(chars); // console.log(prons); item.chars = chars; item.prons = prons; console.log(item); }) self.playAll(); } this.hints = { 'explain': '解释', 'example': '例句', 'english': '英文', 'similar': '同义词', 'source': '来源', 'opposite': '反义词', } this.currentPPT; this.code; var myAudio; var playoutTimer; this.index = 0; $scope.$on("$destroy", function() { self.index = 999; if (myAudio) { myAudio.pause(); } }); function playNext() { console.log(self.index,self.chapter.ppts.length); if (self.index < self.chapter.ppts.length) { var file = self.chapter.ppts[self.index].audio; self.playAudioFile(file, function() { playoutTimer = $timeout(playNext, 100); }); self.currentPPT = self.chapter.ppts[self.index]; } else { console.log("-- doing nothing --"); if (playoutTimer) { myAudio.pause(); $timeout.cancel(playoutTimer); playoutTimer = null; } } self.index++; } this.playAll = function() { self.playAudioFile(self.chapter.options.name.audio, function() { self.index = 0; console.log(" play all"); playNext() }); } this.togglePPTAudio = function(ppt) { if (ppt.playFlag) { ppt.playFlag = false; try { if (myAudio) { myAudio.pause(); } } catch (e) {} } else { angular.forEach(self.chapter.ppts, function(_ppt) { _ppt.playFlag = false; }) ppt.playFlag = true; try { if (!myAudio) { myAudio = new Audio(resUrl + ppt.audio); } else { myAudio.pause(); myAudio.src = resUrl + ppt.audio; } // myAudio = new Audio(resUrl + ppt.audio); myAudio.addEventListener("ended", function() { $timeout(function() { ppt.playFlag = false; }, 10); }); myAudio.play(); } catch (e) {} } } this.playAudioFile = function(file, callback) { if (!file) { return; } try { if (!myAudio) { myAudio = new Audio(resUrl + file); } else { myAudio.pause(); myAudio.src = resUrl + file; } // if (myAudio) { // myAudio.pause(); // } // myAudio = new Audio(resUrl + file); myAudio.addEventListener("ended", callback); myAudio.play(); } catch (e) {} } });