Java音频播放器最终会削减音频

时间:2014-04-10 10:59:02

标签: java audio javasound

嘿堆栈溢出。

我正在用Java创建一个播放列表播放器,到目前为止一切顺利,我把所有逻辑都搞定了,项目即将完成。我们一直在通过创建一些大型播放列表来测试播放,然后让事情从头到尾。播放听起来不错,但有时音频会在最后被切断。这很少发生。最后x秒(时间变化)不会播放。

我测试的文件都是16或24位采样大小的PCM波形文件。我使用Java声音引擎结合Java缩放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();
        }
    }
}

正如你所看到的那样,我之后排空了线路,所以我不认为问题是在播放流中的所有内容之前关闭线路。
任何人都可以看到这段代码可能有什么问题吗?

1 个答案:

答案 0 :(得分:0)

我没有看到明显的答案,但有些事情会为我带来黄旗。通常的做法是将line.write()方法放在while循环中,而不是重复调用它。通常不需要测试line.available()或处理锁定行。如果可用行上没有空格,方法line.write()将处理必要的阻塞。我总是被警告不要不必要地锁定或阻塞音频线。

锁定逻辑是处理队列序列的一个组成部分吗?您描述的错误可能在于处理。 (与缓冲区大小相比,可能与available()的测试相互作用?截止量是否大致等于缓冲区大小?)

我会考虑实现一个LineListener来宣告cue何时结束,并使该事件成为下一个cue播放的触发器。当给定文件完成时,可以发出STOP类型的LineEvent,通知处理队列的任何处理以继续下一个文件。