Classes that play sounds
Copy Code code as follows:
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.SourceDataLine;
Classes that play sounds
public class Playsounds extends Thread {
private String filename;
Public Playsounds (String wavfile) {
filename = System.getproperty ("User.dir") +wavfile;
}
public void Run () {
File Soundfile = new file (filename);
Audioinputstream audioinputstream = null;
try {
Audioinputstream = Audiosystem.getaudioinputstream (soundfile);
catch (Exception E1) {
E1.printstacktrace ();
Return
}
Audioformat format = Audioinputstream.getformat ();
Sourcedataline auline = null;
Dataline.info Info = new Dataline.info (sourcedataline.class, format);
try {
Auline = (sourcedataline) audiosystem.getline (info);
Auline.open (format);
catch (Exception e) {
E.printstacktrace ();
Return
}
Auline.start ();
int nbytesread = 0;
It's a buffer.
byte[] Abdata = new byte[512];
try {
while (Nbytesread!=-1) {
Nbytesread = Audioinputstream.read (abdata, 0, abdata.length);
if (nbytesread >= 0)
Auline.write (abdata, 0, Nbytesread);
}
catch (IOException e) {
E.printstacktrace ();
Return
finally {
Auline.drain ();
Auline.close ();
}
}
}
The following is a Java application that plays sound, which can play the sound one time and loop the sound
Musicpaly mymusicplay = new Musicplay (GetClass (). GetResource ("/music/button.wav"));
Mymusicplay.start ()//play Once
Mymusicplay. Stop ();/stop
Mymusicplay. Continuousstart ()//loop playback
Mymusicplay. Continuousstop ()/Stop
Copy Code code as follows:
FileName: Muiscplay.java
Import java.io.*;
Import Java.net.URL;
Import sun.audio.*;
/**
*
* @author Wuhuiwen
* Play audio files to produce sound effects
*/
public class Musicplay {
Private audiostream as; Play a sound with a single time
Continuousaudiodatastream cas;//Loop Play sound
Constructors
Public musicplay (URL url)
{
try {
Open a sound file stream as input
as = new Audiostream (Url.openstream ());
catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Play Start once
public void Start ()
{
if (as==null) {
System.out.println ("Audiostream object is not created!");
Return
}else{
AudioPlayer.player.start (AS);
}
}
Play Stop once
public void Stop ()
{
if (as==null) {
System.out.println ("Audiostream object is not created!");
Return
}else{
AudioPlayer.player.stop (AS);
}
}
Loop Play Start
public void Continuousstart ()
{
Create Audiodata Source.
Audiodata data = null;
try {
data = As.getdata ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Create Continuousaudiodatastream.
cas = new Continuousaudiodatastream (data);
Play audio.
AudioPlayer.player.start (CAS);
}
Loop playback Stop
public void Continuousstop ()
{
if (CAS!= null)
{
AudioPlayer.player.stop (CAS);
}
}
}