Results 1 to 2 of 2
Thread: Audio files.
- 03-10-2011, 10:01 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
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
Client CodeJava 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(); } } }
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
- 03-11-2011, 05:36 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 64
- Rep Power
- 0
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
-
Audio conferencing using JMF
By sathishkumar in forum Advanced JavaReplies: 0Last Post: 03-10-2011, 03:02 PM -
duration of the audio file
By Saran185 in forum Java AppletsReplies: 2Last Post: 02-14-2011, 05:31 AM -
playing audio in J-App??
By ashton in forum New To JavaReplies: 3Last Post: 01-30-2009, 08:50 AM -
Please help Transmitting Audio and Video using RTP
By pedaramanareddy in forum New To JavaReplies: 0Last Post: 01-03-2008, 02:39 PM -
Audio Recorder
By elizabeth in forum Java AppletsReplies: 1Last Post: 08-06-2007, 09:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks