32

我正在用内置的 MediaRecorder 替换 RecordRTC,以便在 Chrome 中录制音频。然后使用音频 api 在程序中播放录制的音频。我无法让 audio.duration 属性正常工作。它说

如果视频(音频)是流式传输的并且没有预定义的长度,则返回“Inf”(无穷大)。

使用 RecordRTC,我必须使用 ffmpeg_asm.js 将音频从 wav 转换为 ogg。我的猜测是在 RecordRTC 设置预定义音频长度的过程中的某个地方。有没有办法使用 MediaRecorder 设置预定义的长度?

4

4 回答 4

45

这是一个铬错误

FF 确实公开了录制媒体的持续时间,如果您确实将currentTime录制媒体的 设置为大于其实际duration,则该属性在 chrome 中可用...

var recorder,
  chunks = [],
  ctx = new AudioContext(),
  aud = document.getElementById('aud');

function exportAudio() {
  var blob = new Blob(chunks);
  aud.src = URL.createObjectURL(new Blob(chunks));

  aud.onloadedmetadata = function() {
    // it should already be available here
    log.textContent = ' duration: ' + aud.duration;
    // handle chrome's bug
    if (aud.duration === Infinity) {
      // set it to bigger than the actual duration
      aud.currentTime = 1e101;
      aud.ontimeupdate = function() {
        this.ontimeupdate = () => {
          return;
        }
        log.textContent += ' after workaround: ' + aud.duration;
        aud.currentTime = 0;
      }
    }
  }
}

function getData() {
  var request = new XMLHttpRequest();
  request.open('GET', 'https://upload.wikimedia.org/wikipedia/commons/4/4b/011229beowulf_grendel.ogg', true);
  request.responseType = 'arraybuffer';
  request.onload = decodeAudio;
  request.send();
}


function decodeAudio(evt) {
  var audioData = this.response;
  ctx.decodeAudioData(audioData, startRecording);
}

function startRecording(buffer) {

  var source = ctx.createBufferSource();
  source.buffer = buffer;
  var dest = ctx.createMediaStreamDestination();
  source.connect(dest);

  recorder = new MediaRecorder(dest.stream);
  recorder.ondataavailable = saveChunks;
  recorder.onstop = exportAudio;
  source.start(0);
  recorder.start();
  log.innerHTML = 'recording...'
  // record only 5 seconds
  setTimeout(function() {
    recorder.stop();
  }, 5000);
}

function saveChunks(evt) {
  if (evt.data.size > 0) {
    chunks.push(evt.data);
  }

}

// we need user-activation
document.getElementById('button').onclick = function(evt){
  getData();
  this.remove();
}
<button id="button">start</button>
<audio id="aud" controls></audio><span id="log"></span>

所以这里的建议是给错误报告加星标,以便铬的团队需要一些时间来修复它,即使这种解决方法可以解决问题......

于 2016-10-11T05:52:27.590 回答
7

感谢@Kaiido 识别错误并提供工作修复。

我准备了一个名为get-blob-duration的 npm 包,您可以安装它来获得一个很好的 Promise 包装函数来完成脏活。

用法如下:

// Returns Promise<Number>
getBlobDuration(blob).then(function(duration) {
  console.log(duration + ' seconds');
});

或 ECMAScript 6:

// yada yada async
const duration = await getBlobDuration(blob)
console.log(duration + ' seconds')
于 2018-02-15T03:10:24.400 回答
5

2016 年检测到但今天(2019 年 3 月)仍然存在的 Chrome 中的一个错误是此行为背后的根本原因。在某些情况下audioElement.duration会返回Infinity

此处此处的Chrome 错误信息

以下代码提供了避免该错误的解决方法。

用法:创建您的audioElement,并一次调用此函数,提供您的audioElement. 当返回promise解析时,audioElement.duration属性应该包含正确的值。(它也解决了同样的问题videoElements

/**
 *  calculateMediaDuration() 
 *  Force media element duration calculation. 
 *  Returns a promise, that resolves when duration is calculated
 **/
function calculateMediaDuration(media){
  return new Promise( (resolve,reject)=>{
    media.onloadedmetadata = function(){
      // set the mediaElement.currentTime  to a high value beyond its real duration
      media.currentTime = Number.MAX_SAFE_INTEGER;
      // listen to time position change
      media.ontimeupdate = function(){
        media.ontimeupdate = function(){};
        // setting player currentTime back to 0 can be buggy too, set it first to .1 sec
        media.currentTime = 0.1;
        media.currentTime = 0;
        // media.duration should now have its correct value, return it...
        resolve(media.duration);
      }
    }
  });
}

// USAGE EXAMPLE :  
calculateMediaDuration( yourAudioElement ).then( ()=>{ 
  console.log( yourAudioElement.duration ) 
});
于 2019-03-11T00:10:24.290 回答
2

感谢@colxi的实际解决方案,我添加了一些验证步骤(因为解决方案运行良好,但存在长音频文件问题)。

我花了大约 4 个小时才让它处理长音频文件,结果验证是解决方法

        function fixInfinity(media) {
          return new Promise((resolve, reject) => {
            //Wait for media to load metadata
            media.onloadedmetadata = () => {
              //Changes the current time to update ontimeupdate
              media.currentTime = Number.MAX_SAFE_INTEGER;
              //Check if its infinite NaN or undefined
              if (ifNull(media)) {
                media.ontimeupdate = () => {
                  //If it is not null resolve the promise and send the duration
                  if (!ifNull(media)) {
                    //If it is not null resolve the promise and send the duration
                    resolve(media.duration);
                  }
                  //Check if its infinite NaN or undefined //The second ontime update is a fallback if the first one fails
                  media.ontimeupdate = () => {
                    if (!ifNull(media)) {
                      resolve(media.duration);
                    }
                  };
                };
              } else {
                //If media duration was never infinity return it
                resolve(media.duration);
              }
            };
          });
        }
        //Check if null
        function ifNull(media) {
          if (media.duration === Infinity || media.duration === NaN || media.duration === undefined) {
            return true;
          } else {
            return false;
          }
        }

    //USAGE EXAMPLE
            //Get audio player on html
            const AudioPlayer = document.getElementById('audio');
            const getInfinity = async () => {
              //Await for promise
              await fixInfinity(AudioPlayer).then(val => {
                //Reset audio current time
                AudioPlayer.currentTime = 0;
                //Log duration
                console.log(val)
              })
            }
于 2020-03-19T23:24:02.697 回答