我正在尝试打开音频文件,但它不起作用。 我不知道它是操作系统还是别的什么,但当我在Windows上时,我没有这个问题。 我得到一个NullPointerException这里是错误; 我尝试使用applet,bufferedInputStream,FileStream,AudioInputStream。 什么都行不通。 我在Linux上。
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();
}
}
答案 0 :(得分:0)
试试这个。如果File
存在,则只能进一步。
File f = new File("D:\\folder\\file.mp3");
System.out.print(f.exists());