Results 1 to 1 of 1
- 04-24-2010, 08:27 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 1
- Rep Power
- 0
forwarding, rewinding , stopping an audiostream using client server
Hi Everyone!!
I would kindly like some help to be able to stop, forward and rewind the the wav file when pressing the respective buttons for it. n i would like to do that by 1st determining the current byte the sound that was played when button was pressed and then forwarding it or rewinding it based on that byte. The help is very much appreciated. Here is some of my code:-
Server Side- To Establish Connection :-
Client Side- To Receive Connection and list of file names:-Java Code:try{ welcomeSocket = new ServerSocket(4563); while (true) { clientSocket = welcomeSocket.accept(); inFromClient1 = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); outToClient = new DataOutputStream(clientSocket.getOutputStream());
Client Side- To send selected file name to be played from Server upon play button is pressed.Java Code:try{ clientSocket = new Socket("localhost",4563); outToServer = new DataOutputStream(clientSocket.getOutputStream()); inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); outToServer.writeBytes(user + '\n'); outToServer.writeBytes(str.toString()+'\n'); //outToServer.writeBytes(str); String fileNames[] = new String[20]; int count = (inFromServer.read()); for (int i = 0; i<count; i++) fileNames[i] = inFromServer.readLine(); jList1.setListData(fileNames); jList1.updateUI(); // clientSocket.close(); } catch (IOException e) { System.err.println("Can not Connect");}
Server Side- To send sound file.Java Code:try{ outToServer.write(jList1.getSelectedIndex()); playAudio(); } catch (IOException e) { System.err.println("Error");}
Client Side - To play file.Java Code:class StreamThread extends Thread{ byte b[] = new byte[100]; public void run(){ { try { outToClient.write(count); for (int i = 0 ; i >< count; i++) outToClient.writeBytes(fileName[i]+'\n'); int cf = inFromClient1.read(); System.out.println(cf); FileInputStream fis = new FileInputStream(files[cf]); // byte[] b = new byte [100]; int r = fis.read(b, 0, b.length); while( r != -1) { outToClient.write(b, 0, r); r = fis.read(b,0, b.length); } System.out.println("Streaming finished"); } catch(IOException e) { System.err.println(e); System.exit(1); } } } }
Thank You.Java Code:private void playAudio() { try{ audioInputStream = AudioSystem.getAudioInputStream(clientSocket.getInputStream()); audioFormat = audioInputStream.getFormat(); System.out.println(audioFormat); DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class, audioFormat); sourceDataLine = (SourceDataLine)AudioSystem.getLine( dataLineInfo); //Create a thread to play back the data and // start it running. It will run until the // end of file, or the Stop button is // clicked, whichever occurs first. // Because of the data buffers involved, // there will normally be a delay between // the click on the Stop button and the // actual termination of playback. new PlayThread().start(); }catch (Exception e) { e.printStackTrace(); System.exit(0); }//end catch }//end playAudio class PlayThread extends Thread{ byte tempBuffer[] = new byte[100]; public void run(){ try{ sourceDataLine.open(audioFormat); sourceDataLine.start(); int buffcount; int cnt; //Keep looping until the input read method // returns -1 for empty stream or the // user clicks the Stop button causing // stopPlayback to switch from false to // true. while((cnt = audioInputStream.read( tempBuffer,0,tempBuffer.length)) != -1 && stopPlayback == false){ if(cnt > 0){ //Write data to the internal buffer of // the data line where it will be // delivered to the speaker. sourceDataLine.write( tempBuffer, 0, cnt); } else if(stopPlayback = true) { cnt= audioInputStream.read( tempBuffer); outToServer.writeByte(cnt); } else if(forwardPlayback = true) { cnt = audioInputStream.read( tempBuffer); outToServer.writeByte(cnt); } else if(rewindPlayback = true) { cnt= audioInputStream.read( tempBuffer); outToServer.writeByte(cnt); } }//end while //Block and wait for internal buffer of the // data line to empty. sourceDataLine.drain(); sourceDataLine.close(); //Prepare to playback another file jButton3.setEnabled(false); jButton2.setEnabled(true); stopPlayback = false; }catch (Exception e) { e.printStackTrace(); System.exit(0); }//end catch } //end run
Similar Threads
-
how to send a file from server to client and client saves the file?
By KoolCancer in forum New To JavaReplies: 3Last Post: 07-29-2009, 04:52 AM -
Client server
By yash in forum NetworkingReplies: 9Last Post: 01-04-2009, 04:33 PM -
client-server app
By shwein in forum NetworkingReplies: 3Last Post: 10-14-2008, 05:20 PM -
Datagram Client and Server, client timer question
By saru88 in forum NetworkingReplies: 1Last Post: 10-05-2008, 03:12 PM -
Identify Client in Socket Client Server Application
By masadjie in forum NetworkingReplies: 1Last Post: 12-20-2007, 09:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks