Как я могу сделать живое потоковое аудио с сервером узла? - PullRequest
0 голосов
/ 29 августа 2018

Я хочу сделать живое потоковое аудио. Я передаю свой mp3-файл с сервера узлов, проблем нет. Я играю mp3 на стороне клиента, но он просто играет, когда заканчивается поток. Я хочу играть в нее, когда поток продолжится. Как я могу это сделать?

Серверная сторона

   var stream = ss.createStream();
        ss(socket).emit('H_to_S_shareVoice', stream);
        var filename = 'musix' + '.mp3';
        console.log(filename);
        fs.createReadStream(filename).pipe(stream);

Клиент

<br>
<br>
<h1 id='socket'>not connected</h1>
<br>
<audio autoplay volume="1" controls id="audio" />

    $(document).ready(function () {

        $(function () {
            var socket = io.connect('http://localhost:4000');


            socket.on("connect", function () {
                console.log("on connect");
                document.getElementById('socket').innerHTML = 'connected to the server';

                ss(socket).on('H_to_S_shareVoice', function (stream, data) {
                    console.log('received', data);

                    var binaryString = "";
                    stream.on('data', function (data) {
                        console.log('data')
                        console.log(data.length)
                        for (var i = 0; i < data.length; i++) {
                            binaryString += String.fromCharCode(data[i]);
                        }
                        $("#audio").attr("src", "data:audio/wav;base64," + window.btoa(binaryString));
                        document.getElementById('audio').load();
                        document.getElementById('audio').play();
                        //document.getElementById('test').innerHTML = window.btoa(binaryString);
                        /*if (dat.length > 0 && oneTime) {
                            oneTime = false;

                        }*/
                        //  $("#audio").attr("src", "data:audio/wav;base64," + window.btoa(binaryString));


                        binaryString = ""
                    });

                    /*stream.on('end', function (data) {
                        console.log('end')
                        document.write(binaryString);
                        $("#audio").attr("src", "data:audio/wav;base64," + window.btoa(binaryString));

                        binaryString = "";
                    });*/
                });
            });
        });
    });

</script>

Ответы [ 2 ]

0 голосов
/ 31 августа 2018

Я исправил с помощью PCMPlayer.

var player = new PCMPlayer({
                        encoding: '16bitInt',
                        channels: 2,
                        sampleRate: 16000,
                        flushingTime: 1000
                    });
                    stream.on('data', function (data) {
                        console.log(data)
                        player.feed(data);
                    });
0 голосов
/ 31 августа 2018

Удалите весь код Socket.IO и весь кодовый код base64 ...

Все, что вам нужно сделать, это вывести данные мультимедиа по HTTP, когда поступит запрос. Код вашего клиента так же прост, как <audio src="path/on/the/server"></audio>.

...