我对 Java 完全陌生。但我想给我的女朋友一个用 Java 写的小礼物。我拥有的程序本身并且它可以工作......但我想添加的是一个音频文件,只要程序正在运行,它就会自行循环。我浏览了很多东西,尝试了很多东西,但不知何故,我似乎犯了一个错误。是的,我知道有很多代码示例。它不是小程序。
那是下面的代码......它不应该是干净的代码,我只是希望我能以某种方式让它工作。如果有人可以提供帮助,我将不胜感激。
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Shore
*/
public class audio {
public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
File f = new File("C:\\Users\\Shore\\Documents\\NetBeansProjects\\Meli13\\src\\WW.wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(f);
BufferedInputStream bufferedInputStream = new BufferedInputStream(audioInputStream);
AudioFormat af = audioInputStream.getFormat();
int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
byte[] audio = new byte[size];
DataLine.Info info = new DataLine.Info(Clip.class, af, size);
bufferedInputStream.read(audio, 0, size);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(af, audio, 0, size);
clip.start();
}
}
非常感谢。WW.wav 在同一个文件夹中。