通过socket.io 1.0音频直播 [英] Live audio via socket.io 1.0

查看:2386
本文介绍了通过socket.io 1.0音频直播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于从socket.io网站

As from socket.io website

在1.0开始,就可以发送任何一滴来回:图像,音频,视频

Binary streaming

Starting in 1.0, it's possible to send any blob back and forth: image, audio, video.

我现在想知道,如果这不可能是什么我想最近实现的解决方案。

I'm now wondering, if this couldn't be the solution for something I'm trying to achieve recently.

我其实是在寻找一种方式如何从广播实时音频流(A - 也就是说,麦克风输入..)连接到我的网站的所有客户端。是这样的可能吗?我一直在搞乱WebRTC技术( https://www.webrtc-experiment.com/ )的例子但我一直没能管理目标,为超过几个连接的客户端。

I'm actually looking for a way how to broadcast live audio stream from (A - ie, mic input..) to all clients connected to a website of mine. Is something like this possible? I've been messing with WebRTC (https://www.webrtc-experiment.com/) examples but I haven't been able to manage the goal for more than few connected clients.

我的想法是在边像getUserMedia或任何其他音源(PCM,无论..)一个被切成块,并提供给客户和HTML5音频元素或任何出场例如..我需要作出规定:流尽可能实时地,不喊/冰组播业务快速的werent足够的(事实上,他们不是解决我的问题,他们打算使用这种方式),我真的不关心音频质量。跨平台的兼容性将是真棒。

My idea is about something like getUserMedia or any other audio source (PCM, whatever..) on side A being chopped to chunks and provided to client and played for example by html5 audio element or anything.. I need to make that stream as much realtime as possible, no shout/ice cast services werent fast enough (indeed, they arent solution to my problem, they're meant to be used this way) and I don't really care about the audio quality. Crossplatform compatibility would be awesome.

是一样的东西,可能吗?通过使用socket.io的方式如何提供这些数据给客户?

Is something like that possible? By using socket.io as way how to provide those data to clients?

我将是任何参考,提示或来源,可以帮助我实现这个非常感激。
非常感谢。

I would be very grateful for any reference, hint or source that could help me achieve this. Thanks a lot.

推荐答案

此示例显示了如何使用 MediaRecorder 上传的音频,然后将其转发使用 socket.io 。你叫后,此code将只播出 mediaRecorder.stop()。您可以选择 ondataavailable 内播出。如果你这样做,你可能想传递一个时间片 mediaRecorder.start(),所以它不 ŧ触发 ondataavailable 经常。

This example shows you how to use the MediaRecorder to upload audio and then forward it using socket.io. This code will only broadcast after you're called mediaRecorder.stop(). You can choose to broadcast inside of ondataavailable. If you do that, you might want to pass a timeslice to mediaRecorder.start(), so that it doesn't trigger ondataavailable so often.

这解决方案是不是真正的生活,但我认为这会帮助谁回来的人,发现了这个问题。

This solution isn't truly live, but I think it will help people who come back and find this question.

客户端code

var constraints = { audio: true };
navigator.mediaDevices.getUserMedia(constraints).then(function(mediaStream) {
    var mediaRecorder = new MediaRecorder(mediaStream);
    mediaRecorder.onstart = function(e) {
        this.chunks = [];
    };
    mediaRecorder.ondataavailable = function(e) {
        this.chunks.push(e.data);
    };
    mediaRecorder.onstop = function(e) {
        var blob = new Blob(this.chunks, { 'type' : 'audio/ogg; codecs=opus' });
        socket.emit('radio', blob);
    };

    // Start recording
    mediaRecorder.start();

    // Stop recording after 5 seconds and broadcast it to server
    setTimeout(function() {
        mediaRecorder.stop()
    }, 5000);
});

// When the client receives a voice message it will play the sound
socket.on('voice', function(arrayBuffer) {
    var blob = new Blob([arrayBuffer], { 'type' : 'audio/ogg; codecs=opus' });
    var audio = document.createElement('audio');
    audio.src = window.URL.createObjectURL(blob);
    audio.play();
});

服务器code

socket.on('voice', function(blob) {
    // can choose to broadcast it to whoever you want
    socket.broadcast.emit('voice', blob);
});

这篇关于通过socket.io 1.0音频直播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆