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的错误,很多都是在外国的网站上提问的人比较多 ...
随机推荐
- work,工作模式
work,工作模式 一个消息只能被一个消费者获取 工作模式就是simple模式多了几个消费者,其他一样 来自为知笔记(Wiz)
- Set的常用实现类HashSet和TreeSet
Set HashSet public static void main(String[] args) { //不可以重复 并且是无序的 //自然排序 从A-Z //eqauls从Object继 ...
- boost.tribool
tribool boost.tribool类似c++内建的bool类,但基于三态的布尔逻辑,在true和false之外还有一个indeterminate状态.一个例子场景是执行某项任务,在执行之前状态 ...
- npm start的时候改变端口及组合脚本
windows npm修改端口启动 set PORT=3000&&roadhog server npm start Linux npm 修改端口启动 set PORT=3000 roa ...
- C Primer Plus--C存储类、链接和内存管理之存储类(storage class)
目录 存储类 作用域 链接 存储时期 自动变量 寄存器变量 具有代码块作用域的静态变量 具有外部链接的静态变量 extern关键字 具有内部链接的静态变量 多文件 存储类 C为变量提供了5种不同的存储 ...
- P3719 [AHOI2017初中组]rexp——递归模拟
P3719 [AHOI2017初中组]rexp 没有什么算法的题做起来真不适应,这道题深深讽刺了我想用栈维护匹配括号个数的想法: 递归解决就行了: 时刻注意函数返回值是什么,边界条件是什么: #inc ...
- python 中 list 去重复
方法1 list=[,,,] set=set(list) list2=list(set) 方法2 list=[,,,] list2=[] for i in list: if i not in list ...
- phpstorm 2019.1 修改选中内容背景色,以及匹配的内容背景色
#与选中内容匹配的内容背景色Editor -> Color Scheme -> General -> Code -> Identifier under caret #选中内容前 ...
- hive 属性随笔记录
set hive.mapred.mode=strict; //设置hive执行模式,默认为nonstrict(非严格模式),这里设置为严格模式 set hiveconf:hive.cli.print. ...
- DELPHI开发LINUX插件架构的程序
DELPHI开发LINUX插件架构的程序 DELPHI可以开发LINUX配置型插件架构的程序,并且这一套插件架构,同样适用于MSWINDOWS和MAC. 配置插件: 根据配置,动态加载插件: