Transferring audio files over websocket

I have a wav file generated on an Android mobile and I want to send this file to the server via a web socket. i am using autobahn lib to connect to websocket. Used under code to send file stream.

byte [] buffer  = new byte [1024];
FileInputStream fis = new FileInputStream(myFile);

Log.d(TAG, "Sending...");
boolean status = true;
while (status) {
     int in = fis.read(buffer, 0, buffer.length);                
     if(in != -1){                  
         if(in < buffer.length){                         
             byte[] temp = new byte[in];                        
             for(int i=0; i<in; i++){
                 temp[i] = buffer[i];                           
             }
             mConnection.sendBinaryMessage(temp);
         } else {
             mConnection.sendBinaryMessage(buffer); 
         }                                       
     } else {
         status = false;
     }                  
 }

      

wav sound file works great on mobile. but on the server side, the resulting file is not a proper sound wav file and does not play.

server code:

public void onMessage(byte[] data, boolean arg1) {                      
    try {
        System.out.println("Starting new recording.");
        FileOutputStream fOut = new FileOutputStream(f1, true);
        fOut.write(data);
        fOut.close();                                                       
    } catch (Exception e) {
        e.printStackTrace();
    }       
}

      

Please, help.........

+3


source to share





All Articles