Java Audio playback sample sharing (Java How to play audio) _java

Source: Internet
Author: User

This is a streamlined code that can support a very limited format.

Copy Code code as follows:

Package com.hongyuan.test;

Import Java.io.File;
Import java.io.IOException;

Import Javax.sound.sampled.AudioFormat;
Import Javax.sound.sampled.AudioInputStream;
Import Javax.sound.sampled.AudioSystem;
Import Javax.sound.sampled.DataLine;
Import javax.sound.sampled.LineUnavailableException;
Import Javax.sound.sampled.SourceDataLine;
Import javax.sound.sampled.UnsupportedAudioFileException;

public class Musictest {

public static final String music_file = "Meet a smile. wav";

public static void Main (string[] args) throws Lineunavailableexception,
Unsupportedaudiofileexception, IOException {

Get audio input stream
Audioinputstream Audioinputstream = Audiosystem
. Getaudioinputstream (New File (Music_file));
Get Audio Encoding Object
Audioformat Audioformat = Audioinputstream.getformat ();

Set Data entry
Dataline.info datalineinfo = new Dataline.info (Sourcedataline.class,
Audioformat, audiosystem.not_specified);
Sourcedataline sourcedataline = (sourcedataline) audiosystem
. Getline (Datalineinfo);
Sourcedataline.open (Audioformat);
Sourcedataline.start ();

/*
* Read the data from the input stream and send it to the mixer
*/
int count;
byte tempbuffer[] = new byte[1024];
while ((count = Audioinputstream.read (tempbuffer, 0, Tempbuffer.length))!=-1) {
if (Count > 0) {
Sourcedataline.write (tempbuffer, 0, Count);
}
}

Empty the data buffer and close the input
Sourcedataline.drain ();
Sourcedataline.close ();
}

}

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.