gpt4 book ai didi

Javasound:AudioSystem.getAudioInputStream 在服务器上失败,在客户端代码中工作

转载 作者:行者123 更新时间:2023-12-01 12:30:32 25 4
gpt4 key购买 nike

我的应用程序涉及通过 Web UI 上传波形文件,将格式转换为 8k、8 位 mulaw 并将其存储在服务器上。我的代码在尝试执行以下操作时在服务器上失败:

final AudioInputStream ais = AudioSystem.getAudioInputStream( in );

错误是:

Caused by: javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1102) [classes.jar:1.6.0_65]

因此,我缩减到一个简单的测试用例,尝试将音频文件作为本地文件加载:

final File audioFile = new File("/path/to/audio.wav");
final InputStream in = new FileInputStream( file );
final AudioInputStream ais = AudioSystem.getAudioInputStream( in );

这有效。然后我回到服务器端并添加一些调试,如下所示:

    final byte[] audio = IOUtils.toByteArray( in );
final File audioLog = File.createTempFile( "audiolog", ".wav" );
IOUtils.write( audio, new FileOutputStream( audioLog ) );
s_logger.info( "File logged to: " + audioLog.getAbsolutePath() );
final InputStream byteIn = new ByteArrayInputStream( audio );
final AudioInputStream ais = AudioSystem.getAudioInputStream( byteIn );

这会以完全相同的方式失败。

然后,我将上传的原始文件与服务器上记录的音频文件进行比较,发现它们完全相同,甚至校验和:

$ cksum uploaded.wav 
4019972581 84076 uploaded.wav
$ cksum audiolog6415586848170376004.wav
4019972581 84076 audiolog6415586848170376004.wav

有什么想法可能会发生什么吗?我的服务器端代码在 JBoss 7.1 上运行。

谢谢。

-拉杰

最佳答案

以下方法可能值得尝试。

InputStream 中间步骤会执行各种测试,如果失败,可能会抛出 IOException。来自 api :

The implementation of this method may require multiple parsers to examine the stream to determine whether they support it. These parsers must be able to mark the stream, read enough data to determine whether they support the stream, and, if not, reset the stream's read pointer to its original position. If the input stream does not support these operation, this method may fail with an IOException.

我建议完全避免InputStream步骤,并将自己设置为通过AudioSystem.getAudioInputStream(File)AudioSystem.getAudioInputStream(URL)加载。这两个没有这个要求。

不过,我承认,这与您的写作不一致,即抛出的错误是UnsupportedAudioFileException,并且您能够使用此方法作为“本地文件”进行加载。在不同的情况下,这些测试的实现方式可能会有所不同。

关于Javasound:AudioSystem.getAudioInputStream 在服务器上失败,在客户端代码中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25954052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com