日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發(fā)布時間:2024/9/19 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java wav 时间,Java-调整WAV文件的播放速度 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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(); }

}

總結

以上是生活随笔為你收集整理的java wav 时间,Java-调整WAV文件的播放速度的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。