Sound (audio file) player in java - working source code example
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/sound-audio-file-player-in-java-working.html
Sound (audio file) player in java - working source code example
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Player implements Runnable {
static final int BUF_SIZE = 16384;
private AudioInputStream audioInputStream;
private AudioFormat format;
SourceDataLine line;
Thread thread;
public Player(AudioInputStream audioInputStream) {
this.audioInputStream = audioInputStream;
format = audioInputStream.getFormat();
}
public Player(File wavFile) throws UnsupportedAudioFileException, IOException {
this.audioInputStream = AudioSystem.getAudioInputStream(wavFile);
format = audioInputStream.getFormat();
}
public void start() {
thread = new Thread(this);
thread.setName("Playback");
thread.start();
}
public void stop() {
thread = null;
}
private void shutDown(final String message) {
if (thread != null) {
thread = null;
}
System.out.println(message);
}
@Override
public void run() {
// make sure we have something to play
if (audioInputStream == null) {
shutDown("No loaded audio to play back");
return;
}
// get an AudioInputStream of the desired format for playback
final AudioInputStream playbackInputStream = AudioSystem.getAudioInputStream(format, audioInputStream);
if (playbackInputStream == null) {
shutDown("Unable to convert stream of format " + audioInputStream + " to format " + format);
return;
}
line = getSourceDataLineForPlayback();
// play back the captured audio data
final int frameSizeInBytes = format.getFrameSize();
final int bufferLengthInFrames = line.getBufferSize() / 8;
final int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
final byte[] audioBuffer = new byte[bufferLengthInBytes];
int numBytesRead = 0;
// start the source data line
line.start();
while (thread != null) {
try {
if ((numBytesRead = playbackInputStream.read(audioBuffer)) == -1) {
break;
}
int numBytesRemaining = numBytesRead;
while (numBytesRemaining > 0) {
numBytesRemaining -= line.write(audioBuffer, 0, numBytesRemaining);
}
} catch (final Exception e) {
shutDown("Error during playback: " + e);
break;
}
}
// stop and close the line.
if (thread != null) {
line.drain();
}
line.stop();
line.close();
line = null;
thread = null;
}
private SourceDataLine getSourceDataLineForPlayback() {
SourceDataLine line;
final DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
return null;
}
// get and open the source data line for playback.
try {
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, BUF_SIZE);
} catch (final LineUnavailableException ex) {
return null;
}
return line;
}
}
Testing the wave sound player
public class Testss {
public static void main(String[] args) throws Exception {
Player pl = new Player(new File("filename.wav"));
pl.start();
Thread.sleep(2000);
}
}
Sound (audio file) player in java - working source code example的更多相关文章
- Java Collections Source Code Series 2 ---接口
废话开篇 自己学完Java Collections框架之后,其中的一个较大的收获就是接口对于层次的重要性.Java Collections的最终实现至少有几十个,其中很多都有非常相似的功能(metho ...
- Java Collections Source Code Series 1 --- 简介
废话开篇 由于项目需要,需要对Java Collections进行系统地了解,所以在此记录下,方便自己,服务他人. Java Collections 简介 Java Collections 框架主要包 ...
- [转]Native Java Bytecode Debugging without Source Code
link from:http://www.crowdstrike.com/blog/native-java-bytecode-debugging-without-source-code/index.h ...
- Java Sound : generate play sine wave - source code
转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-sound-generate-play-sine-wave.html Work ...
- How to Convert a Class File to a Java File?
What is a programming language? Before introducing compilation and decompilation, let's briefly intr ...
- py, pyc, pyw, pyo, pyd Compiled Python File (.pyc) 和Java或.NET相比,Python的Virtual Machine距离真实机器的距离更远
https://my.oschina.net/renwofei423/blog/17404 1. PyCodeObject与Pyc文件 通常认为,Python是一种解释性的语言,但是这种说法 ...
- Artistic Style 3.1 A Free, Fast, and Small Automatic Formatter for C, C++, C++/CLI, Objective‑C, C#, and Java Source Code
Artistic Style - Index http://astyle.sourceforge.net/ Artistic Style 3.1 A Free, Fast, and Small Aut ...
- Combining an audio file with video file in python
Combining an audio file with video file in python - Stack Overflow https://stackoverflow.com/questio ...
- [idea]Error:java: invalid source release: 1.8 标签: idea 2017-02-24 15:50 961人阅读
最近用idea敲struts,虽然idea的界面很好看,代码提示也很强大,不过也的确是碰到了一些在eclipse上从来没有碰到过的问题,而且我发现,idea的错误,很多都是在外国的网站上提问的人比较多 ...
随机推荐
- [CodeForces - 906D] Power Tower——扩展欧拉定理
题意 给你 $n$ 个 $w_i$ 和一个数 $p$,$q$个询问,每次询问一个区间 $[l,r] $,求 $w_l ^{w_{l+1}^{{\vdots}^{w_r}}} \ \% p$ 分析 由扩 ...
- Mac上搭建Python集成环境
Jenkins安装 第一种方式下载安装包 官网 https://jenkins.io/download/ 下载安装包 第二种通过homebrew安装,前提需要安装jdk(推荐) brew instal ...
- Java入门程序HelloWord
Java程序开发三步骤:编写,编译,运行 编译器(编译):javac.exe 解释器(运行):java.exe 编译:把我们能看得懂的java代码(xxx.java)翻译成jvm可以运行的java字节 ...
- 通过类型断言获取error类型,获得更详细的信息
package main import ( "fmt" "os" ) func main() { f, err := os.Open("/test.t ...
- Vue模板语法(二)
Vue基础模板语法 二 1. 样式绑定 1.1 class绑定 使用方式:v-bind:class="expression" expression的类型:字符 ...
- AQS面试题
问:什么是AQS? 答:AQS的全称为(AbstractQueuedSynchronizer),这个类在java.util.concurrent.locks包下面.AQS是一个用来构建锁和同步器的框架 ...
- 深入浅出MYSQL数据库—思维导图[附下载链接]
源文件下载地址:https://github.com/JluTiger/schoolRecruit2020
- [内网渗透]MS14-068复现(CVE-2014-6324)
0x01 简介 在做域渗透测试时,当我们拿到了一个普通域成员的账号后,想继续对该域进行渗透,拿到域控服务器权限.如果域控服务器存在MS14_068漏洞,并且未打补丁,那么我们就可以利用MS14_068 ...
- Zrender:实现波浪纹效果
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- SpringBoot配置虚拟化路径用于图片的展示
前言:springboot默认可以访问resources下的static文件夹下的静态资源,我们一般将图片指定上传到static下的某个文件夹,例如images,开发阶段可以使用,但是当项目打成jar ...