Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ul> <li>Add the audio clip to a Jar</li> <li>Add the Jar to the run-time class-path of the app. (not necessary if the clip and classes are in the same Jar).</li> <li>Use <code>Class.getResource("/path/to/the.wav)</code> to get an URL to the clip.</li> <li>Play the clip using code based on that seen in the <a href="https://stackoverflow.com/tags/javasound/info">Java Sound info. page</a>.</li> </ul> <blockquote> <p>File objects are required for AudioInputStream.</p> </blockquote> <p><strong>No they are not!</strong> Here is a copy/paste of the source to which I referred you on the Java Sound info. page.</p> <pre><code>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!"); } }); } } </code></pre> <p>It uses an <strong>URL</strong> for getting the <code>AudioInputStream</code>, not a <code>File</code>. </p> <p>The call to <code>AudioSystem.getAudioInputStream</code> is overloaded to accept 5 different argument types. The most important/generic 3 are.</p> <ul> <li><a href="http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioSystem.html#getAudioInputStream%28java.io.File%29" rel="nofollow noreferrer"><code>File</code></a></li> <li><a href="http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioSystem.html#getAudioInputStream%28java.io.InputStream%29" rel="nofollow noreferrer"><code>InputStream</code></a> <strong>&amp;</strong></li> <li><a href="http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioSystem.html#getAudioInputStream%28java.net.URL%29" rel="nofollow noreferrer"><code>URL</code></a></li> </ul>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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