簡體   English   中英

onaudioprocess 未在 ios11 上調用

[英]onaudioprocess not called on ios11

最近添加了支持后,我正在嘗試從在 iOS11 上的 Safari 上工作的麥克風獲取音頻捕獲

但是,永遠不會調用onaudioprocess回調。 這是一個示例頁面:

<html>
    <body>
        <button onclick="doIt()">DoIt</button>
        <ul id="logMessages">
        </ul>
        <script>
            function debug(msg) {
                if (typeof msg !== 'undefined') {
                    var logList = document.getElementById('logMessages');
                    var newLogItem = document.createElement('li');
                    if (typeof  msg === 'function') {
                        msg = Function.prototype.toString(msg);
                    } else if (typeof  msg !== 'string') {
                        msg = JSON.stringify(msg);
                    }
                    var newLogText = document.createTextNode(msg);
                    newLogItem.appendChild(newLogText);
                    logList.appendChild(newLogItem);
                }
            }
            function doIt() {
                var handleSuccess = function (stream) {
                    var context = new AudioContext();
                    var input = context.createMediaStreamSource(stream)
                    var processor = context.createScriptProcessor(1024, 1, 1);

                    input.connect(processor);
                    processor.connect(context.destination);

                    processor.onaudioprocess = function (e) {
                        // Do something with the data, i.e Convert this to WAV
                        debug(e.inputBuffer);
                    };
                };

                navigator.mediaDevices.getUserMedia({audio: true, video: false})
                        .then(handleSuccess);
            }
        </script>
    </body>
</html>

在大多數平台上,當onaudioprocess回調被調用時,您會看到項目被添加到消息列表中。 但是,在 iOS 上,永遠不會調用此回調。

我還應該做些什么來嘗試使用 Safari 在 iOS 11 上調用它?

有兩個問題。 主要的一點是 iOS 11 上的 Safari 似乎會自動掛起不是為了響應點擊而創建的新AudioContext 您可以resume()它們,但只能響應點擊。

(更新:Chrome 移動版也執行此操作,從 70 版 / 2018 年 12 月開始,Chrome 桌面版將具有相同的限制。)

因此,您必須在獲得MediaStream之前創建它,或者讓用戶稍后再次點擊。

您的代碼的另一個問題是AudioContext在 Safari 中以webkitAudioContext為前綴。

這是一個工作版本:

<html>
<body>
<button onclick="beginAudioCapture()">Begin Audio Capture</button>
<script>
  function beginAudioCapture() {

    var AudioContext = window.AudioContext || window.webkitAudioContext;    
    var context = new AudioContext();
    var processor = context.createScriptProcessor(1024, 1, 1);
    processor.connect(context.destination);

    var handleSuccess = function (stream) {
      var input = context.createMediaStreamSource(stream);
      input.connect(processor);

      var recievedAudio = false;
      processor.onaudioprocess = function (e) {
        // This will be called multiple times per second.
        // The audio data will be in e.inputBuffer
        if (!recievedAudio) {
          recievedAudio = true;
          console.log('got audio', e);
        }
      };
    };

    navigator.mediaDevices.getUserMedia({audio: true, video: false})
      .then(handleSuccess);
  }
</script>
</body>
</html>

(您可以更快地設置onaudioprocess回調,但是在用戶批准麥克風訪問之前,您會獲得空緩沖區。)

哦,還有一個需要注意的 iOS 錯誤:iPod touch 上的 Safari(從 iOS 12.1.1 開始)報告說它沒有麥克風(它有)。 因此,如果您在那里請求音頻, getUserMedia 將錯誤地拒絕並顯示Error: Invalid constraint

僅供參考:我在 npm 上維護了麥克風流包,它為您執行此操作並以 Node.js 樣式的 ReadableStream 提供音頻。 如果您或其他任何人更願意在原始代碼上使用它,它包括此修復程序。

在 iOS 11.0.1 上試過了,可惜這個問題還是沒有解決。

作為一種解決方法,我想知道用一個函數替換 ScriptProcessor 是否有意義,該函數從自助餐中獲取蒸汽數據,然后每 x 毫秒處理一次。 但這對功能來說是一個很大的變化。

只是想知道...您是否在 Safari 設置中啟用了該設置? 它在 iOS11 中默認啟用,但也許您只是在沒有注意到的情況下禁用了它。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM