转载自: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的更多相关文章

  1. Java Collections Source Code Series 2 ---接口

    废话开篇 自己学完Java Collections框架之后,其中的一个较大的收获就是接口对于层次的重要性.Java Collections的最终实现至少有几十个,其中很多都有非常相似的功能(metho ...

  2. Java Collections Source Code Series 1 --- 简介

    废话开篇 由于项目需要,需要对Java Collections进行系统地了解,所以在此记录下,方便自己,服务他人. Java Collections 简介 Java Collections 框架主要包 ...

  3. [转]Native Java Bytecode Debugging without Source Code

    link from:http://www.crowdstrike.com/blog/native-java-bytecode-debugging-without-source-code/index.h ...

  4. Java Sound : generate play sine wave - source code

    转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-sound-generate-play-sine-wave.html Work ...

  5. How to Convert a Class File to a Java File?

    What is a programming language? Before introducing compilation and decompilation, let's briefly intr ...

  6. 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是一种解释性的语言,但是这种说法 ...

  7. 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 ...

  8. Combining an audio file with video file in python

    Combining an audio file with video file in python - Stack Overflow https://stackoverflow.com/questio ...

  9. [idea]Error:java: invalid source release: 1.8 标签: idea 2017-02-24 15:50 961人阅读

    最近用idea敲struts,虽然idea的界面很好看,代码提示也很强大,不过也的确是碰到了一些在eclipse上从来没有碰到过的问题,而且我发现,idea的错误,很多都是在外国的网站上提问的人比较多 ...

随机推荐

  1. jumpserver 安装

    # CentOS 7 安装jumpserver $ setenforce 0 # 可以设置配置文件永久关闭$ systemctl stop iptables.service$ systemctl st ...

  2. YAML_12 批量创建用户,分别设置用户组

    with_items标准循环 ansible]# vim add.yml --- - hosts: web2   remote_user: root   tasks:     - user:     ...

  3. BZOJ 5093: [Lydsy1711月赛]图的价值 第二类斯特林数+NTT

    定义有向图的价值为图中每一个点的度数的 \(k\) 次方之和. 求:对于 \(n\) 个点的无向图所有可能情况的图的价值之和. 遇到这种题,八成是每个点单独算贡献,然后累加起来. 我们可以枚举一个点的 ...

  4. win32线程栈溢出问题 (二)

    3.2.函数递归调用引发的栈溢出 写一段最简单的无穷递归代码,如下: #include "stdafx.h" void f(void) { f(); } int _tmain(in ...

  5. 数据库(以MySQL为例)

    一.数据库简介 数据库就是数据的仓库,用来按照特定的结构去组织和管理数据,有了数据库可以更加方便.便捷的操作需要保存的数据 不管是什么数据库,最终都是将数据保存到硬盘中,只是存储的格式不同于文本文件 ...

  6. C静态库和动态库的制作

    (一)静态库就是把一些*.o的文件集合起来:以*.a结尾打包:做成的lib库文件:专门放到lib目录下 静态库的制作: 1.制作*.a文件 把之前src/mymath.c 的源文件 编译成单独的o文件 ...

  7. leetcode解题报告(24):Pascal's TriangleII

    描述 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [ ...

  8. 洛谷 P2661 信息传递 题解

    P2661 信息传递 题目描述 有 \(n\) 个同学(编号为 \(1\) 到 \(n\) )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 \(i\) 的同学的信息传 ...

  9. Ring3挂起进程,跟恢复进程.

    目录 Ring3挂起进程,跟恢复进程. 一丶简介 二丶代码 Ring3挂起进程,跟恢复进程. 一丶简介 有时候我们做对抗的时候可能会遇到.一个进程常常操作我们.但是我们 可以通过挂起进程来挂起它让它无 ...

  10. ACL终极详解