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

标签 java audio javasound

我的应用程序涉及通过 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/

相关文章:

javascript - 如何测试声音是否在 SoundManager2 中播放?

java - 音频:更改字节数组中样本的音量

java - BigClip 无法播放 .wav 文件

java - 为什么 RGBA 位图需要 3x3 数组来存储 1 个像素,如何避免这种情况?

java - 在 JAVA 中将 INFINITY 值打印到其他内容

java - 如何在 JUnit 测试中强制 Mockito 抛出 RemoteException

java - 将音频添加到这些波形公式中

java - LinearLayout 不显示以编程方式添加的图像

c# - 加载关卡后声音无法播放

iphone - Speakhere 接听电话后不会录音