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文件的播放速度相关推荐

  1. java时间规划书_【计算机本科补全计划】Java学习笔记(九) Java日期时间

    正文之前 终于好像仿佛看完了菜鸟教程的Java课程,感觉自己收获颇丰!很好,Java看完之后正愁如何开始进阶呢!结果发现菜鸟还准备了Java实例这种好东西!简直就是教程界的良心啊 !!!没事,先写写笔 ...

  2. java awt android_Android开发基础之Java 日期时间

    Java 日期时间 java.util 包提供了 Date 类来封装当前的日期和时间. Date 类提供两个构造函数来实例化 Date 对象. 第一个构造函数使用当前日期和时间来初始化对象. Date ...

  3. 【总结】Java 日期时间

    Java 日期时间 Java Date 类 应用场景 常用方法 DateDemo Java SimpleDateFormat 类 应用场景 常用方法 SimpleDateDemo Java Calen ...

  4. java wav 切割_java切割音频文件

    工具: 一个jar包即可:jave-1.0.2.jar 可以切割wav格式的音频文件 完整工程目录 就一个jar包,一个main类 代码: package com.zit; import java.i ...

  5. java 修改wav文件头_使用Java聲音API從WAV文件中修剪開頭和結尾

    我有製作的基礎知識.但是,輸出文件一遍又一遍地重複WAV標頭字節.生成的文件大小合適,但是它與垃圾一起提交.使用Java聲音API從WAV文件中修剪開頭和結尾 我想使用一個擴展AudioInputSt ...

  6. Java程序获取和修改.wav音频文件的内部结构

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/79498075冷血之心的博客) wav音频文件是一种无损的音频文件, ...

  7. java wav文件_java播放wav文件

    STM32播放WAV程序_计算机软件及应用_IT/计算机_专业资料.此程序配置的采样率为16k,PWM频率144k; 此代码可以实现16位单声道的WAV文件解码,通过PWM 或者DAC输出音频.... ...

  8. 【JAVA】将多个WAV或MP3文件合成一个文件

    JAVA将多个WAV文件合成一个文件,或者多个mp3合成一个文件 话不多说 直接上源码 使用: public static void main(String[] args) throws IOExce ...

  9. Java日期时间调整的几种方式

    一.Calendar类 我们现在已经能够格式化并创建一个日期对象了,但是我们如何才能设置和获取日期数据的特定部分呢,比如说小时,日,或者分钟? 我们又如何在日期的这些部分加上或者减去值呢? 答案是使用 ...

最新文章

  1. nginx将泛解析的匹配域名绑定到子目录配置方法
  2. MPB:扬大林淼组-瘤胃内容物样本中有机酸的定量分析 (高效液相色谱)
  3. 2020年全国大学生智能车竞赛华南赛区线上比赛高校组合
  4. NR 5G UE初始接入流程
  5. Unity-WIKI 之 AnimationToPNG
  6. 关于angular的$resource中的isArray属性问题
  7. ajax 获取服务器返回的XML字符串
  8. 正确的 send recv 行为
  9. git无法pull仓库refusing to merge unrelated histories
  10. RabbitMQ 消息队列六种模式
  11. 抽象类,接口都与继承有关
  12. RocketMQ的核心概念讲解
  13. oracle查询包含某个字段的表
  14. javascript setTimeout 和 setInterval 区别
  15. XML万能数据库设计
  16. videojs重播_vue2.0 vue-video-player 直播hls 回放mp4
  17. Obective-C之宏定义
  18. js获取chrome浏览器版本信息
  19. 元宇宙引擎脑语言2500令v0.5.6
  20. 关闭远程服务器端口,远程端口 程序自动关闭问题解决方案

热门文章

  1. 制作openstack Centos镜像 -- Example: CentOS image
  2. [Android Pro] Android 6.0 Root
  3. bindService执行成功后,低概率出现onServiceConnected没有被调用
  4. 获得了知识(条件)后的概率称为后验概率 对先验概率的个性化调整
  5. ***大赛结果,名企员工缺乏安全意识
  6. 当DRM出错时的解决办法
  7. windows7下修改hosts文件无效解决办法
  8. SQL SERVER 2005 进行XML查询
  9. asp.net 按钮单击事件问题(自动弹出新窗口)
  10. 怎么在搭建Android开发环境?