Reputation: 41
I was writing a little alarm clock program that plays an audio file several times when the alarm time is up.
The alarm side of the program is done. Now I'm down to playing the file. I use the jFileChooser to select the file. The javax.sound.sampled.AudioInputStream , javax.sound.sampled.AudioSystem and javax.sound.sampled.Clip to play my wav file. Working as well.
But I really just want to play one wav file and I also want to be able to move this alarm clock on multiple systems so I want to avoid using jFileChooser to chose my alarm tone. I don't want to have to browse and chose the file every time i run the program.
Would it be easier to give the file a relative path (in the same folder as the jar, for example) and then fix the path string? Or would it be easier to somehow make the wav a part of the jar file (which is what i would prefer as then i woudn't have to worry about the wav file at all. It would be just one jar file independently functioning as an alarm clock. A byte array,maybe?)
Thanks in advance! Sorry if this is too dumb. I'm a newbie!
Here's my code for the AudioInputStream.
public void getaudPath() {
try {
JFileChooser chooser;
String choosertitle=null;
chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"WAV only", "wav");
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
apath = (chooser.getSelectedFile()).toString();
File soundFile = new File(apath);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
URL url = this.getClass().getClassLoader().getResource(apath);
clip = AudioSystem.getClip();
clip.open(audioIn);
JOptionPane.showMessageDialog(this,"Got file, A-okay!");
} else {
apath = null;
}} catch (Exception e) {JOptionPane.showMessageDialog(this,"Error in getting sound file:=> "+e); }
}
Upvotes: 1
Views: 827
Reputation: 168795
Class.getResource("/path/to/the.wav)
to get an URL to the clip.File objects are required for AudioInputStream.
No they are not! Here is a copy/paste of the source to which I referred you on the Java Sound info. page.
import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;
public class LoopSound {
public static void main(String[] args) throws Exception {
URL url = new URL(
"http://pscode.org/media/leftright.wav");
Clip clip = AudioSystem.getClip();
// getAudioInputStream() also accepts a File or InputStream
AudioInputStream ais = AudioSystem.
getAudioInputStream( url );
clip.open(ais);
clip.loop(Clip.LOOP_CONTINUOUSLY);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// A GUI element to prevent the Clip's daemon Thread
// from terminating at the end of the main()
JOptionPane.showMessageDialog(null, "Close to exit!");
}
});
}
}
It uses an URL for getting the AudioInputStream
, not a File
.
The call to AudioSystem.getAudioInputStream
is overloaded to accept 5 different argument types. The most important/generic 3 are.
Upvotes: 5