Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to play music in Java program
    primarykey
    data
    text
    <p>I'm trying to get a test program working that plays a sound file from my hard drive, but I continue to get <code>NullPointerException</code>. Here's the code so far, which I primarily ripped from <a href="http://www.dreamincode.net/forums/topic/14083-incredibly-easy-way-to-play-sounds/" rel="nofollow">dream in code</a>:</p> <pre><code>package ForSelf; import java.applet.*; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.event.ActionEvent; import javax.swing.*; import java.net.*; public class AudioTest extends JApplet{ JPanel playerPanel; JButton playSound, stopSound; public class Sound // Holds one audio file { private AudioClip song; // Sound player private URL songPath; // Sound path Sound(String filename){ try { songPath = new URL(getCodeBase(),filename); // Get the Sound URL song = Applet.newAudioClip(songPath); // Load the Sound }catch(Exception e){ e.printStackTrace(); //e.getMessage(); } // Satisfy the catch } public void playSound(){ song.loop(); // Play } public void stopSound(){ song.stop(); // Stop } public void playSoundOnce(){ song.play(); // Play only once } } public void init(){ Sound testsong = new Sound("C:\\Users\\MyName\\Music\\PuzzleSolutionGet.wav"); Container c = getContentPane(); c.setBackground(Color.white); c.setLayout(null); playerPanel = new JPanel(); playSound = new JButton("Play"); stopSound = new JButton("Stop"); playerPanel.add(playSound); playerPanel.add(stopSound); c.add(playerPanel); testsong.playSound(); } public void paint(Graphics g){ super.paint(g); playerPanel.setLocation(0, 0); playerPanel.setSize(300, 300); } public void actionPerformed(ActionEvent e){ } } </code></pre> <p>The JComponents will be implemented later to play and stop the song file with the I'm not sure if it's just my file path or what, so any help would be appreciated.</p> <p>The updated code is the following:</p> <pre><code>package ForSelf; import java.applet.*; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.event.ActionEvent; import javax.swing.*; import java.net.*; public class AudioTest extends JApplet{ JPanel playerPanel; JButton playSound, stopSound; public class Sound // Holds one audio file { private AudioClip song; // Sound player private URL songPath; // Sound path Sound(String filename){ try { songPath = new URL(getCodeBase(),filename); // Get the Sound URL song = Applet.newAudioClip(songPath); // Load the Sound }catch(Exception e){ e.printStackTrace(); //e.getMessage(); } // Satisfy the catch } public void playSound(){ song.loop(); // Play } public void stopSound(){ song.stop(); // Stop } public void playSoundOnce(){ song.play(); // Play only once } } public void init(){ **String directory = System.getProperty("user.dir") + System.getProperty("file.separator"); String puzzleSolutionGet = directory + "PuzzleSolutionGet"; Sound testsong = new Sound(puzzleSolutionGet);** //Sound testsong = new Sound("C:/Users/MyName/Music/PuzzleSolutionGet.wav"); Container c = getContentPane(); c.setBackground(Color.white); c.setLayout(null); playerPanel = new JPanel(); playSound = new JButton("Play"); stopSound = new JButton("Stop"); playerPanel.add(playSound); playerPanel.add(stopSound); c.add(playerPanel); testsong.playSound(); } public void paint(Graphics g){ super.paint(g); playerPanel.setLocation(0, 0); playerPanel.setSize(300, 300); } public void actionPerformed(ActionEvent e){ } } </code></pre> <p>I got the following exceptions when I ran it, though:</p> <pre><code>java.net.MalformedURLException: unknown protocol: c at java.net.URL.&lt;init&gt;(URL.java:574) at java.net.URL.&lt;init&gt;(URL.java:464) at ForSelf.AudioTest$Sound.&lt;init&gt;(AudioTest.java:23) at ForSelf.AudioTest.init(AudioTest.java:45) at sun.applet.AppletPanel.run(AppletPanel.java:424) at java.lang.Thread.run(Thread.java:662) java.lang.NullPointerException at ForSelf.AudioTest$Sound.playSound(AudioTest.java:32) at ForSelf.AudioTest.init(AudioTest.java:62) at sun.applet.AppletPanel.run(AppletPanel.java:424) at java.lang.Thread.run(Thread.java:662) </code></pre> <p>This may prove more informative than before. I even moved the file to my desktop for faster access, but I'm certain the location isn't the issue.</p>
    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.
 

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