Play multiple audio parts sequentially in Java, instead of playing simultaneously. The code is as follows:
list<string> files = new arraylist<string> (); Files.add ("Barcode_prefix.wav"); for (char Letter:barcode.toCharArray ()) {Files.add (letter + ". wav"); } files.add ("Barcode_suffix.wav"); byte[] buffer = new byte[4096]; for (String file:files) {try {Audioinputstream = audios Ystem.getaudioinputstream (This.getclass (). getResourceAsStream (file)); Audioformat format = Is.getformat (); Sourcedataline line = audiosystem.getsourcedataline (format); Line.open (format); Line.start (); while (is.available () > 0) {int len = is.read (buffer); Line.write (buffer, 0, Len); } line.drain (); **[deit]** wait for the BUFFer to empty before closing, the line Line.close (); } catch (Exception e) {e.printstacktrace (); } }
This I play in the local no problem, put on the server will be error:
Java.io.ioexception:mark/reset not Supportedat Java.util.zip.InflaterInputStream.reset (Unknown Source) at Com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream (Unknown Source) at Javax.sound.sampled.AudioSystem.getAudioInputStream (Unknown Source) at Com.yulong.mes.winapp.form.quality.forbatchoperationform$playaudiorunnable.run (ForBatchOperationForm.java:4317 ) at Java.lang.Thread.run (Unknown Source)
Online said to use Bufferedinputstream encapsulation. The modified code does this:
list<string> files = new arraylist<string> (); Files.add ("Barcode_prefix.wav"); for (char Letter:barcode.toCharArray ()) {Files.add (letter + ". wav"); } files.add ("Barcode_suffix.wav"); byte[] buffer = new byte[4096]; for (String file:files) {try {InputStream stream = this.ge Tclass (). getResourceAsStream (file); InputStream Bufferedin = new Bufferedinputstream (stream); Audioinputstream is = Audiosystem.getaudioinputstream (Bufferedin); Audioformat format = Is.getformat (); Sourcedataline line = audiosystem.getsourcedataline (format); Line.open (format); Line.start (); while (is.available () > 0) {int len = is.read (buffer); Line.write (buffer, 0, Len); } line.drain (); **[deit]** wait for the buffer to empty before closing, the line Line.close (); } catch (Exception e) {e.printstacktrace (); } }
Java plays multiple WAV audio sequentially