Note that there are some explanatory texts on larger screens.

plurals
  1. POEmbed wav in java?
    primarykey
    data
    text
    <p>I was writing a little alarm clock program that plays an audio file several times when the alarm time is up.</p> <p>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.</p> <p>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.</p> <p>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?)</p> <p>Thanks in advance! Sorry if this is too dumb. I'm a newbie!</p> <p>Here's my code for the AudioInputStream.</p> <pre><code> 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:=&gt; "+e); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload