Воспроизведение аудио через браузер - PullRequest
0 голосов
/ 13 сентября 2018

Вот простая идея. Я пытаюсь соединить .io вывод аудио-данных с микрофона iPhone в браузер.

Теперь из func captureOutput(_output:, didOutPut:, connection:) он отправляет данные буфера выборки как NSData в Browser .

 func createDataBlock(_ sampleBuffer: CMSampleBuffer) -> NSData {
        let block = CMSampleBufferGetDataBuffer(sampleBuffer)
        var length = 0
        var data: UnsafeMutablePointer<Int8>? = nil
        let status = CMBlockBufferGetDataPointer(block!, 0, nil, &length, &data)    // TODO: check for errors
        let result = NSData(bytes: data, length: length)
        return result
 }

 /** Delegate Function from AVCaptureAudioDataOutputSampleBufferDelegate */
 func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        let audio = createDataBlock(sampleBuffer)
        guard let socket = SocketHelper.shared.socket else { return }

        // Emitting Audio as NSData 
        let info = ["room": SocketHelper.shared.roomKey, "voiceBuffer": audio] as [String : Any]
        socket.emit("SEND_VOICE", info)
 }

Когда браузер получает его. Он приходит как ArrayBuffer {} enter image description here

Теперь при попытке воспроизвести эти двоичные данные . Я полностью застрял и не могу понять, как играть в нее. Это то, что у меня есть для моего JavaScript. Здесь всегда происходит сбой. Ошибка, которую он мне дает: Uncaught (in promise) DOMException: Unable to decode audio data

const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioContext = new AudioContext();
let playSoundBuffer;

    function loadSound(bytes) {
      audioContext.decodeAudioData(
        bytes,
        function(buffer) {
          playSoundBuffer = buffer;
          playSound(); // don't start processing it before the response is there!
        },
        function(error) {
          console.error("decodeAudioData error", error);
        }
      );
    } 

function playSound() {
  var source = audioContext.createBufferSource();
  source.buffer = playSoundBuffer; // This is the line that generates the error
  source.connect(audioContext.destination);
  source.start(0);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...