Atitit java播放器调音速率快慢的实现
目录
FloatControl SAMPLE_RATE_control=(FloatControl)sourceDataLine.getControl(FloatControl.Type.SAMPLE_RATE);
SAMPLE_RATE_control.setValue(20000);
sourceDataLine.start();
PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
Exception in thread "main" java.lang.IllegalArgumentException: Unsupported control type: Sample Rate
at com.sun.media.sound.AbstractLine.getControl(AbstractLine.java:150)
at apkg.soundPlay.t(soundPlay.java:55)
at apkg.soundPlay.main(soundPlay.java:32)
只好使用第三方的了。第三方jl库也么有调整采样率的设置,
只好回到使用原生api
看到这个文章有了解决思路
- 使用Java增加/降低AudioInputStream的音频播放速度(Increase/decrease audio play speed of AudioInputStream with Java)
调节里面的 AudioFormat audioFormat2= new AudioFormat( 70000, 16, 2, true, false);速率即可
private static void fastPlay(String file) throws Exception {
AudioFormat audioFormat= new AudioFormat( 20000, 16, 2, true, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem
.getLine(info);
AudioFormat audioFormat2= new AudioFormat( 70000, 16, 2, true, false);
sourceDataLine.open(audioFormat2); //this audioformat can overwrite last DataLine.Info.audioFormat
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(file));
sourceDataLine.start();
int cnt;
// 读取数据到缓存数据
byte[] tempBuffer = new byte[10000] ;
while ((cnt = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) {
if (cnt > 0) {
// 播放缓存数据
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
// Block等待临时数据被输出为空
sourceDataLine.drain();
sourceDataLine.close();
}
JavaAPI方式语音识别mp3转换为pcm.html