1

另外,我想从 android MP3 数据流流式传输到由 Node.js 实现的 WebServer。Android-Node.js:协议是 TCP。在我的项目中,从 android MP3 sound 获取的音频流数据会发送到 node.js,它是 web 服务器。

var net = require('net');
var http = require('http');
var fs = require('fs');
var server = http.createServer( function(req, res){
    fs.readFile('index.html', function (error, data) {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.end(data);

   });  
    console.log('Client connected; streaming');
});
server.listen("8888", "127.0.0.1");

///////// webServer
var object = {};
var WebSocketServer = require('ws').Server;
wss = new WebSocketServer({port: 8080});

wss.on('connection', function(ws) {

    ws.on('message', function(message) {
        ws.send('pong');
    });
    console.log("object[1]====" + object[1]);
});

wss.on('close', function(){
    clearInterval(t);
    console.log(" websocket 연결 끊킴...");
});

//Android Tcp Server
var server_net = net.createServer(function (socket){ 

    socket.on('data', function (data)    {
        wss.clients.forEach(function(client) {
            client.send(  data, {binary:true , mast:true}  );

        });

         console.log(data);    
    });

});
server_net.listen(6666,function (){ 
        console.log('Android Node Server listening port:6666 ');
    });    

// Node.js log : 来自android音频数据的音频数据日志

//Web index.html 

<!DOCTYPE html>
<html>
<head>
    <script src='http://code.jquery.com/jquery-1.7.2.min.js' type='text/javascript'></script>
    <script type='text/javascript'>
        var ctx = new webkitAudioContext();
        function playSound(arrBuff) {
            ctx.decodeAudioData(arrBuff , function(buffer){
                var src = ctx.createBufferSource();
                src.buffer = buffer;
                src.looping = false;
                src.connect(ctx.destination);
                src.noteOn(0);           //Play  immediately
                console.log('Finished decoding');
            } ,function(e){
                console.log(e);
            });
        }
        var ws = new WebSocket('ws://210.118.69.173:8080/data');
        ws.binaryType = 'arraybuffer';
        ws.onopen=function(){
            console.log("Connected");
        }
        ws.onmessage = function(e) {
            console.log(e.data.byteLength);
            playSound(e.data);
        }
        ws.onclose = function() {
            console.log("disconnected");
        }
    </script>
</head>
<body></body>
</html>

我无法收听从 Android 获取的音频数据到 web(html5),有人知道原因吗?请告诉我为什么...

4

0 回答 0