UNPKG

unserver-unify

Version:

209 lines (197 loc) 5.8 kB
angular.module('bamboo.course').controller('ReadingChapterCtrl', function($scope, $timeout, $stateParams, ApiService) { this.cid = $stateParams.cid; console.log("--- WordChapterCtrl"); var self = this; var resUrl = ApiService.SHOST + '/lcourse/' + this.cid + '/resources/'; this.init = function(chapter) { self.chapter = chapter; resUrl = ApiService.SHOST + '/lcourse/' + self.chapter.cid + '/resources/'; self.readingImageUrl = ApiService.SHOST + "/public/" + ApiService.RES + '/lcourse/' + self.chapter.cid + '/resources/'; console.log(self.chapter); self.Bishun = []; if (self.chapter.ppts && self.chapter.ppts[0] && self.chapter.lang=='eng') { showAwsPPT(); } } // this.currentIndex; /* this.playText = function(text, index) { self.currentIndex = index; if (self.chapter.libs && self.chapter.libs[text]) { self.playAudio(self.chapter.libs[text]); } }*/ var myAudio; this.audioStatus="idle"; function audioEnded(){ console.log("audio ended"); $timeout(function(){ self.audioStatus="idle"; },100) } this.stopPlay=function(){ try { if (myAudio) { myAudio.pause(); self.audioStatus="idle"; } } catch (e) {} } this.currentIndex=-1; this.playAudio = function(file,index) { console.log('play',index); if (!file) { return; } var filename = resUrl + file; self.currentIndex=index; try { if(!myAudio){ myAudio = new Audio(filename); }else{ myAudio.pause(); myAudio.src=filename; } // myAudio = new Audio(filename); myAudio.play(); myAudio.addEventListener("ended",audioEnded); self.audioStatus='play'; } catch (e) {} }; this.toggolePlay=function(file,index){ if(self.audioStatus!='play'||index!=self.currentIndex){ self.playAudio(file,index); }else{ self.stopPlay(); } } this.fullTexts = []; this.onEnterCuePoint2 = function(currentTime, timeLapse, params) { // console.log("onEnterCuePoint", currentTime, timeLapse, params); self.currIndex = params.index; } this.replay = function(index) { console.log(index); var time = (self.chapter.ppts[0].data[index].time - 100) / 1000; self.API.seekTime(time, false); self.API.play(); } self.API; this.onPlayerReady = function(API) { self.API = API; }; this.videoReady = false; function showAwsPPT() { var ppt = self.chapter.ppts[0]; var cues = []; self.mp4config = { sources: [{}], cuePoints: {}, }; self.mp4config.sources[0].src = resUrl + ppt.audio; self.mp4config.sources[0].type = 'audio/mp3'; angular.forEach(ppt.data, function(item, index) { console.log(item); var cue = { timeLapse: { start: (item.time + 50) / 1000, }, onEnter: self.onEnterCuePoint2.bind(self), params: { index: index, } } /* if (ppt.data[index + 1]) { cue.timeLapse.end = ppt.data[index + 1].time } */ cues.push(cue); self.fullTexts.push(item.value); }) console.log(cues); self.mp4config.cuePoints.textCues = cues; console.log(self.mp4config); } this.mode; function shootAudio(callback) { var audioObj = { audio: true, // video: true, } if (myAudio) { myAudio.pause(); self.audioStatus='idle'; } var errBack = function(error) { return callback(false, error); }; navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia); if (navigator.getUserMedia) { // Standard navigator.getUserMedia(audioObj, function(stream) { console.log("audio detected"); callback(stream); }, errBack); } else { console.log("--- not supported---"); return callback(false, "Not Supported Browser"); } } var _stream; this.recordAudio = function(ppt) { this.mode = "recording"; shootAudio(function(stream) { _stream = stream; $timeout(function() { window.audioVideoRecorder = window.RecordRTC(stream, { type: 'audio', // sampleRate:22050, // numberOfAudioChannels:1, // mimeType: 'video/webm', // or video/mp4 or audio/ogg }); window.audioVideoRecorder.startRecording(); ppt.mode = "recording"; }, 200) }); } this.playbacksrc; this.stopRecording = function(ppt) { ppt.mode = 'idle'; this.mode = null; window.audioVideoRecorder.stopRecording(function(url) { // console.log(_stream); // _stream.stop(); $timeout(function() { ppt.playbacksrc = url; }, 500); }); } this.currentRecIndex=-1; this.playUserAudio = function(ppt,index) { if (!ppt.playbacksrc) { return; } self.currentRecIndex=index; try { if(!myAudio){ myAudio = new Audio(ppt.playbacksrc); }else{ myAudio.pause(); myAudio.src=ppt.playbacksrc; } // myAudio = new Audio(ppt.playbacksrc); myAudio.play(); myAudio.addEventListener("ended",audioEnded); self.audioStatus='playrec'; } catch (e) {} } this.toggoleRec=function(ppt,index){ if(self.audioStatus!='playrec'||index!=self.currentRecIndex){ self.playUserAudio(ppt,index); }else{ self.stopPlay(); } } $scope.$on("$destroy", function() { if (myAudio) { myAudio.pause(); } }); });