java版的speex编码和解码器,亲测好用
引入pom.xml
<dependency>
<groupId>com.orctom</groupId>
<artifactId>speex4j</artifactId>
<version>1.0.3</version>
</dependency>
测试
package com.iflytek.rd.jobs;
import com.google.common.base.Stopwatch;
import com.orctom.speex4j.SpeexDecoder;
import com.orctom.speex4j.SpeexEncoder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import java.io.*;
/**
* @Desc test
* @Author wadu
* @Date 2020/9/14
* @Version 1.0
**/
@Slf4j
public class Test {
public void testDecoder() throws IOException {
byte[] bytes = FileUtils.readFileToByteArray(new File("/home/data/test.speex"));
try (SpeexDecoder decoder = new SpeexDecoder()) {
byte[] pcm = decoder.decode(bytes);
File decodedFile = new File("/home/data/speex.pcm");
//SpeexUtils自带工具类中有pcm和wav互转功能
if (decodedFile.exists()) {
decodedFile.delete();
}
OutputStream out = null;
try {
out = new FileOutputStream(decodedFile);
out.write(pcm);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void testEncode() throws Exception {
byte[] bytes = FileUtils.readFileToByteArray(new File("/home/data/speex.pcm"));
try (SpeexEncoder encoder = new SpeexEncoder()) {
Stopwatch stopwatch = Stopwatch.createStarted();
byte[] spx = encoder.encode(bytes);
File encodedFile = new File("/home/data/test2.speex");
if (encodedFile.exists()) {
encodedFile.delete();
}
OutputStream out = new FileOutputStream(encodedFile);
out.write(spx);
out.close();
}
}
}
资料: