How do I record my voice and make it a file in java? -
i'm trying code, after test 5 , test 9, code seems stop functioning @ audiostream.write()
.
nothing after
audiosystem.write(audiostream, audiofileformat.type.wave, audiofile);
is executed program. how fix or there way record voice?
import javax.sound.sampled.*; import java.io.*; public class mic{ public static void main(string[] args){ system.out.println("sound test starting"); try { audioformat format = new audioformat(audioformat.encoding.pcm_signed, 44100, 16, 2, 4, 44100, false); dataline.info info = new dataline.info(targetdataline.class, format); if(!audiosystem.islinesupported(info)) { system.err.println("line not supported"); } final targetdataline targetline = (targetdataline)audiosystem.getline(info); targetline.open(); system.out.println("recording voice"); targetline.start(); thread thread = new thread(); { //@override public void run(); { audioinputstream audiostream = new audioinputstream(targetline); file audiofile = new file("recording.wav"); system.out.println("test5"); try { system.out.println("test9"); audiosystem.write(audiostream, audiofileformat.type.wave, audiofile); system.out.println("audio writing test"); } catch(ioexception e){ e.printstacktrace(); } system.out.println("test6"); system.out.println("recording stopped"); } }; thread.start(); thread.sleep(5000); targetline.stop(); targetline.close(); system.out.println("ended sound test"); } catch(interruptedexception ie){ ie.printstacktrace(); } catch(lineunavailableexception e){ e.printstacktrace(); } } }
there 2 possible things happening:
the
write
call blocking ever.the
write
call throwing unchecked exception, going unreported.
temporarily change this:
catch (ioexception e) { e.printstacktrace(); }
to this:
catch (throwable e) { e.printstacktrace(); }
and show stacktrace (if any) gets output.
here's couple of possibility:
the write
operation calling copy audiostream
file:
if source (
audiostream
) not set up,write
blocked in reading data input device.have checked output file? has been written it? recording process working ...
either way, next thing would run under debugger, , try figure out write
call doing while (apparently) blocked.
Comments
Post a Comment