我目前正在尝试使用 将音乐启动到我的程序中javax.sound.sampled
,并且我编写了一个名为 music 的方法,该方法应该在执行时启动音乐剪辑。它是这样的:
public void playMusic(){
try {
AudioInputStream astream = AudioSystem.getAudioInputStream(
newFileInputStream("bin/ctk_tune.mp3"));
AudioFormat baseFormat = astream.getFormat();
AudioFormat newFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
AudioInputStream dstream = AudioSystem.getAudioInputStream(
newFormat, astream);
Clip clip = AudioSystem.getClip();
clip.open(dstream);
clip.setFramePosition(0);
clip.start();
} catch(IOException ex) {
System.out.println("music not loaded : ");
ex.printStackTrace();
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
当我尝试运行它时,我得到了一个未捕获的异常(程序没有启动),它说:
Exception in thread "main" java.lang.NegativeArraySizeException
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:449)
at mainProgram.playMusic(mainProgram.java:211)
at mainProgram.<init>(mainProgram.java:67)
at Launcher.main(Launcher.java:16)