Java plays multiple WAV audio sequentially

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.