我希望我的Java程序播放声音,但我遇到了一些问题。
public static void main(String[] args) throws IOException, UnsupportedAudioFileException, LineUnavailableException {
AudioInputStream audioIn = AudioSystem.getAudioInputStream(new File("PTUK1.mp3"));
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
clip.stop();
}
我收到以下异常,“PTKU1.mp3”:
Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1187)
使用“PTUK2.wav”,我得到了这个例外:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.createStream(PulseAudioDataLine.java:142)
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:99)
at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:283)
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402)
at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453)
答案 0 :(得分:0)
如果你想播放mp3!
private void playMe(){
try{
Player player;
File file=new File(“F://a.mp3″);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
player.play();
}catch(Exception e){
System.out.print(“ERROR “+e);
}
}
请注意:
你必须拥有jlayer.jar 你必须有mp3plugin.jar 你可以得到它们www.javaextreme.wordpress.com