问题
The libraries I founded so far only have methods to decode from a file or InputStream
. I have a ByteBuffer
with OGG vorbis data and I need it decoded to PCM without having to write it to a file first.
回答1:
There seem to be 2 parts to this problem. 1) Getting Java Sound to deal with OGG Vorbis format. 2) Avoiding the File.
For (1), the Java Sound API allows the addition of extra formats via the Service Provider Interface. The idea is to put an encoder/decoder into a Jar and use a standard path and format of file to identify the class that does the encoding/decoding.
For (2), it is simply a matter of supplying an InputStream and required AudioFormat to the relevant methods of the AudioSystem static functions. E.G. (Pseudo code..)
byte[] b = byteBuffer.array();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
InputStream is = new InputStream(bais);
AudioInputStrream aisOgg = AudioSystem.getAudioInputStream(is);
AudioInputStrream aisPcm = AudioSystem.
getAudioInputStream(pcmAudioFormat, aisOgg);
回答2:
You can use ByteArrayInputStream which is a subclass of InputStream. If your stream is very large you probably will have to write to file.
来源:https://stackoverflow.com/questions/3987518/how-can-i-decode-ogg-vorbis-data-from-a-bytebuffer