Java player sound class and a simple example
- 2020-04-01 03:05:02
- OfStack
The class that plays the sound
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;
//The class that plays the sound
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;
//This is the 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 playback sound application that can play a single sound, loop sound
MusicPaly myMusicPlay = new MusicPlay(getClass(). GetResource ("/music/button.wav"));
MyMusicPlay. Start (); // play once
MyMusicPlay. Stop (); / / stop
MyMusicPlay. ContinuousStart (); // looping
MyMusicPlay. ContinuousStop (); / / stop
//The file name: MuiscPlay. Java
import java.io.*;
import java.net.URL;
import sun.audio.*;
public class MusicPlay {
private AudioStream as; //To play a single sound
ContinuousAudioDataStream cas;//Loop sound
//The constructor
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();
}
}
//Start of one play
public void start()
{
if( as==null ){
System.out.println("AudioStream object is not created!");
return;
}else{
AudioPlayer.player.start (as);
}
}
//Stop play once
public void stop()
{
if( as==null ){
System.out.println("AudioStream object is not created!");
return;
}else{
AudioPlayer.player.stop(as);
}
}
//Loop 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 stop
public void continuousStop()
{
if(cas != null)
{
AudioPlayer.player.stop (cas);
}
}
}