将blob MIME类型设置为wav仍会导致webM [英] Setting blob MIME type to wav still results in webM
问题描述
我关注了这篇文章(直到第6步的代码)制作一个录音机,如下所示.生成audioBlob
后,我调用一个自定义函数来上传blob.此功能适用于其他文件类型.
I followed this article (the code up to step 6) to make an audio recorder as below. After making the audioBlob
, I call a custom function to upload the blob. This function works for other file types.
我将{ type: 'audio/wav' }
传递给Blob
构造函数.生成的文件确实假装为wave文件,可以在浏览器中正常播放,但不能在iOS上播放.我与 http://checkfiletype.com/进行了检查,发现文件实际上是WebM:
I pass { type: 'audio/wav' }
to the Blob
constructor. The resulting file indeed pretends to be a wave file, and would play fine in the browser, but not on iOS. I checked with http://checkfiletype.com/ and discovered the file was actually WebM:
如何确保文件实际上是.wav
文件?
How do I ensure the file is in fact a .wav
file?
该文章中编辑的代码:
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
const mediaRecorder = new MediaRecorder(stream);
mediaRecorder.start();
const audioChunks = [];
mediaRecorder.addEventListener("dataavailable", event => {
audioChunks.push(event.data);
});
mediaRecorder.addEventListener("stop", () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
// Custom upload function
uploadFile(audioBlob);
});
setTimeout(() => {
mediaRecorder.stop();
}, 3000);
});
推荐答案
如何确保文件实际上是.wav文件?
How do I ensure the file is in fact a .wav file?
非常简单,我所知没有浏览器不支持原生导出到 wav .因此,您可以确定您的文件不是wav文件.
Quite easy, no browsers (I know of) does support exporting to wav natively. So you can be sure your file is not a wav file.
Blob构造函数的type
选项仅表示浏览器应如何设置MIME类型以满足进一步的需求(例如何时将其发送到服务器).
但这实际上并不会更改您作为第一个参数传递的内容.
The type
option of the Blob constructor only says the browser how it should set the MIME-type for further needs (e.g when it will send it to a server).
But it doesn't actually change the content you passed as the first argument.
因此,它不会神奇地将您的webm文件转换为其他格式.
So it won't magically transform your webm file to an other format.
现在,您确实有一些选项更改MediaRecorder将其内容输出的格式,但是所支持的格式/编解码器取决于实现,而且我认为除 opus 之外,没有其他浏览器支持任何其他功能,在 webm 容器或 ogg *一个(仅* FF)中.
Now, you do have some options to change the format to which the MediaRecorder will output its content, but the set of supported formats / codecs is up to the implementations, and I don't think any browser supports anything else than opus, either in a webm container or in an ogg * one (* FF only).
[edit]:实际上是Chrome现在在webm中支持PCM .
感谢 isTypeSupported 方法,您可以很好地设置代码以使用这种格式(如果有的话),但是我个人对此并没有太大的希望,并且会
Thanks to the isTypeSupported method, you can very well setup your code to use this format if it ever becomes available, but I personally wouldn't grow too much hope on this, and would look somewhere else if it is required to output a wav file...
const mime = ['audio/wav', 'audio/mpeg', 'audio/webm', 'audio/ogg']
.filter(MediaRecorder.isTypeSupported)[0];
const recorder = new MediaRecorder(stream, {
mimeType: mime
});
这篇关于将blob MIME类型设置为wav仍会导致webM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!