What exactly does AudioInputStream.read method return?

一世执手 提交于 2019-12-08 06:19:26

AudioInputStream read() method returns the raw audio data. You don't know what is the 'construction' of data before you read the audio format with getFormat() which returns AudioFormat. From AudioFormat you can getChannels() and getSampleSizeInBits() and more... This is because the AudioInputStream is made for known format.

If you calculate a sample value you have different possibilities with signes and endianness of the data (in case of 16-bit sample). To make a more generic code use your AudioFormat object returned from AudioInputStream to get more info about the data buffer:

  • encoding() : PCM_SIGNED, PCM_UNSIGNED ...
  • bigEndian() : true or false

As you already discovered the incorrect sample building may lead to some disturbed sound. If you work with various files it may case a problems in the future. If you won't provide a support for some formats just check what says AudioFormat and throw exception (e.g. javax.sound.sampled.UnsupportedAudioFileException). It will save your time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!