当用户在我的网站上录制音频文件时,能够读取(内置)麦克风的硬件信息(至少是名称)吗?
这在JavaScript中是可能的,还是有其他方法来解决这个问题?我搜索了网络,但只能找到用JavaScript录制的脚本。
发布于 2015-08-01 05:29:27
更新版本的:在火狐,微软边缘,和Chrome 45的实验标志。
使用标准的navigator.mediaDevices.enumerateDevices()
,您可以获得可用源的列表。每个源都有一个kind
属性以及一个label
。
var stream;
navigator.mediaDevices.getUserMedia({ audio:true })
.then(s => (stream = s), e => console.log(e.message))
.then(() => navigator.mediaDevices.enumerateDevices())
.then(devices => {
stream && stream.stop();
console.log(devices.length + " devices.");
devices.forEach(d => console.log(d.kind + ": " + d.label));
})
.catch(e => console.log(e));
var console = { log: msg => div.innerHTML += msg + "<br>" };
<div id="div"></div>
文档及相关的
navigator.mediaDevices
on MDN - https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices最后一个演示是在普通Chrome中运行的,这要归功于adapter.js的填充。
发布于 2015-07-31 21:45:33
过时API
这个答案使用的API是非标准的,并且浏览器支持有限.在编写时,它在当前的Chrome中工作,但不会在其他浏览器的未来版本中采用,而且可能会在Chrome中消失。有关更广泛支持的解决方案,请参见:https://stackoverflow.com/a/31758598/610573
使用MediaStreamTrack.getSources()
,您可以获得可用资源的列表。每个源都有一个kind
属性以及一个label
。
MediaStreamTrack.getSources(function(sourceInfos) {
for (var i = 0; i != sourceInfos.length; ++i) {
var thisSource = sourceInfos[i];
console.log('stream type: '+thisSource.kind+', label: '+thisSource.label);
// example: stream type: audio, label: internal microphone
}
});
文档及相关的
MediaStreamTrack
on MDN - https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrackhttps://stackoverflow.com/questions/31754529
复制相似问题