音频传输android和节点webkit

Audio transfer android and node-webkit

本文关键字:节点 webkit android 传输 音频      更新时间:2023-09-26

我一直试图在我的android应用程序和我的node webkit应用程序之间传输音频文件,但我对socket.io/nodejs/delivery.js世界还不熟悉。

这是我的代码:

android代码ERROR-LINE:os.write(mybyterarray,0,mybyterArray.length);

protected Void doInBackground(Void... arg0) {
        Socket sock;
        try {
            // sock = new Socket("MY_PCs_IP", 1149);
            sock = new Socket("192.168.0.10", 5001);
            System.out.println("Connecting...");
            // sendfile
            File myFile = new File(this.currentSong.getPath());
            byte[] mybytearray = new byte[(int) myFile.length()];
            FileInputStream fis = new FileInputStream(myFile);
            BufferedInputStream bis = new BufferedInputStream(fis);
            bis.read(mybytearray, 0, mybytearray.length);
            OutputStream os = sock.getOutputStream();
            System.out.println("Sending...");
            os.write(mybytearray, 0, mybytearray.length);
            os.flush();
            System.out.println("Sended..");
            // RESPONSE FROM THE SERVER
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(sock.getInputStream()));
            in.ready();
            String userInput = in.readLine();
            System.out.println("Response from server..." + userInput);
            sock.close();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

节点webkit代码

var io  = require('socket.io').listen(5001),
dl = require('delivery'), //delivery.server
fs  = require('fs');
io.sockets.on('connection', function(socket){
delivery = dl.listen(socket);
delivery.on('receive.success',function(file){
    fs.writeFile("music/"+file.name,file.buffer, function(err){
        if(err){
            console.log('File could not be saved.');
        }else{
            console.log('File saved.');
            addSong("music/"+file.name);
        };
    });
});
});

注意:我的服务器端运行良好,已经通过js客户端进行了测试

这是我得到的错误:

安卓系统错误:

08-28 14:56:36.180: W/System.err(30510): java.net.SocketException: sendto failed: EPIPE (Broken     pipe)
08-28 14:56:36.180: W/System.err(30510):    at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:499)
08-28 14:56:36.180: W/System.err(30510):    at libcore.io.IoBridge.sendto(IoBridge.java:468)
08-28 14:56:36.180: W/System.err(30510):    at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)

所以,也许由于协议的原因,我试图进行糟糕的连接是错误的。。在插座和插座之间。io。。?如果有人能帮我,我会很高兴的。我已经环顾四周了,但正如我所说的,我对这个世界很陌生,我变得很模糊基本上我的问题是:怎么了?以及我如何实现我的目标?

感谢您抽出时间

我正在使用com.koushikutta.sync.http.socketio.SocketIOClient这个库和socket.io有一些问题,但它是通过使用对节点webkit 的依赖来解决的

"socket.io":"~0.9",

还需要读取文件->base64编码->然后在服务器端发出字符串必须这样做:

socket.on('finishFileTransfer',function(){
    fs.writeFile("music/"+fileName,new Buffer(file,'base64'), function(err){
        if(err){
            console.log('File could not be saved.');
        }else{
            console.log('File saved.');
            addSong("musica/"+fileName);
        }
        file = "";
        fileName = null;
    });
});