将AudioInputStream与ProgressMonitorInputStream一起使用

时间:2013-01-02 02:01:20

标签: java inputstream ioexception javasound progressmonitor

修订/概述:

我使用插件解码MP3音频文件。我想提供一个ProgressMonitor来向用户提供反馈。构造解码MP3格式AudioFile的AudioInputStream的逻辑如下:

readAudioFile(File pAudioFile) throws UnsupportedAuioFileException, IOException {
    AudioInputStream nativeFormatStream = AudioSystem.getAudioInputStream(pAudioFile);
    AudioInputStream desiredFormatStream = AudioSystem.getAudioInputStream(AUDIO_OUTPUT_FORMAT,nativeFormatStream);
    int bytesRead, bufferLength;
    byte[] rawAudioBuffer[bufferLength=4096];
    bytesRead=desiredFormatStream.read(rawAudioBuffer,0,bufferLength));
   ...
}

首次尝试使用ProgressMontorInputStream包装音频文件,然后从中获取AudioInputStream:

readAudioFile(File pAudioFile) throws UnsupportedAuioFileException, IOException {
    ProgressMonitorInputStream monitorStream = new ProgressMonitorInputStream(COMP,"Decoding",new FileInputStream(pAudioFile);
    AudioInputStream nativeFormatStream = AudioSystem.getAudioInputStream(monitorStream);
    AudioInputStream desiredFormatStream = AudioSystem.getAudioInputStream(AUDIO_OUTPUT_FORMAT,nativeFormatStream);
    int bytesRead, bufferLength;
    byte[] rawAudioBuffer[bufferLength=4096];
    bytesRead=desiredFormatStream.read(rawAudioBuffer,0,bufferLength));
   ...
}

在构建时,在执行时,我从AudioInputStream构建ProgressMonitorInputStream时得到以下内容:

java.io.IOException: mark/reset not supported

下面的评论确认AudioInputStream需要它包装的InputStream来支持mark()和reset()方法,显然ProgressMonitorInputStream没有。

下面的另一个建议是使用BufferedInputStream(它支持mark / reset)包装ProgressMonitorInputStream。那么我有:

readAudioFile(File pAudioFile) throws UnsupportedAuioFileException, IOException {
    ProgressMonitorInputStream monitorStream = new ProgressMonitorInputStream(COMP,"Decoding",new FileInputStream(pAudioFile);
    AudioInputStream nativeFormatStream = AudioSystem.getAudioInputStream(new BufferedInputStream(monitorStream));
    AudioInputStream desiredFormatStream = AudioSystem.getAudioInputStream(AUDIO_OUTPUT_FORMAT,nativeFormatStream);
    int bytesRead, bufferLength;
    byte[] rawAudioBuffer[bufferLength=4096];
    bytesRead=desiredFormatStream.read(rawAudioBuffer,0,bufferLength));
   ...
}

现在这种构建和执行没有错误。但是,尽管setMillisToPopup(10)和setMillisToDecideToPopup(10)具有激进的设置,但ProgressMonitor永远不会出现;我的理论是,将未解码的音频实际读入内存的时间仍然快于10毫秒。实际上花在从磁盘读取后解码原始音频的时间。所以下一步是在构造解码AudioInputStream之前用ProgressMonitorInputStream包装未解码的AudioInputStream:

readAudioFile(File pAudioFile) throws UnsupportedAuioFileException, IOException {
    AudioInputStream nativeFormatStream = AudioSystem.getAudioInputStream(pAudioFile);
    AudioInputStream desiredFormatStream = AudioSystem.getAudioInputStream(AUDIO_OUTPUT_FORMAT,new BufferedInputStream(new ProgressMonitorInputStream(COMP,"Decoding",nativeFormatStream);
    int bytesRead, bufferLength;
    byte[] rawAudioBuffer[bufferLength=4096];
    bytesRead=desiredFormatStream.read(rawAudioBuffer,0,bufferLength));
   ...
}

我似乎在踢路,但没有取得进展。这个问题有解决方法吗?是否有另一种方法为解码过程提供ProgressMonitor?我(不满意)后备正在显示一个忙碌的光标。有关实现目标的其他方法的任何建议 - 为用户提供视觉反馈,至少估计剩余时间以完成解码?

0 个答案:

没有答案