我在选择wav时遇到问题。附近是代码:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
                            "mp3 & wav Images", "wav", "mp3");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(getParent());
if(returnVal == JFileChooser.APPROVE_OPTION) {
        System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
}

InputStream in = new FileInputStream(chooser.getSelectedFile().getAbsolutePath());
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);


我在eclipse中遇到了AudioStream和AudioPlayer的错误,我使用JDK7。如何解决该问题?

最佳答案

尝试以下对我有用的代码段:

...
URL url = ClassLoader.getResource(chooser.getSelectedFile().getAbsolutePath());
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(ais);
...


How to cast from InputStream to AudioInputStream中也讨论过

顺便说一句:您的代码在我的机器上工作正常...

10-08 17:31