我正在尝试在 Java 中的不同时间播放多个声音文件。我通过停止一个文件然后在几秒钟后启动另一个文件来做到这一点。但是,每当我这样做时,每当我尝试切换文件时,Java 都会冻结或抛出错误。这是我用来播放不同文件的功能:
public void backgroundSound(String musicFile){
System.out.println("1");
if(this.clip!= null){
if(this.clip.isRunning()){
this.clip.close();
}
}
System.out.println("2");
try{
File soundFile = new File(musicFile);
AudioInputStream audioIn =
AudioSystem.getAudioInputStream(soundFile);
this.clip = AudioSystem.getClip();
this.clip.open(audioIn); // line 538
this.clip.start();
this.clip.loop(Clip.LOOP_CONTINUOUSLY);
}
catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
任何帮助,将不胜感激。
this.clip.flush()
这是使用而不是时的堆栈跟踪this.clip.close()
:
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.
at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:492)
at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1278)
at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:107)
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1059)
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1149)
at MyGame.backgroundSound(MyGame.java:538)
at MyGame.actionPerformed(MyGame.java:161)
at javax.swing.Timer.fireActionPerformed(Timer.java:291)
at javax.swing.Timer$DoPostEvent.run(Timer.java:221)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:666)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:627)
at java.awt.EventQueue$2.run(EventQueue.java:625)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:636)
...