I would like to run audio file from java and i read many codes in SO but unbale to run my file perhaps! Seems I have mentioned wrong path or using wrong lib . Please assist me what's wrong in below code to run mp3 or VLC .aac format file
public void playSound() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("D:/clinic/clinic/mysound.mp3").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch(Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
}
If you use the this.getClass.getResource() method instead of the File(file) method, maybe it would work. Remember that the file that the audio is in has to be in the same package as the class that is running it. If this doesn't work, then try it with a .wav file(you can use a .mp3 to .wav converter).
public void run() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(this.getClass().getResource("mysound.mp3"));
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (Exception ex) {
ex.printStackTrace();
}
}
I hope this helps.