0

对不起我的英语不好。我正在构建一个使用网络摄像头录制视频的 AngularJS 应用程序(使用 1.2.0 RC2)。我使用 RecordRTC录制视频流。我得到了例子。当我使用一个流时,一切正常。但我不明白,如何在我的控制器中使用这些功能与承诺。

function startRecord(time,text,id){
        var deferred = $q.defer();
        navigator.getMedia = ( navigator.getUserMedia ||
            navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia ||
            navigator.msGetUserMedia);

        navigator.getMedia(
            {
                audio: true,
                video: video_constraints
            }, function(stream) {
                    preview.src = window.URL.createObjectURL(stream);
                    preview.play();
                    recordAudio = RecordRTC(stream, {});
                    recordVideo = RecordRTC(stream, {
                        type: 'video'
                    });
                    recordAudio.startRecording();
                    recordVideo.startRecording();

                    $timeout(function() {
                       saveRecord(id).then(function(data){
                            if(data.status===200){
                                deferred.resolve();
                                $log.info("resolve");
                            }else{
                                deferred.reject();
                                $log.info("reject");
                            }
                        })
                    }, time*1000);

                    $scope.$apply(function(){
                        $timeout(function() {
                            addtext(text);
                        }, 100);

                    })
                });
      return deferred.promise;
    }

    var fileName;

    function saveRecord(id) {
        fileName = Math.round(Math.random() * 99999999) + 99999999;
        recordAudio.stopRecording();

        $scope.save(recordAudio.getBlob(), 'audio', fileName + '.wav',id);

        recordVideo.stopRecording();
        var save_promise = $scope.save(recordVideo.getBlob(), 'video', fileName + '.webm',id);
        return save_promise;
    };

    $scope.save = function(blob,fileType,fileName,id) {
        var save_promise=  $http({
            method: 'PUT',
            url: "/api/videos/"+id,
            headers: { 'Content-Type': false },
            transformRequest: function (data) {
                var formData = new FormData();
                formData.append(fileType, fileName);
                formData.append(fileType + '-blob', blob,fileName);

                return formData;
            },
            data: { model: fileName, files: '' }

        });
        return save_promise;
    };

我可以创建手动链接承诺

var main = $q.defer();
main.promise
        .then(function(questions){
            startRecord(questions[0].time,questions[0].text,1)
                                .then(function(){
                                    startRecord(questions[1].time,questions[1].text,2)
                                        .then(function(){
                                            startRecord(questions[2].time,questions[2].text,3)
                                    })
                                })
                       });

main.resolve(问题);

但是我从我的 api 得到的数组问题可以是不同的长度。

var questions = [
        {id:1,time:10,text:'who are you?'},
        {id:2,time:15,text:'where are do you live?'},
        {id:3,time:15,text:'why  do you live?'},
    ]

我尝试了不同的变体,但我的 JS 水平非常低。

4

1 回答 1

0

我找到了适合我情况的例子。一切正常。

 var temp =  $q.when({});
 var questions = [
        {id:1,time:10,text:'who are you?'},
        {id:2,time:15,text:'where are do you live?'},
        {id:3,time:15,text:'why  do you live?'},

    ]

questions.forEach(function(value) {
   temp = temp.then(
        function(){
              return startRecord(value.time,value.text,value.id);
        }
    );
}
于 2013-11-01T12:54:39.387 回答