修订/总结:
我正在使用插件来解码 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
from时得到以下信息ProgressMonitorInputStream
:
java.io.IOException: mark/reset not supported
下面的评论确认 AudioInputStream 需要它包装的 InputStream 来支持 mark() 和 reset() 方法,而 ProgressMonitorInputStream 显然不支持。
下面的另一个建议是用 BufferedInputStream 包装 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));
...
}
现在这个构建和执行没有错误。然而,ProgressMonitor 从未出现,尽管 setMillisToPopup(10) 和 setMillisToDecideToPopup(10); 我的理论是,将未解码的音频实际读入内存的时间仍然快于 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
解码过程?我的(不满意的)后备是显示一个忙碌的光标。对实现目标的其他方法有什么建议 - 向用户提供视觉反馈,至少估计完成解码的剩余时间?