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文件的更多相关文章

  1. C#播放wav文件

    C#使用HWQPlayer类播放wav文件 类的代码: using System.IO; using System.Runtime.InteropServices; namespace HoverTr ...

  2. python 播放 wav 文件

    未使用其他库, 只是使用 pywin32 调用系统底层 API 播放 wav 文件. # Our raison d'etre - playing sounds import pywintypes im ...

  3. WinAPI: sndPlaySound - 播放 wav 文件

    WinAPI: sndPlaySound - 播放 wav 文件 //声明: sndPlaySound(   lpszSoundName: PChar; {声音文件}   uFlags: UINT{播 ...

  4. 如何播放 WAV 文件?

    from http://www.vckbase.com/index.php/wv/434 平时,你在多媒体软件的设计中是怎样处理声音文件的呢?使用Windows 提供的API函数 sndPlaySou ...

  5. 8086汇编语言 调用声卡播放wav文件(sound blaster)

    开更 大概最后做了一个能播放无损音乐(无压缩.不需解码)的播放器 原理是基于dosbox的模拟声卡,通过硬件之间的相互通讯做到的 关于详细内容接下来再讲. 一.从dosbox入手 我们知道cpu可以直 ...

  6. Linux音频编程--使用ALSA库播放wav文件

    在UBUNTU系统上使用alsa库完成了对外播放的wav文件的案例. 案例代码: /** *test.c * *注意:这个例子在Ubuntu 12.04.1环境下编译运行成功. * */ #inclu ...

  7. c++(qt)播放wav文件的四种方式

    //方法一(要符合RIFF规范) 1 QSound::play("E:/Projects/报警声1-1.wav"); //方法二(要符合RIFF规范) 1 QSoundEffect ...

  8. windows下使用waveout函数族播放wav文件

    要使用waveout函数组,族,首先要知道几个数据结构,首先是这个 typedef struct tWAVEFORMATEX { WORD wFormatTag; /* 格式的类型 */ WORD n ...

  9. Java 合并.wav 文件

    package org.lib.speech.test;import java.io.*; import java.util.*;import javax.sound.sampled.AudioFil ...

随机推荐

  1. Unity3D 导入Xcode 工程后。编译很慢

    Unity3D 导入Xcode 工程后.编译很慢 选择Targets-->Build options -->debug information format  然后选择DWARF 这样再次 ...

  2. 微软BI 之SSAS 系列 - 关于父子维度的设计

    除了之前的几篇文章中出现的时间维度,雪花型维度的设计之外还有一种比较特殊的维度 - 父子维度.父子维度特殊就特殊在它包含了一种基于递归关系(Recursive Relationship)的引用结构,  ...

  3. Want to write a book? Use word count to stay on track

    http://paloalto.patch.com/groups/maria-murnanes-blog/p/bp--want-to-write-a-book-use-word-count-to-st ...

  4. Unbuntu和Centos中部署同时多版本PHP的详细过程

        镜像制作:Unbuntu14 部署LAMP过程 1.Azure经典版中创建源Ubuntu14,并使用Xshell连接,并切换到root帐户下. 2.安装php5.4,新建/var/local/ ...

  5. spring boot 运行提示:Process finished with exit code 1

    spring boot 运行提示:Process finished with exit code 1 经检查发现是由于在application.properties配置文件中将某些自定义配置项移除了, ...

  6. 请指出document load和document ready的区别?

    共同点:这两种事件都代表的是页面文档加载时触发. 异同点: ready 事件的触发,表示文档结构已经加载完成(不包含图片等非文字媒体文件). onload 事件的触发,表示页面包含图片等文件在内的所有 ...

  7. stopPropagation 和stopImmediatePropagation区别

    1.示例代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UT ...

  8. Android——代码中使用颜色值

    android中设置颜色时,可以直接使用颜色值来设置: view.setBackgroundColor(Color.parseColor("#颜色值"));

  9. Android开发经典笔试面试题汇总(持续更新中)

    1.我们都知道Handler是线程与Activity通信的桥梁,假设线程处理不当.你的机器就会变得非常慢,那么线程销毁的方法是:(A) A. onDestroy() B. onClear() C. o ...

  10. V-rep学习笔记:main script and child scripts

    The main and child scripts The main script and the child scripts, which are simulation scripts, play ...