0
package net.NitroCruze.mrpg.baseengine.music;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;

public class CPSound implements Runnable{
    AudioInputStream as1;
    AudioFormat af;
    Clip clip1;
    DataLine.Info info;
    Line line1;

    public CPSound() {
        Thread soundThread;
        soundThread = new Thread(this, "Sound");
        soundThread.start();
    }

    public void play() {
        try{
            as1 = AudioSystem.getAudioInputStream(new FileInputStream(new File("/res/music.wav")));
            af = as1.getFormat();
            clip1 = AudioSystem.getClip();
            info = new DataLine.Info(Clip.class, af);
            line1 = AudioSystem.getLine(info);
        }
        catch(Exception e)
        {
        }

        if ( ! line1.isOpen() )
        {
            try
            {
                clip1.open(as1);
            }
            catch (Exception e)
            {  
            }
            clip1.loop(Clip.LOOP_CONTINUOUSLY);
            clip1.start();
        }
    }

    public void run()
    {
        play();
    }
}

Why does this not work? It gives me a NullPointerException?

Steve
  • 1
  • 2
  • 3
    where is your audio file placed? – jmj Jun 27 '14 at 19:48
  • 5
    Here is a clue: the exception includes a line number. look on that line or tell us the line number and indicate which line it is in your included code. – DwB Jun 27 '14 at 19:48
  • I'm assuming the error is at `as1 = AudioSystem.getAudioInputStream...`, since the file path is probably wrong. Where is your `.wav` file located? – tcooc Jun 27 '14 at 19:52
  • Well this question was posted for availing bounty--->http://stackoverflow.com/questions/24317210/no-sound-when-trying-to-play-audio-wav-files-in-java – Am_I_Helpful Jun 27 '14 at 19:52

1 Answers1

0

I found out why it wasn't working! I had put "/" before 'res/music.wav' which caused the file loading system to think I was loading from a DRIVE (eg C:/ F:/), but there was nothing before that / so it thought it was loading from a null drive, which cannot exist.

Steve
  • 1
  • 2