Pessoal, eu já fiz um desses a muito tempo atrás …
Taí o codigo :
/**
* @author Marcio L. M.
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.*;
import java.net.*;
import javax.sound.sampled.*;
public class Transmissor
{
private static int porta;
private String host;
private AudioFormat formato;
private Socket socket;
private DataOutputStream saida;
TargetDataLine line;
public Transmissor( String host )
{
this.host = host;
formato = new AudioFormat( AudioFormat.Encoding.PCM_UNSIGNED,
(float)8000, 8, 1, 1, (float)8000, false );
}
public void executar()
{
try
{
socket = new Socket("127.0.0.1", porta );
saida = new DataOutputStream( socket.getOutputStream() );
DataLine.Info info;
info = new DataLine.Info( TargetDataLine.class, formato );
line = (TargetDataLine) AudioSystem.getLine( info );
line.open( formato, line.getBufferSize() );
int frameSizeInBytes = formato.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[ bufferLengthInBytes ];
System.out.println("Formato = " + formato.toString() );
System.out.println("Taxa = " + formato.getSampleRate() );
System.out.println("Bytes por Frame = " + frameSizeInBytes );
System.out.println("tamanho do Buffer = " + line.getBufferSize() );
System.out.println("Frames por Buffer = " + bufferLengthInFrames );
System.out.println("Buffers em Frames = " + bufferLengthInBytes );
line.start();
System.out.println("....Conectado ao Servidor");
System.out.println("....Pronto para Transmissão!");
int cont=0;
while ( line.read( data, 0, data.length ) != -1 )
{
saida.write( data );
}
line.stop();
line.close();
saida.close();
socket.close();
}
catch ( SocketException e )
{
return;
}
catch ( Exception e )
{
e.printStackTrace();
}
finally
{
System.out.println("Fim da Transmissao...!");
fecharSocket();
}
}
private void fecharSocket()
{
try
{
if ( socket != null )
{
socket.close();
line.close();
saida.close();
}
}
catch ( IOException e )
{
System.out.println("Erro ao fechar a conexao!");
e.printStackTrace();
}
}
public static void main( String[] args )
{
if ( args.length == 0 )
{
System.out.println("É necessário específicar uma porta válida!");
System.exit( 0 );
}
Transmissor trasmissor = new Transmissor( args[ 0 ] );
porta = Integer.parseInt( args[ 0 ] );
trasmissor.executar();
}
}
/**
* @author Marcio L. M
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.net.*;
import java.io.*;
import javax.sound.sampled.*;
public class Receptor
{
private File file;
private Socket socket;
private static int porta;
private SourceDataLine line;
private AudioFormat formato;
private int frameSizeInBytes;
private DataInputStream entrada;
private int bufferLengthInBytes;
public Receptor()
{
formato = new AudioFormat( AudioFormat.Encoding.PCM_UNSIGNED,
(float)8000, 8, 1, 1, (float)8000, false );
}
public void executar()
{
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket( porta );
System.out.println("Receptor Ativado na Porta : " + porta );
}
catch( Exception e )
{
System.out.println("Erro ao conectar " + e.toString() );
e.printStackTrace();
fecharSocket();
return;
}
while( true )
{
try
{
socket = serverSocket.accept();
System.out.println("Aguardando conexao ... " + socket.toString() );
entrada = new DataInputStream( socket.getInputStream() );
DataLine.Info info;
info = new DataLine.Info( SourceDataLine.class, formato );
SourceDataLine line = (SourceDataLine) AudioSystem.getLine( info );
line.open( formato, line.getBufferSize() );
frameSizeInBytes = formato.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[ bufferLengthInBytes ];
System.out.println("Formato = " + formato.toString());
System.out.println("Taxa = " + formato.getSampleRate());
System.out.println("Bytes por Frame = " + frameSizeInBytes );
System.out.println("Tamnho do Buffer = " + line.getBufferSize() );
System.out.println("Buffer em Frames = " + bufferLengthInFrames );
System.out.println("Buffers em Bytes = " + bufferLengthInBytes );
line.start();
while ( entrada.read( data ) != -1 )
{
line.write( data, 0, data.length );
}
line.drain();
line.stop();
line.close();
entrada.close();
socket.close();
System.out.println("Modulo Receptor Fechado");
}
catch ( IOException e )
{
e.printStackTrace();
}
catch ( Exception e )
{
e.printStackTrace();
}
finally
{
fecharSocket();
System.out.println("Receptor Desativado!");
}
}
}
private void fecharSocket()
{
try
{
if ( socket != null )
{
socket.close();
}
}
catch ( IOException e )
{
System.out.println("Erro ao fechar a conexao!");
e.printStackTrace();
}
}
public static void main( String[] args )
{
if ( args.length == 0 )
{
System.out.println("É necessário específicar uma porta válida!");
System.exit( 0 );
}
porta = Integer.parseInt( args[ 0 ] );
Receptor receptor = new Receptor();
receptor.executar();
}
}
Não pensem que está tudo pronto, ainda tem muitos problemas para ser resolvidos neste código, como por exemplo o delay que ele gera ao londo da transmissão.
Bom…
Já é um começo para vocês, agora falta pesquisar mais um pouco para aperfeiçoar.
[]'s