博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java播放wav文件
阅读量:6510 次
发布时间:2019-06-24

本文共 3369 字,大约阅读时间需要 11 分钟。

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");
}}

 

转载地址:http://djbfo.baihongyu.com/

你可能感兴趣的文章
Codeforces 591 B Rebranding【Codeforces Round #327 (Div. 2)】
查看>>
命名难,难于上青天
查看>>
APUE读书笔记-05标准输入输出库(7)
查看>>
23 第一周作业
查看>>
DNS解析偶尔延迟
查看>>
iOS打电话,发短信,发邮件,打开网址
查看>>
06-验证码-基本功能实现
查看>>
Java数据结构与算法(六) 希尔排序
查看>>
canvas学习笔记
查看>>
elasticsearch安装步骤
查看>>
PHP获取Cookie模拟登录CURL(转)
查看>>
PHP-权限控制类(转)
查看>>
CSS3秘笈第三版涵盖HTML5学习笔记9~12章
查看>>
bzoj1044木棍分割
查看>>
leetcode-136-Single Number
查看>>
微信小程序笔记<五> 页面管理及生命周期(route)——getCurrentPages()
查看>>
http服务器小项目
查看>>
一些数学上的名词及操作
查看>>
<%@ include %>指令和<jsp:include>区别
查看>>
因为文件组 'PRIMARY' 已满 解决办法
查看>>