How can I decode OGG vorbis data from a ByteBuffer?

时间秒杀一切 提交于 2019-12-25 09:27:01

问题


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

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