William Holm Jacobsen
William Holm Jacobsen

Reputation: 55

MediaStreamRecorder and TypeScript Uncaught TypeError

I am currently developing an audio recorder that uses the users PC microphone, which works fine. However, when I want to record the audio I get this type error:

audioHandler.ts:45 Uncaught TypeError: MediaStreamRecorder is not a function

This is the code:

/// <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);
    }

Can anyone point me in the right direction?

Upvotes: 2

Views: 3757

Answers (1)

Fenton
Fenton

Reputation: 250972

It looks like your error is at compile time (as the error message points to your .ts file).

The error means that the compile is unaware of the MediaStreamRecorder type, which I am assuming you are importing from a JavaScript library.

To clear the error, you can make a simple type definition for it using the following like of code...

declare var MediaStreamRecorder: any;

This will clear the error, but won't give you any further type checking or auto-completion for the type. You can add more detail to your type definition to improve this situation if you need to.

Upvotes: 3

Related Questions