Sound (audio file) player in java - working source code example

时间:2022-08-27 12:49:54

转载自: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://*.com/questio ...

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

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

随机推荐

  1. Centos7 wifi

    centos7如果在安装系统选择安装软件的选项是gnome套件(要注意退出选择界面回到安装界面时软件选项显示的是gnome,仅仅选择了gnome的软件也不行),安装完成后就会有wifi的图标,下面的方 ...

  2. CentOS 安装 Wine

    1. 下载安装包 Wine的中文官网可以下载到最新稳定和开发版本的Wine安装包,根据不同需求可以自行下载 2. 解压安装包,编译前检查 根据不同的平台选择不同的编译选项: For 32-Bit Sy ...

  3. 封装ios静态库碰到的一些问题(二)

    在静态库建立好了之后呢,于是应用程序就引用它,加上拷贝的h文件,但是引用之后Build之后提示很多sybmbol 重复 于是进行检查,确实由于是从其他工程修改过来的,很多基础库都引用了,删除之,最后就 ...

  4. iOS-点击推送消息跳转处理

    当用户通过点击通知消息进入应用时 - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDi ...

  5. 在ios下提示“@synthesize of ‘weak’ property is only allowed in ARC or GC mode”

    现在的项目是手动内存管理,所以在引入第三方资源库时候,很多资源库更新以后都开始使用arc进行编码,这样就导致两种代码风格不一致,有的时候可能开发者也没有注意到这些问题,反正用的时候也没有报错,就直接使 ...

  6. 繁华模拟赛 ljw搭积木

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  7. fis3简单教程

    #进入工作目录初始化fis3目录(此步非必须,如果当前目录已有fis-conf.js文件,可以不用初始化)fis3 init#部署(<path>为部署目录,如果想部署到当前output目录 ...

  8. 创建自定义的Middleware中间件

    创建自定义的Middleware中间件 阅读目录 何为Middleware中间件 使用Inline方式注册Middleware 使用Inline+ AppFunc方式注册Middleware 定义原生 ...

  9. 【代码笔记】iOS-UIActionSheet字体的修改

    一,效果图. 二,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIVi ...

  10. ajax 跨域访问 :Access-Control-Allow-Origin

    一说到ajax跨域.首先想到的就是jsonp  . JSONP方法是一种非官方方法,而且这种方法只支持GET方式,不如POST方式安全. 即使使用jQuery的jsonp方法,type设为POST,也 ...