|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Throwing java.io.IOEXception:stream closed, when recording voice from an applet
<pre>
I have developed a small applet which will record voice in wave format and play it. If I run this in IE it is working perfetly alright, but if i run it in Mozilla in "Linux", sometimes it will run successfully and sometimes it will throw "java.io.Exception: stream closed". So, here is the code, I hope i'll get the solution soon. Thanx I have made the line very bold and red in color where i am getting the error. public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("Record")) //Recording Media { act="Record"; rectimerstop=false; stopped=false; new Player(this).start(); System.out.println("time is : " + time); new RecordTimer(time).start(); record.setEnabled(false); test.setEnabled(false); recStop.setEnabled(true); bottomLabel.setText("Recording........"); getAppletContext().showStatus("<<Hello Mike est>>"); } else if(e.getActionCommand().equals("Play")) //Playing Media { act="Play"; stopped=true; playtimerstop=false; new Player(this).start(); play.setEnabled(false); playStop.setEnabled(true); bottomLabel.setText("Playing........"); getAppletContext().showStatus(">>"); } } <b>Class which records and play back</b> class Player extends Thread { Applet appl; Player(Applet appl) { this.appl=appl; } /* This method records the Media from the Microphone */ public void record() { System.out.println("iside recorde and record will start in a moment"); try { if(clip!=null) { clip.stop(); clip.close(); ai.close(); } if(tadl!=null) { tadl.stop(); tadl.close(); ai.close(); } File fos=new File(recfilename); System.out.println("file exists :" + fos.exists()); if(!fos.exists()) fos.createNewFile(); System.out.println("file exists :" + fos.exists()); System.out.println("in record"); System.out.println("format is" + format); if(format==null) { System.out.println("format is null and initialising"); format=new AudioFormat(8000,16,2,true,false); } bottomLabel.setText("Recording........."); Line.Info info = new DataLine.Info(TargetDataLine.class,format); Mixer.Info mi[]=AudioSystem.getMixerInfo(); //for(int i=0;i<mi.length;i++) //System.out.println(mi[i]); tadl=(TargetDataLine)AudioSystem.getLine(info); ai=new AudioInputStream(tadl); System.out.println("file is " + recfilename); filename=recfilename.substring(recfilename.lastInd exOf("/")+1,recfilename.length()); System.out.println("tadl is running " +tadl.isRunning()); tadl.open(format); System.out.println("Recording started"); tadl.start(); System.out.println("ai is" + ai); System.out.println("AudioFileFormat.Type.WAVE is" + AudioFileFormat.Type.WAVE); System.out.println("fos is" + fos); AudioSystem.write(ai,AudioFileFormat.Type.WAVE,fos ); ai.close(); } catch(Exception e) { bottomLabel.setText(e.toString()); e.printStackTrace(); } } /* This method plays the Media */ public void play() { try { if(tadl!=null) { tadl.stop(); tadl.close(); ai.close(); } System.out.println("in play"); System.out.println(recfilename); urltype=recfilename.substring(0,recfilename.lastIn dexOf("/")+1); if(!vMail.equals("none")){ audiUrl=new URL("<A href="file://"+urltype+playf">file://"+urltype+playf); System.out.println("url is " + audiUrl); } else audiUrl=new URL("<A href="file://"+playfilename">file://"+playfilename); //System.out.println(audiUrl); String med=audiUrl.toString(); if(med.indexOf("/")>0) getAppletContext().showStatus(med.substring(med.la stIndexOf("/")+1,med.length())); else getAppletContext().showStatus(med.substring(med.la stIndexOf("<A href="file://\")+1,med.length">\\")+1,med.length())); ai = AudioSystem.getAudioInputStream(audiUrl); AudioFormat format = ai.getFormat(); if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { format = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), format.getSampleSizeInBits()*2, format.getChannels(), format.getFrameSize()*2, format.getFrameRate(), true); // big endian ai = AudioSystem.getAudioInputStream(format, ai); } DataLine.Info info = new DataLine.Info(Clip.class, ai.getFormat(), ((int)ai.getFrameLength()*format.getFrameSize())); clip = (Clip) AudioSystem.getLine(info); clip.open(ai); playtime=clip.getMicrosecondLength()/(1000*1000); clip.start(); new PlayTimer().start(); }catch(Exception e) { bottomLabel.setText(e.toString()); e.printStackTrace(); } } public void convert() { //implemented in v1.1 } public void run() { if(act.equals("Record")) { record(); } else { play(); } } } <b> Timer Class which displays the record time left against record time * available and also resets the buttons on the applet </b> class RecordTimer extends Thread { int time=0; int secs=0; RecordTimer(int time) { this.time=(time*60); //System.out.println(time); } public void run() { try { while((secs-1)<time && !rectimerstop) { sleep(1000); secs++; //System.out.println(secs); bottomLabel.setText("Recording..Secs: "+secs+" of "+time); if(record.isEnabled()) { record.setEnabled(false); test.setEnabled(false); } } bottomLabel.setText("Recording Stopped.........in RecordTimer by user"); sleep(5000); System.out.println("status of tadl is " + tadl); if(tadl!=null) { System.out.println("Before tadl stopped"); tadl.stop(); System.out.println("tadl stopped"); tadl.close(); System.out.println("tadl close"); //ai.close(); //System.out.println("ai close"); stopped=true; bottomLabel.setText("Ready.......For Test........."); getAppletContext().showStatus("(.)"); } test.setEnabled(true); recStop.setEnabled(false); uploadMedia.setEnabled(true); }catch(Exception ex){bottomLabel.setText(ex.toString());} } } </pre> |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Throwing java.io.IOEXception:stream closed, when recording voice from an applet |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|