为什么这个WAV文件没有播放?它给了我一个NullPointerException

时间:2014-06-27 19:46:23

标签: java loops wav

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();
    }
}

为什么这不起作用?它给了我一个NullPointerException

1 个答案:

答案 0 :(得分:0)

我发现它为什么不起作用!我把" /"之前' res / music.wav'这导致文件加载系统认为我是从驱动器加载的(例如C:/ F:/),但之前没有任何东西/所以它认为它是从空驱动器加载的,它不能存在。