how to play wav files in a java program on multiple devices

how to play .wav files in a java program on multiple devices!!! For instance when I send my source files to my brother they need to be able to play on his computer as well as mine?

Solution


package audioplay;

import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;

public class AudioPlay {

// size required to read the audio stream
private static final int MAX_FILE_SIZE = 4096;

//Function to play audio file
void playMusic(String Path) {
File wavFile = new File(Path);
try {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(wavFile);

AudioFormat format = audioStream.getFormat();

DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

SourceDataLine al = (SourceDataLine) AudioSystem.getLine(info);

al.open(format);

al.start();

System.out.println(\"Staring the Music....!!\");

byte[] bytesBuffer = new byte[MAX_FILE_SIZE];
int bytesRead = -1;

while ((bytesRead = audioStream.read(bytesBuffer)) != -1) {
al.write(bytesBuffer, 0, bytesRead);
}

al.drain();
al.close();
audioStream.close();

System.out.println(\"Music stopped.!!\");

} catch (UnsupportedAudioFileException ex) {
System.out.println(\"The audio file not supported.Please try again with another audio file...!!!\");
ex.printStackTrace();
} catch (LineUnavailableException ex) {
System.out.println(\"Error Occurred\");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println(\"Error..File may b corrupted.\");
ex.printStackTrace();
}
}

public static void main(String[] args) {
String audioFilePath = \"C:\\\\Users\\\\ttechnocraft\\\\Music\\\\adix.wav\";
AudioPlay player = new AudioPlay();
player.playMusic(audioFilePath);
}

}

 how to play .wav files in a java program on multiple devices!!! For instance when I send my source files to my brother they need to be able to play on his comp
 how to play .wav files in a java program on multiple devices!!! For instance when I send my source files to my brother they need to be able to play on his comp

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site