[英]How to record audio at mobile Safari and all other browsers with Javascript?
I simply want to record audio in all browsers, in desktop browsers as well as in mobile (iOS/Android) browsers. 我只想在所有浏览器,桌面浏览器以及移动(iOS / Android)浏览器中录制音频。
I have written some Javascript which is working in Chrome browser at desktop. 我写了一些Javascript,它在桌面上的Chrome浏览器中运行。 But when I try this on iPhone 7 with Safari/Chrome;
但是当我在带有Safari / Chrome的iPhone 7上试用它时; it is not working.
它不起作用。
As per my understanding, this has to do with the different mobile browser API's. 根据我的理解,这与不同的移动浏览器API有关。 So I need to change the code so that it is supporting all types of browser API's.
所以我需要更改代码,以便它支持所有类型的浏览器API。
How can I change the code so that is supporting all the browsers? 如何更改代码以支持所有浏览器?
'use strict'
var log = console.log.bind(console),
id = val => document.getElementById(val),
ul = id('ul'),
gUMbtn = id('gUMbtn'),
start = id('start'),
stop = id('stop'),
stream,
recorder,
counter = 1,
chunks,
media;
var mv = id('mediaVideo'),
mediaOptions = {
video: {
tag: 'video',
type: 'video/webm',
ext: '.mp4',
gUM: { video: true, audio: true }
},
audio: {
tag: 'audio',
type: 'audio/ogg',
ext: '.ogg',
gUM: { audio: true }
}
}
media = mediaOptions.audio;
navigator.mediaDevices.getUserMedia(media.gUM).then(_stream => {
stream = _stream;
id('btns').style.display = 'inherit';
start.removeAttribute('disabled');
recorder = new MediaRecorder(stream);
recorder.ondataavailable = e => {
chunks.push(e.data);
let blob = new Blob(chunks, { type: media.type })
, url = URL.createObjectURL(blob)
, li = document.createElement('li')
, mt = document.createElement(media.tag)
, hf = document.createElement('a');
if (recorder.state == 'inactive') makeLink(url, li, mt, hf);
let reader = new FileReader();
reader.addEventListener('load', e=>{
var url = "http://localhost:8080/rest/api/v1/audio/submit";
var payload = '{"recording":"'+reader.result+'"}';
var xhttp = new XMLHttpRequest();
xhttp.open("POST",url,true );
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.send(payload);
});
reader.readAsDataURL(blob);
};
}).catch(log);
start.onclick = e => {
start.disabled = true;
stop.removeAttribute('disabled');
chunks = [];
recorder.start();
}
stop.onclick = e => {
stop.disabled = true;
recorder.stop();
start.removeAttribute('disabled');
}
function makeLink(url, li, mt, hf) {
mt.controls = true;
mt.src = url;
hf.href = url;
hf.download = `${counter++}${media.ext}`;
hf.innerHTML = `${hf.href}`;
li.appendChild(mt);
li.appendChild(hf);
ul.appendChild(li);
}
I think that audio recording is still not working with Chrome on iOS. 我认为录音仍然无法在iOS上使用Chrome。 It does work with the safari browser.
它适用于Safari浏览器。 But you can not use the MediaRecorder, instead you have to use the Media Capture and Streams API.
但您无法使用MediaRecorder,而必须使用Media Capture和Streams API。 Here is a link to that: https://developer.mozilla.org/en-US/docs/Web/API/Media_Streams_API
这是以下链接: https : //developer.mozilla.org/en-US/docs/Web/API/Media_Streams_API
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.