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的错误,很多都是在外国的网站上提问的人比较多 ...
随机推荐
- c++中如何使用memset()
转载链接1 转载链接2
- js select 默认回显判断
<select id="dataselect" class="input-medium" style="width: 20%"> ...
- Dubbo生产者和消费者经典案例
一.导入依赖 <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</art ...
- Default Trace 查找日志文件快速增长的原因
select loginname, loginsid, spid, hostname, applicationname, servername, databasename, objectname, e ...
- 【转载】Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration info
网上查到http://social.msdn.microsoft.com/Forums/vstudio/en-US/58271e39-beca-49ac-90f9-e116fa3dd3c0/mxed- ...
- 【CSS】知识笔记
一.CSS文件 1.引用css文件,放在Head里面,可以减少repaint和reflow. 浏览器渲染页面大概是这样的,当浏览器从上到下一边下载html生成DOM tree一边根据浏览器默认及现有C ...
- mysqli扩展有一系列的优势,相对于mysql扩展的提升主要优势有哪些?
mysqli扩展有一系列的优势,相对于mysql扩展的提升主要优势有哪些? 面向对象接口 prepared语句支持(译注:关于prepare请参阅mysql相关文档) 多语句执行支持 事务支持 增强的 ...
- imagick的简单使用
原文:https://blog.csdn.net/wulove52/article/details/78376142 PHP建图通常都用GD库,因为是内置的不需要在服务器上额外安装插件,所以用起来比较 ...
- C void的指针 强制类型转换(int*)p 把变量指针p强制转换成指向int类型的指针
#include <stdio.h> int main(void){ void *p; int a = 14322; char c ='A'; p = &a; //p = & ...
- c语言实现杨辉三角形
#include <stdio.h> int main(void) { int a[9][9]={}; int i,j; for(i=0;i<9;i++){ for(j=0;j< ...