简体   繁体   English

如何使用Javascript在移动Safari和所有其他浏览器上录制音频?

[英]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.

相关问题 javascript表单提交不能在Safari中使用,并且可以在所有其他浏览器中使用? - javascript Form submit not working in safari and works in all other browsers? 在适用于所有浏览器和iOS的网站上录制音频 - Record Audio on Website that works on all browsers and iOS 如何仅在智能手机的 Chrome 上隐藏 Javascript? 但不是在其他移动浏览器(或)桌面? - How to hide a Javascript only on Chrome for smartphone? But not in other mobile browsers (or) desktop? JavaScript中的简单正则表达式可用于除Safari之外的所有浏览器 - Simple regex in JavaScript works in all browsers but Safari Opera mini和其他移动浏览器的JavaScript setInterval - JavaScript setInterval for opera mini and other mobile browsers JavaScript-如何为其他浏览器添加对document.all的支持? - JavaScript - how to add support for document.all for other browsers? 音频无法播放移动浏览器 - Audio Not Playing Mobile Browsers javascript如何在FireFox中完美地工作,而在其他浏览器中却根本不工作? - How can javascript work perfectly in FireFox, but not work at all in other browsers? Regex-除safari外的所有移动浏览器的用户代理 - Regex - User Agent for all mobile browsers apart from safari Javascript —将音频推送到移动设备(Mobile Safari和Chrome) - Javascript — push audio to mobile device (Mobile Safari and Chrome)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM