Results 1 to 2 of 2

Thread: Audio files.

  1. #1
    sarah jain is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default Audio files.

    Hello,

    This code is for transferring audio files between client and server. I used mp3 file format. A new mp3 file called B.mp3 is created but with 0 size, I want the file to play music. Please help me.

    SERVER code

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class server {
      public static void main (String [] args ) throws IOException {
        ServerSocket server = new ServerSocket(13267);
        while (true) {
          System.out.println("Waiting...");
    
          Socket sock = server.accept();
          System.out.println("Accepted connection : " + sock);
          File myFile = new File ("C:\\Users\\Sarah\\Desktop\\A.mp3");
          byte [] myarray  = new byte [(int)myFile.length()];
          FileInputStream fis = new FileInputStream(myFile);
          BufferedInputStream bis = new BufferedInputStream(fis);
          bis.read(myarray,0,myarray.length);
          OutputStream os = sock.getOutputStream();
          System.out.println("Sending...");
          os.write(myarray,0,myarray.length);
          os.flush();
          sock.close();
          }
        }
    }
    Client Code

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class client{
      public static void main (String [] args ) throws IOException {
        int filesize=7022316; 
    
        long start = System.currentTimeMillis();
        int bytesRead;
        int current = 0;
        Socket sock = new Socket("localhost",13267);
        System.out.println("Connecting...");
        byte [] mybytearray  = new byte [filesize];
        InputStream is = sock.getInputStream();
        FileOutputStream fos = new      FileOutputStream("C:\\Users\\Sarah\\Documents\\B.mp3");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = is.read(mybytearray,0,mybytearray.length);
        current = bytesRead;
        do {
           bytesRead =
              is.read(mybytearray, current, (mybytearray.length-current));
           if(bytesRead >= 0) current += bytesRead;
        } while(bytesRead != 0);
    
        bos.write(mybytearray, 0 , current);
        bos.flush();
        long end = System.currentTimeMillis();
        System.out.println(end-start);
        bos.close();
        sock.close();
      }
    }

    Thanks,
    Sarah

  2. #2
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    run the server, and point FireFox to
    localhost:13267
    see what happens. This will tell you if the server is okay.

    On the client,
    while(bytesRead != 0); <--- are sure about the 0 ?
    Read up on InputStream.read, and what it returns when the end is reached.
    also, put a
    System.out.print(bytesRead);
    inside the do-while loop and see what happens.

Similar Threads

  1. Audio conferencing using JMF
    By sathishkumar in forum Advanced Java
    Replies: 0
    Last Post: 03-10-2011, 03:02 PM
  2. duration of the audio file
    By Saran185 in forum Java Applets
    Replies: 2
    Last Post: 02-14-2011, 05:31 AM
  3. playing audio in J-App??
    By ashton in forum New To Java
    Replies: 3
    Last Post: 01-30-2009, 08:50 AM
  4. Please help Transmitting Audio and Video using RTP
    By pedaramanareddy in forum New To Java
    Replies: 0
    Last Post: 01-03-2008, 02:39 PM
  5. Audio Recorder
    By elizabeth in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 09:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •