Play audio in Java

Source: Internet
Author: User

Package com. Sunray. Express. Core. util;

Import java. Io. bufferedinputstream;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. ioexception;

Import sun. Audio. audioplayer;
Import sun. Audio. audiostream;

Public class playaudio1 {

Static bufferedinputstream Buf;

Public static void start () // This is to read the sound file through the stream
{
Try {
File dir = new file ("E: \ music \ YH \ WAV"); // the path of the file to be played
File [] FS = dir. listfiles ();
For (File file: FS ){
System. Out. println (file. getname ());
File fobject = new file (Dir, file. getname (); // song name, remember that the format is limited
Fileinputstream files = new fileinputstream (fobject );
Buf = new bufferedinputstream (files );
Audiostream audio = new audiostream (BUF );
Audioplayer. Player. Start (audio );
Try {
Thread. Sleep (3000 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
} Catch (ioexception e ){
System. Out. println (E. getmessage ());
}
}

Public void close () throws exception {
Buf. Close ();
}

Public static void loop (){
// Audioplayer. Player.
}

Public static void main (string [] ARGs ){
Start ();
Loop ();
}

}

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.