java播放wav文件
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.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException; public class PlayWav { public PlayWav(String file) {
try {
// read wav file to audio stream
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(file));
// read audio format from stream
AudioFormat audioFormat = audioInputStream.getFormat();
System.out.println("采样率:" + audioFormat.getSampleRate());
System.out.println("总帧数:" + audioInputStream.getFrameLength());
System.out.println("时长(秒):" + audioInputStream.getFrameLength() / audioFormat.getSampleRate());
// SourceDataLine info
Info dataLineInfo = new Info(SourceDataLine.class, audioFormat); SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
byte[] b = new byte[1024];
int len = 0;
sourceDataLine.open(audioFormat, 1024);
sourceDataLine.start();
while ((len = audioInputStream.read(b)) > 0) {
sourceDataLine.write(b, 0, len);
} audioInputStream.close();
sourceDataLine.drain();
sourceDataLine.close(); } catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} } public static void main(String[] args) {
new PlayWav("D:\\123.wav"); }
}
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.Info;import javax.sound.sampled.LineUnavailableException;import javax.sound.sampled.SourceDataLine;import javax.sound.sampled.UnsupportedAudioFileException;
public class PlayWav {public PlayWav(String file) {try {// read wav file to audio streamAudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(file));// read audio format from streamAudioFormat audioFormat = audioInputStream.getFormat();System.out.println("采样率:" + audioFormat.getSampleRate());System.out.println("总帧数:" + audioInputStream.getFrameLength());System.out.println("时长(秒):" + audioInputStream.getFrameLength() / audioFormat.getSampleRate());// SourceDataLine infoInfo dataLineInfo = new Info(SourceDataLine.class, audioFormat);
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);byte[] b = new byte[1024];int len = 0;sourceDataLine.open(audioFormat, 1024);sourceDataLine.start();while ((len = audioInputStream.read(b)) > 0) {sourceDataLine.write(b, 0, len);}audioInputStream.close();sourceDataLine.drain();sourceDataLine.close();} catch (UnsupportedAudioFileException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (LineUnavailableException e) {e.printStackTrace();}
}
public static void main(String[] args) {new PlayWav("D:\\123.wav");
}}
java播放wav文件的更多相关文章
- C#播放wav文件
C#使用HWQPlayer类播放wav文件 类的代码: using System.IO; using System.Runtime.InteropServices; namespace HoverTr ...
- python 播放 wav 文件
未使用其他库, 只是使用 pywin32 调用系统底层 API 播放 wav 文件. # Our raison d'etre - playing sounds import pywintypes im ...
- WinAPI: sndPlaySound - 播放 wav 文件
WinAPI: sndPlaySound - 播放 wav 文件 //声明: sndPlaySound( lpszSoundName: PChar; {声音文件} uFlags: UINT{播 ...
- 如何播放 WAV 文件?
from http://www.vckbase.com/index.php/wv/434 平时,你在多媒体软件的设计中是怎样处理声音文件的呢?使用Windows 提供的API函数 sndPlaySou ...
- 8086汇编语言 调用声卡播放wav文件(sound blaster)
开更 大概最后做了一个能播放无损音乐(无压缩.不需解码)的播放器 原理是基于dosbox的模拟声卡,通过硬件之间的相互通讯做到的 关于详细内容接下来再讲. 一.从dosbox入手 我们知道cpu可以直 ...
- Linux音频编程--使用ALSA库播放wav文件
在UBUNTU系统上使用alsa库完成了对外播放的wav文件的案例. 案例代码: /** *test.c * *注意:这个例子在Ubuntu 12.04.1环境下编译运行成功. * */ #inclu ...
- c++(qt)播放wav文件的四种方式
//方法一(要符合RIFF规范) 1 QSound::play("E:/Projects/报警声1-1.wav"); //方法二(要符合RIFF规范) 1 QSoundEffect ...
- windows下使用waveout函数族播放wav文件
要使用waveout函数组,族,首先要知道几个数据结构,首先是这个 typedef struct tWAVEFORMATEX { WORD wFormatTag; /* 格式的类型 */ WORD n ...
- Java 合并.wav 文件
package org.lib.speech.test;import java.io.*; import java.util.*;import javax.sound.sampled.AudioFil ...
随机推荐
- 通示jQuery实例方法,未DOM对象添加多个方法
<script type="text/javascript"> /* * 通示jQuery实例方法,未DOM对象添加多个方法 * 用按钮做多个事件的调用 */ (fun ...
- ExtJs4.2中Tab选项卡的右击关闭其它和关闭当前功能不准确的解决方法
一.ExtJs4.2中Tab选项卡的右击关闭其它和关闭当前功能不准确的解决方法 二.找到ux目录下的TabCloseMenu.js文件,将内容替换成下面代码. 三.代码: /** * Plugin f ...
- SpringMVC,Ehcache
来自:http://blog.csdn.net/jadyer/article/details/12257865 http://jinnianshilongnian.iteye.com/blog/200 ...
- Emacs 使用graphviz-dot-mode创建架构图
安装 首先要安装graphviz-dot-mode模块,假设list-packages的网站国内无法訪问,改为手动下载graphviz-dot-model.el.放到~/.emacs.d/文件夹下. ...
- mysql代理之Atlas
Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy0.8.2版本号的基础上.改动了大量bu ...
- Flash:彻底理解crossdomain.xml、跨swf调用。
安全域.crossdomain.xml,到处都有各种各种零碎的基础解释,所以这里不再复述这些概念. 本文目的是整理一下各种跨域加载的情况.什么时候会加载crossdomain,什么时候不加载. 1 ...
- oracle12c安装报错:PRVF-0002的解决方法
出错信息: 安装oracle12c, ./runInstaller 启动图形化报错 PRVF-0002 : Could not retrieve local nodename. 原因: 找不到主机名的 ...
- oracle 根据字段查询重复数据
1.情景展示 由上图可知,APPUSERID字段和VIRTUAL_CARDID字段存在一对多的关系,如何将重复的APPUSERID字段的数据查询出来呢? 2.原因分析 先查出重复的APPUSERI ...
- spring 配置bean的方法及依赖注入发方式
Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).FactoryBean 这里依据全类名配置bean <bean id="helloWo ...
- 【php正则】php正则匹配UTF-8格式的中文汉字 和 【,】【,】【。】等符号
1.php正则匹配UTF-8格式的中文汉字 和 [,][,][.]等符号 if (preg_match_all("/([\x{4e00}-\x{9fa5}]+((,)?)+((,)?)+(( ...