我正在尝试在我的项目中放一些音乐或一小段声音。我从这里尝试一些帮助。有点工作,但我听不到任何声音。它只是说它在玩。
我找到了很多例子,但是没有任何工作对我有用。也许您可以帮助我使用该AudioStream,AudioPlayer。许多人正在使用it。如何在Java Eclipse 4.4.0中使用它?
import java.io.File;
import java.io.FileReader;
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;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import sun.audio.*;
public class zaciatky {
public static void main(String[] args) throws LineUnavailableException {
// TODO Auto-generated method stub
try {
AudioInputStream audoAudioInputStream=AudioSystem.getAudioInputStream(new File("D:\\Betka\\Dokumenty\\INF\\JAVA\\Saxik\\Music\\saxik.wav"));
Clip songik=AudioSystem.getClip();
songik.open(audoAudioInputStream);
songik.start();
System.out.println("Song is playing...");
System.out.println(songik.getFormat());
if(songik.isActive()) System.out.println("is active");
songik.stop();
if(songik.isActive()) System.out.println("is active");
else System.out.println("not active");
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
System.out.println("Error occures with playing the sound.");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Error occures with playing the sound.");
e.printStackTrace();
}
}
}