Saturday, November 21, 2009

Streaming WAV sound files over network

I've created a simple program that serves sound file to a single client, and it worked well within a LAN . I was able to play the file remotely....
You just try the code :

SoundServer.java

import java.io.*;
import java.net.*;
public class SoundServer{

private InputStream in ;
private OutputStream out ;
private ServerSocket ss ;
private Socket s ;
private String filename ;

public SoundServer(String filename)throws Exception{
this.filename = filename ;
in = new FileInputStream(filename) ;
in = new BufferedInputStream(in) ;

ss = new ServerSocket(2345) ;

}


public class RunWhenShuttingDown extends Thread {
public void run() {
System.out.println("Control-C caught. Shutting down...");
try {
in.close() ;
out.close() ;
ss.close() ;
s.close() ;
}catch(Exception e2){
e2.printStackTrace() ;
}
System.out.println("Shutdown gracefully");
}
}



public void runMain()throws Exception{


Runtime.getRuntime().addShutdownHook(new RunWhenShuttingDown());

s = ss.accept() ;

out = s.getOutputStream() ;
out = new BufferedOutputStream(out) ;


int availSize = 0 ;
byte[] buf = null ;
int noRead = -2 ;

while(noRead!=-1 && noRead!=0 ){
availSize = (in.available())/5 ;
buf = new byte[availSize] ;
noRead = in.read(buf,0,availSize) ;
out.write(buf,0,availSize) ;
System.out.println("Avail Size : "+availSize+" Bytes read : "+noRead+" items");
}

System.out.println("Sound File Served completely");
}

public static void main(String[] args)throws Exception{
if(args.length !=1){
System.out.println("Usage: java SoundServer ");
System.exit(0);
}
new SoundServer(args[0]).runMain() ;
}

}


SoundClient.java

import java.io.*;
import java.net.*;
import javax.sound.sampled.*;

public class SoundClient {
private InetAddress localhost ;
private Socket s ;
private InputStream in ;
private AudioInputStream ais ;
private DataLine.Info info ;
private SourceDataLine srcDataLine ;


public SoundClient(byte[] arr)throws Exception{
// localhost = InetAddress.getByName(name) ;
localhost = InetAddress.getByAddress(arr) ;
s = new Socket(localhost,2345) ;

in = s.getInputStream() ;
in = new BufferedInputStream(in) ;

ais = AudioSystem.getAudioInputStream(in) ;
if(ais==null){
System.out.println("Failure: in getting AudioInputStream ");
System.exit(0);
}

info = new DataLine.Info(SourceDataLine.class,ais.getFormat()) ;
srcDataLine = (SourceDataLine) AudioSystem.getLine(info) ;
srcDataLine.open(ais.getFormat()) ;
srcDataLine.start() ;

int avail = 0 ;
int numBytesRead = 0 ,total = 0;
byte[] myread = null ;
while(true){
avail = ((srcDataLine.getBufferSize())/5);
myread = new byte[avail] ;
numBytesRead = ais.read(myread,0,avail) ;
if(numBytesRead== -1)break;
total += numBytesRead ;
srcDataLine.write(myread,0,numBytesRead) ;
}

srcDataLine.flush() ;

System.out.println("Total Number of bytes read : "+total);

srcDataLine.close() ;
in.close() ;
ais.close();
s.close() ;


System.out.println("Completed Successfully");

}

public static void main(String[] args)throws Exception{
if(args.length!= 4){
System.out.println("Usage: java SoundClient /neg: java SoundClient 192 168 1 1 ");
System.exit(0);
}
byte[] var = new byte[4] ;
var[0] = new Integer(args[0]).byteValue() ;
var[1] = new Integer(args[1]).byteValue() ;
var[2] = new Integer(args[2]).byteValue() ;
var[3] = new Integer(args[3]).byteValue() ;
new SoundClient(var) ;
}
}

No comments:

Post a Comment