我正在录制演讲并将其转换为可下载的WAV文件。我正在使用Angular6和MediaRecorder。首先,我获取blob,然后从blob中获取.wav文件。问题在于,WAV文件(可以播放且听起来不错)在过程中失去了很多属性,因此不是有效的WAV文件。它仍然是一个WebM文件。为了进一步处理,我需要真正有效且高质量的WAV文件。最终,我得到了大约20KB的文件,而不是大约300KB的更大文件。
我的代码如下:
我的代码如下:
//convert Blob to File. Pass the blob and the file title as arguments
var blobToFile = (theBlob: Blob, fileName:string): File => {
var b: any = theBlob;
//Add properties to the blob
b.lastModifiedDate = new Date();
b.name = fileName;
return <File>theBlob;
}
var browser = <any>navigator;
var headers = new Headers();
var audioCtx = new AudioContext();
var chunks =[];
var constraints = { audio: true, video: false };
var promisifiedOldGUM = function(constraints, successCallback, errorCallback) {
var getUserMedia = (browser.getUserMedia || browser.webkitGetUserMedia || browser.mozGetUserMedia || browser.msGetUserMedia);
// Some browsers just don't implement it - return a rejected promise with an error to keep a consistent interface
if(!getUserMedia) {
return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
}
// Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
return new Promise(function(successCallback, errorCallback) {
getUserMedia.call(browser, constraints, successCallback, errorCallback);
});
}
if (browser.mediaDevices.getUserMedia) {
browser.mediaDevices.getUserMedia(constraints).then((stream) => {
this.mediaRecorder = new MediaRecorder(stream);
this.mediaRecorder.onstop = function(){
var last_bit= chunks[chunks.length-1];
var blob = new Blob([last_bit], { 'type' : 'audio/wav' });
var audioURL = window.URL.createObjectURL(blob);
//convert Blob to file
var file = blobToFile(blob, "my-recording.wav");
var link = document.createElement("a");
link.href = audioURL;
link.download = 'audio_recording_' + new Date().getTime() + '.wav';
link.innerHTML = "download file";
document.body.appendChild(link);
};
在typings.d.ts
文件中,MediaRecorder的设置如下:
declare class MediaRecorder extends EventTarget {
// readonly mimeType: string;
readonly MimeType: 'audio/x-wav'; // 'audio/vnd.wav';
readonly state: 'inactive' | 'recording' | 'paused';
readonly stream: MediaStream;
ignoreMutedMedia: boolean;
videoBitsPerSecond: number;
audioBitsPerSecond: 16000//number;
ondataavailable: (event : MediaRecorderDataAvailableEvent) => void;
onerror: (event: MediaRecorderErrorEvent) => void;
onpause: () => void;
onresume: () => void;
onstart: () => void;
onstop: () => void;
constructor(stream: MediaStream);
start();
stop();
resume();
pause();
isTypeSupported(type: string): boolean;
requestData();
addEventListener<K extends keyof MediaRecorderEventMap>(type: K, listener: (this: MediaStream, ev: MediaRecorderEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof MediaRecorderEventMap>(type: K, listener: (this: MediaStream, ev: MediaRecorderEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
}
我不知道如何更改 blobToFile()
函数以在转换过程中保留录音质量。如果您能帮助我解决这个问题,我将非常感激。
MediaRecorder
。实际上,我得到的文件是webm
格式的。 - Emanuela Colta