3

I send a wave file using a client and server method.

How could I play this file on the client once it's received?

This is the server code used to send the wave file:

import java.lang.*;
import java.io.*;
import java.net.*;

class Server {
//D:\Documents and Settings\Desktop\gradpro\test1
    static final String OUTPUTFILENAME = "C:\\Documents and Settings\\Administratore\\Desktop\\gradpro\\test1\\s1.wav";
    static final int PORT       = 8811;

    public static void main(String args[]) {

        System.out.println("New server running...\n\n");

        // Infinite loop, innit
        while ( true ) {

            try {
                //Create a socket
                ServerSocket srvr = new ServerSocket(PORT);
                Socket skt = srvr.accept();

                //Create a file output stream, and a buffered input stream
                FileOutputStream fos = new FileOutputStream(OUTPUTFILENAME);
                BufferedOutputStream out = new BufferedOutputStream(fos);
                BufferedInputStream in = new BufferedInputStream( skt.getInputStream() );

                //Read, and write the file to the socket
                int i;
                while ((i = in.read()) != -1) {
                    out.write(i);
                    //System.out.println(i);
                    System.out.println("Receiving data...");
                }
                out.flush();
                in.close();
                out.close();
                skt.close();
                srvr.close();
                System.out.println("Transfer complete.");
            }
            catch(Exception e) {

                System.out.print("Error! It didn't work! " + e + "\n");
            }

            //Sleep...
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) {
                System.err.println("Interrupted");
            }

        } //end infinite while loop
    }



}
Ben S
  • 68,394
  • 30
  • 171
  • 212

2 Answers2

1

Look at the javax.sound.sampled API.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

You can use the AudioClip class to play WAV files; alternatively, you can use the Java Sound API.

McDowell
  • 107,573
  • 31
  • 204
  • 267