MediaStreamRecorder和TypeScript Uncaught TypeError

时间:2015-09-28 13:01:36

标签: javascript html5 audio typescript media

我目前正在开发一款使用用户PC麦克风的录音机,工作正常。但是,当我想录制音频时,我得到这种类型的错误:

  

audioHandler.ts:45未捕获TypeError:MediaStreamRecorder不是函数

这是代码:

/// <reference path="../references.d.ts" />
//This component handles all things audio related

export = Scaut.AudioHandler;

module Scaut.AudioHandler {

    var n = <any>navigator;
    var stream = <any>"";
    var stopRecording = <any>"";
    var mediaRecorder = <any>"";
    //var MediaStreamRecorder = <any>""; 

    //Check if microphone is ok
    export function hasGetUserMedia() {
        n.getUserMedia = n.getUserMedia ||
        n.webkitGetUserMedia ||
        n.mozGetUserMedia;

        if (n.getUserMedia) {
            n.getUserMedia({ audio: true},
                function (stream) {
                    var audio = <any>"";
                    audio = document.getElementById('audioRecord');
                    audio.src = window.URL.createObjectURL(stream);
                    audio.onloadedmetadata = function (e) {
                        audio.play();
                    };
                },
                function (err) {
                    alert("The following error occured: " + err.name);
                }
            );
        } else {
            alert("getUserMedia not supported");
        }

        recordAudio(stream);
    }

    //Record audio
    export function recordAudio(stream) {
        mediaRecorder = new MediaStreamRecorder(stream);
        console.log("Stream: "+stream)
        mediaRecorder.mimeType = 'audio/ogg';
        mediaRecorder.audioChannels = 1;
        mediaRecorder.ondataavailable = function (blob) {
            // POST/PUT "Blob" using FormData/XHR2
            var blobURL = URL.createObjectURL(blob);
            //document.write('<a href="' + blobURL + '">' + blobURL + '</a>');
            console.log("Blob: "+blob)
            console.log("BlobUrl: "+blobURL)
        };
        mediaRecorder.start(3000);
    }

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

看起来您的错误是在编译时(因为错误消息指向您的.ts文件)。

错误意味着编译不知道MediaStreamRecorder类型,我假设您从JavaScript库导入。

要清除错误,您可以使用以下类似的代码为它做一个简单的类型定义...

declare var MediaStreamRecorder: any;

这将清除错误,但不会为该类型提供任何进一步的类型检查或自动完成。 You can add more detail to your type definition如果您需要,可以改善这种情况。