我正在使用 mp3plugin.jar 库,我的音乐效果很好。唯一的问题是当我的程序启动时 JFrame 没有显示。在我添加音乐之前,它运行良好。这是我的音乐代码:
package com.org.pong;
import javax.sound.sampled.*;
import javax.sound.*;
import java.io.*;
import java.net.URL;
public class Music {
Music(String url) {
int total, totalToRead, numBytesRead, numBytesToRead;
byte[] buffer;
boolean stopped;
AudioFormat wav;
TargetDataLine line;
SourceDataLine lineIn;
DataLine.Info info;
File file;
FileInputStream fis;
// AudioFormat(float sampleRate, int sampleSizeInBits,
// int channels, boolean signed, boolean bigEndian)
wav = new AudioFormat(44100, 16, 2, true, false);
info = new DataLine.Info(SourceDataLine.class, wav);
buffer = new byte[1024 * 333];
numBytesToRead = 1024 * 333;
total = 0;
stopped = false;
if (!AudioSystem.isLineSupported(info)) {
System.out.print("no support for " + wav.toString());
}
try {
// Obtain and open the line.
lineIn = (SourceDataLine) AudioSystem.getLine(info);
lineIn.open(wav);
lineIn.start();
fis = new FileInputStream(file = new File(url));
totalToRead = fis.available();
while (total < totalToRead && !stopped) {
numBytesRead = fis.read(buffer, 0, numBytesToRead);
if (numBytesRead == -1)
break;
total += numBytesRead;
lineIn.write(buffer, 0, numBytesRead);
}
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} catch (FileNotFoundException nofile) {
nofile.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}
}
}