gpt4 book ai didi

javascript - MediaStreamRecorder 和 TypeScript 未捕获的类型错误

转载 作者:行者123 更新时间:2023-11-27 23:49:41 24 4
gpt4 key购买 nike

我目前正在开发一个使用用户 PC 麦克风的录音机,效果很好。但是,当我想录制音频时,我收到此类型错误:

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

这是代码:

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

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

最佳答案

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

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

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

declare var MediaStreamRecorder: any;

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

关于javascript - MediaStreamRecorder 和 TypeScript 未捕获的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32823663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com