public static byte [] getAudioDataBytes(byte [] sourceBytes, AudioFormat audioFormat) throws UnsupportedAudioFileException, IllegalArgumentException, Exception{
if(sourceBytes == null || sourceBytes.length == 0 || audioFormat == null){
throw new IllegalArgumentException("Illegal Argument passed to this method");
}
ByteArrayInputStream bais = null;
ByteArrayOutputStream baos = null;
AudioInputStream sourceAIS = null;
AudioInputStream convert1AIS = null;
AudioInputStream convert2AIS = null;
try{
bais = new ByteArrayInputStream(sourceBytes);
sourceAIS = AudioSystem.getAudioInputStream(bais);
AudioFormat sourceFormat = sourceAIS.getFormat();
AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels()*2, sourceFormat.getSampleRate(), false);
convert1AIS = AudioSystem.getAudioInputStream(convertFormat, sourceAIS);
convert2AIS = AudioSystem.getAudioInputStream(audioFormat, convert1AIS);
baos = new ByteArrayOutputStream();
byte [] buffer = new byte[8192];
while(true){
int readCount = convert2AIS.read(buffer, 0, buffer.length);
if(readCount == -1){
break;
}
baos.write(buffer, 0, readCount);
}
return baos.toByteArray();
} catch(UnsupportedAudioFileException uafe){
//uafe.printStackTrace();
throw uafe;
} catch(IOException ioe){
//ioe.printStackTrace();
throw ioe;
} catch(IllegalArgumentException iae){
//iae.printStackTrace();
throw iae;
} catch (Exception e) {
//e.printStackTrace();
throw e;
}finally{
if(baos != null){
try{
baos.close();
}catch(Exception e){
}
}
if(convert2AIS != null){
try{
convert2AIS.close();
}catch(Exception e){
}
}
if(convert1AIS != null){
try{
convert1AIS.close();
}catch(Exception e){
}
}
if(sourceAIS != null){
try{
sourceAIS.close();
}catch(Exception e){
}
}
if(bais != null){
try{
bais.close();
}catch(Exception e){
}
}
}
}
Here sourceBytes represents MP3 file or WAV file. audioFormat is PCM format in which you want conversion. Also we need to put mp3spi.jar, tritonus_mp3.jar, jl*.jar, tritonus_share.jar from javazoom.com in classpath. Hope this may help to other.
Java 7 version:
public static byte [] getAudioDataBytes(byte [] sourceBytes, AudioFormat audioFormat) throws UnsupportedAudioFileException, IllegalArgumentException, Exception {
if(sourceBytes == null || sourceBytes.length == 0 || audioFormat == null){
throw new IllegalArgumentException("Illegal Argument passed to this method");
}
try (final ByteArrayInputStream bais = new ByteArrayInputStream(sourceBytes);
final AudioInputStream sourceAIS = AudioSystem.getAudioInputStream(bais)) {
AudioFormat sourceFormat = sourceAIS.getFormat();
AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels()*2, sourceFormat.getSampleRate(), false);
try (final AudioInputStream convert1AIS = AudioSystem.getAudioInputStream(convertFormat, sourceAIS);
final AudioInputStream convert2AIS = AudioSystem.getAudioInputStream(audioFormat, convert1AIS);
final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte [] buffer = new byte[8192];
while(true){
int readCount = convert2AIS.read(buffer, 0, buffer.length);
if(readCount == -1){
break;
}
baos.write(buffer, 0, readCount);
}
return baos.toByteArray();
}
}
}
Maven:
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>mp3spi</artifactId>
<version>1.9.5-1</version>
</dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>jlayer</artifactId>
<version>1.0.1-1</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…