java wav 时间,Java-调整WAV文件的播放速度

I'm likely dense but I cannot seem to find a solution to my issue

(NOTE: I CAN find lots of people reporting this issue, seems like it happened as a result of newer Java (possible 1.5?). Perhaps SAMPLE_RATE is no longer supported? I am unable to find any solution).

I'm trying to adjust the SAMPLE_RATE to speed up/slow down song. I can successfully play a .wav file without issue, so I looked into FloatControl which worked for adjusting volume:

public void adjustVolume(String audioType, float gain) {

FloatControl gainControl = null;

gainControl = (FloatControl) clipSFX.getControl(FloatControl.Type.MASTER_GAIN);

if(gain > MAX_VOLUME)

gain = MAX_VOLUME;

if(gain < MIN_VOLUME)

gain = MIN_VOLUME;

//set volume

gainControl.setValue(gain);

}

But when trying to translate this principle to SAMPLE_RATE, I get an error very early on at this stage:

public void adjustVolume(String audioType, float gain) {

FloatControl gainControl = null;

gainControl = (FloatControl) clipSFX.getControl(FloatControl.Type.SAMPLE_RATE);

//ERROR: Exception in thread "Thread-3" java.lang.IllegalArgumentException: Unsupported control type: Sample Rate

//I haven't gotten this far yet since the above breaks, but in theory will then set value?

gainControl.setValue(gain);

}

Everything I've found online seems to be related to taking input from a mic or some external line and doesn't seem to translate to using an audio file, so I'm unsure what I'm missing. Any help would be appreciated! Thanks!

解决方案

Here we have a method that changes the speed - by doubling the sample rate. Basically the steps are as follows:

open the audio stream of the file

get the format

create a new format with the sample rate changed

open a data line with that format

read from the file/audio stream and play onto the line

The concepts here are SourceDataLine, AudioFormat and AudioInputStream. If you look at the javax.sound tutorial you will find them, or even the pages of the classes. You can now create your own method (like adjust(factor)) that just gets the new format and all else stay the same.

public void play() {

try {

File fileIn = new File(" ....);

AudioInputStream audioInputStream=AudioSystem.getAudioInputStream(fileIn);

AudioFormat formatIn=audioInputStream.getFormat();

AudioFormat format=new AudioFormat(formatIn.getSampleRate()*2, formatIn.getSampleSizeInBits(), formatIn.getChannels(), true, formatIn.isBigEndian());

System.out.println(formatIn.toString());

System.out.println(format.toString());

byte[] data=new byte[1024];

DataLine.Info dinfo=new DataLine.Info(SourceDataLine.class, format);

SourceDataLine line=(SourceDataLine)AudioSystem.getLine(dinfo);

if(line!=null) {

line.open(format);

line.start();

while(true) {

int k=audioInputStream.read(data, 0, data.length);

if(k<0) break;

line.write(data, 0, k);

}

line.stop();

line.close();

}

}

catch(Exception ex) { ex.printStackTrace(); }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值