【发布时间】:2014-04-17 03:08:03
【问题描述】:
嘿,堆栈溢出。
我正在用 Java 创建一个播放列表播放器,到目前为止一切顺利,我把所有的逻辑都搞定了,项目即将完成。我们一直在通过创建一些大型播放列表来测试播放,然后让事情从头到尾进行。播放听起来不错,但有时音频会在最后被切断。这种情况很少发生。最后 x 秒(时间不同)未播放。
我测试的文件都是 16 或 24 位采样大小的 PCM 波形文件。我使用 Java 声音引擎结合 Java zooms mp3 和 ogg spi 来支持其他类型的音频文件。
到目前为止,我已经记录了几次,我的第一个想法是文件可能已损坏,事实并非如此。我已经尝试单独播放该文件,它可以完全播放!
我试图找出问题所在,但就是找不到。我不认为我的音频播放器有什么问题,我的想法不多了。
这是我创建音频输入流的方法:
public static AudioInputStream getUnmarkableAudioInputStream(Mixer mixer, File file)
throws UnsupportedAudioFileException
{
if (!file.exists() || !file.canRead()) {
return null;
}
AudioInputStream stream;
try {
stream = getAudioInputStream(file);
} catch (IOException e) {
logger.error("failed to retrieve stream from file", e);
return null;
}
AudioFormat baseFormat = stream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, baseFormat);
boolean supportedDirectly = false;
if (mixer == null) {
supportedDirectly = AudioSystem.isLineSupported(info);
} else {
supportedDirectly = mixer.isLineSupported(info);
}
// compare the AudioFormat with the desired one
if (baseFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED || !supportedDirectly) {
AudioFormat decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
false);
// convert the audio format to the supported one
if (AudioSystem.isConversionSupported(decodedFormat, baseFormat)) {
stream = AudioSystem.getAudioInputStream(decodedFormat, stream);
} else {
logger.debug(
"Audio format {} is not supported "
+ "and can not be converted to default format",
baseFormat.toString());
return null;
}
}
return stream;
}
这是我的音频播放器线程:
final class PlayerThread extends Thread
{
private byte[] buffer;
/**
* Initialize the buffer
*/
public void initBuffer()
{
linelock.lock();
try {
buffer = new byte[line.getBufferSize() / 5];
} finally {
linelock.unlock();
}
}
public void run()
{
initBuffer();
while (!isInterrupted()) {
checkState();
// if the line is just cleared go to the start of the loop
if (line == null || isInterrupted()) {
continue;
}
write();
}
// clean up all resources
close();
// change the state
state = Player.State.STOPPED;
}
private void checkState()
{
if (state != Player.State.PLAYING) {
if (line != null) {
line.flush();
}
try {
synchronized (this) {
this.wait();
}
} catch (InterruptedException e) {
// reset the interupt status
interrupt();
}
}
}
private void write()
{
// how much bytes could be written on the line
int available = line.available();
// is the space on the line big enough to write the buffer to
if (available >= buffer.length) {
// fill the buffer array
int read = 0;
try {
read = audioStream.read(buffer, 0, buffer.length);
} catch (Throwable ball) {
logger.error("Error in audio engine (read)", ball);
}
// if there was something to read, write it to the line
// otherwise stop the player
if (read >= 0) {
try {
linelock.lock();
line.write(buffer, 0, read);
} catch (Throwable ball) {
logger.error("Error in audio engine (write)", ball);
} finally {
linelock.unlock();
}
bytesRead += read;
} else {
line.drain();
MoreDefaultPlayer.this.stop();
}
}
}
private void close()
{
// invoke close on listeners
invokePlayerClosedOnListeners();
// destroy the volume chain
vc.removeVolumeListener(MoreDefaultPlayer.this);
// close the stream
try {
audioStream.close();
} catch (IOException e) {
logger.error("failed to close audio stream");
}
clearAllListeners();
linelock.lock();
try {
// quit the line
line.stop();
line.close();
line = null;
} finally {
linelock.unlock();
}
}
}
如您所见,我在之后排干了线路,所以我认为问题不是在播放流中的所有内容之前线路被关闭。
谁能看到这段代码可能有什么问题?
【问题讨论】:
-
你可以在
drain之后检查line.isActive(),看看是否所有的输出都完成了 -
1) 为了尽快获得更好的帮助,请发布MCVE(最小完整且可验证的示例)。 2) 您可以尝试使用更简单的
Clip来完成此任务。 -
嘿安德鲁,如果可以的话,我会的,但我似乎无法确定实际的错误。重现它的唯一方法是让玩家运行一整天左右。