Flex - Java byte to mp3

2019-08-02 23:47发布

I've a problem in converting byte to .mp3 sound file. In my case I do it using FileOutputStream using its write(bytes) method but it just creates a data file with mp3 extension but I cannot play it in any player on my PC.

Note: I'm recording it from Flex Michrophone and send ByteArray to java.

Which libraries should I use to add mp3 sound file headers etc. in java?

UPDATE: I couldn't even convert my raw data to Wave format that is supported by java sound api.. It creates for me sound with recorded sound but with a noise - where's the problem?

Here's my code for wave:

AudioFormat format = new AudioFormat(Encoding.PCM_SIGNED, 44100, 16, 2, 2, 44100, true);

ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
AudioInputStream stream = new AudioInputStream(bais, format, bytes.length/format.getFrameSize());


AudioSystem.write(stream, AudioFileFormat.Type.WAVE, new File(path+"zzz.wav")); 

What's wrong with my AudioFormat??? And which one do I have to use in MP3 case ?!

Urgent help! Any help will be highly appreciated!

1条回答
Viruses.
2楼-- · 2019-08-03 00:17

Just writing the raw bytes to a file with the name extension .mp3 doesn't magically convert the data to the MP3 format. You need to use an encoder to compress it.

A quick google search found LAMEOnJ which is a Java API for the popular LAME MP3 encoder.

查看更多
登录 后发表回答