unserver-unify
Version:
273 lines (254 loc) • 6.66 kB
JavaScript
angular.module('bamboo.course').controller('IdiomChapterCtrl', function($scope, $timeout, $stateParams, ApiService) {
this.cid = $stateParams.cid;
console.log("--- IdiomChapterCtrl");
var self = this;
var resUrl = this.resUrl = ApiService.SHOST + '/lcourse/' + this.cid + '/resources/';
this.index = 0;
this.init = function(chapter) {
// console.log(chapter);
// self.chapter = {};
// $timeout(function() {
self.chapter = chapter||{};
resUrl = self.resUrl = ApiService.SHOST + '/lcourse/' + self.chapter.cid + '/resources/';
self.chapter.mainppts = [];
self.chapter.extppts = [];
angular.forEach(self.chapter.ppts, function(item) {
if (item.ext) {
self.chapter.extppts.push(item);
} else {
self.chapter.mainppts.push(item);
}
})
// console.log(self.chapter);
if (!self.chapter.size) {
self.chapter.size = 2;
}
//if(self.chapter.audio){
self.playChapterAudio();
/* }else{
$timeout(playNext, 100);
} */
// }, 100)
}
this.mode = 'all';
this.hints = {
'explain': '解释',
'example': '例句',
'english': '英文',
'similar': '同义词',
'source': '来源',
'opposite': '反义词',
}
this.currentPPT;
this.code;
var myAudio;
var playoutTimer;
this.playWord = function(word) {
var file = self.chapter.libs[word];
if (!file) {
return;
}
var filename = resUrl + file;
try {
if (myAudio) {
myAudio.pause();
myAudio.src=filename;
}else{
myAudio = new Audio(filename);
}
myAudio.play();
} catch (e) {}
}
this.index = 0;
$scope.$on("$destroy", function() {
self.index = 999;
if (myAudio) {
myAudio.pause();
}
});
function playNext() {
console.log(self.index);
if (self.index < self.chapter.ppts.length) {
var word = self.chapter.ppts[self.index].word;
console.log(word);
self.playWord(word);
self.currentPPT = self.chapter.ppts[self.index];
if (playoutTimer) {
$timeout.cancel(playoutTimer);
playoutTimer = null;
}
playoutTimer = $timeout(playNext, 3500);
}
self.index++;
}
this.playAll = function() {
self.index = 0;
playNext();
}
function shootAudio(callback) {
var audioObj = {
audio: true,
// video: true,
}
if (myAudio) {
myAudio.pause();
}
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");
}
}
this.recordingMode = 'idle';
var _stream;
this.recordAudio = function() {
self.index = 999;
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();
self.recordingMode = "recording";
}, 200)
});
}
this.playbacksrc;
this.stopRecording = function() {
self.index = 999;
self.recordingMode = 'idle';
window.audioVideoRecorder.stopRecording(function(url) {
// console.log(_stream);
// _stream.stop();
$timeout(function() {
self.playbacksrc = url;
}, 500);
});
}
this.playUserAudio = function() {
if (!self.playbacksrc) {
return;
}
self.index = 999;
try {
if (myAudio) {
myAudio.pause();
myAudio.src=self.playbacksrc;
}else{
myAudio = new Audio(self.playbacksrc);
}
myAudio.play();
} catch (e) {}
}
this.playChapterAudio = function() {
self.index = 999;
if (!self.chapter.audio) {
self.index = 0;
playNext();
return;
}
var filename = resUrl + self.chapter.audio;
try {
if (myAudio) {
myAudio.pause();
myAudio.src=filename;
}else{
myAudio = new Audio(filename);
}
myAudio.play();
} catch (e) {}
}
this.playAudio = function(charactor, ppt) {
self.currentPPT = ppt;
console.log(charactor, ppt);
self.index = 100;
if (!ppt || !ppt.word) {
return;
}
console.log(ppt);
var word = ppt.word;
self.charactor = charactor;
self.code = charactor.charCodeAt(0).toString(16);
// var file = self.chapter.libs[ppt.word];
if (self.mode == 'single') {
if (!charactor) {
return;
}
word = charactor;
}
self.playWord(word);
};
this.get24Placement = function(index) {
var remainder = index % 2;
if (remainder == 0) {
return 'right';
} else if (remainder == 1) {
return 'left';
}
}
this.get34Placement = function(index) {
var remainder = index % 3;
if (remainder == 0) {
if (index < 3) {
return 'bottom-left';
} else {
return 'right';
}
} else if (remainder == 1) {
if (index < 3) {
return 'bottom-right';
} else {
return 'left';
}
} else if (remainder == 2) {
if (index < 3) {
return 'bottom-right';
} else {
return 'left';
}
}
}
this.get44Placement = function(index) {
var remainder = index % 4;
if(index>15){
return 'top';
}
if (remainder == 0) {
if (index < 4) {
return 'bottom-left';
} else {
return 'right';
}
} else if (remainder == 1) {
if (index < 4) {
return 'bottom-left';
} else {
return 'right';
}
} else if (remainder == 2) {
if (index < 4) {
return 'bottom-right';
} else {
return 'left';
}
} else if (remainder == 3) {
if (index < 4) {
return 'bottom-right';
} else {
return 'left';
}
}
}
});