我試圖打開音頻文件,但它不起作用。 我不知道這是操作系統還是其他的東西,但是當我在Windows上時,我沒有這個問題。 我得到一個NullPointerException這裏是錯誤; 我嘗試了一個applet,bufferedInputStream,FileStream,AudioInputStream。 沒有用。 我在Linux上。打開音頻時出現NullPointerException
Exception in thread "main" java.lang.NullPointerException
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:130)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1111)
at com.base.engine.core.Audio.initAudio(Audio.java:29)
at com.base.engine.core.Audio.<init>(Audio.java:18)
at com.base.engine.game.Game.init(Game.java:43)
at com.base.engine.core.Starter.init(Starter.java:19)
at com.base.engine.core.Starter.run(Starter.java:43)
at com.base.engine.core.Starter.start(Starter.java:27)
at com.base.engine.game.Main.main(Main.java:11)
代碼
package com.base.engine.core;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Audio {
private Clip clip;
private int fPos = 0;
public Audio(String filename){
initAudio(filename);
}
private void initAudio(String filename){
try {
clip = AudioSystem.getClip();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
AudioInputStream stream = AudioSystem.getAudioInputStream(Audio.class.getResourceAsStream("res/sounds/"+filename));
clip.open(stream);
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void play(){
clip.setFramePosition(0);
clip.start();
fPos = clip.getFramePosition();
}
public void playAt(int pos){
clip.setFramePosition(pos);
clip.start();
fPos = clip.getFramePosition();
}
public void continuePlay(){
clip.setFramePosition(fPos);
clip.start();
fPos = clip.getFramePosition();
}
public void stop(){
fPos = clip.getFramePosition();
clip.stop();
}
}
我會檢查文件名的值,確保它存在於預期的位置(即您沒有C:\路徑)。 – scrappedcola 2014-11-03 17:38:02
如果'res'在'src'中,試着用一個正斜槓,即'/ res' – 2014-11-03 17:38:29
那麼我在調試模式下檢查了文件名的值,它說我的文件名爲「sounds.wav」,所以我不用我不認爲是這樣。 – user3779657 2014-11-03 17:39:16