java声音文件操作.docx

上传人:小飞机 文档编号:3159536 上传时间:2023-03-11 格式:DOCX 页数:7 大小:37.52KB
返回 下载 相关 举报
java声音文件操作.docx_第1页
第1页 / 共7页
java声音文件操作.docx_第2页
第2页 / 共7页
java声音文件操作.docx_第3页
第3页 / 共7页
java声音文件操作.docx_第4页
第4页 / 共7页
java声音文件操作.docx_第5页
第5页 / 共7页
亲,该文档总共7页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《java声音文件操作.docx》由会员分享,可在线阅读,更多相关《java声音文件操作.docx(7页珍藏版)》请在三一办公上搜索。

1、java声音文件操作AudioInputStreamAudioFormat getFormat 获得此音频输入流中声音数据的音频格式。 int read(byte b) 从音频输入流读取一定数量的字节,并将其存储在缓冲区数组 b 中。 AudioFormat AudioFormat 是在声音流中指定特定数据安排的类AudioSystem 充当取样音频系统资源的入口点static AudioInputStream getAudioInputStream(File file) 从提供的 File 获得音频输入流。 static Line getLine(Line.Info info) 获得与指定

2、Line.Info 对象中的描述匹配的行。 SourceDataLine接口 源数据行是可以写入数据的数据行void open(AudioFormat format) 打开具有指定格式的行,这样可使行获得所有所需的系统资源并变得可操作。 int write(byte b, int off, int len) 通过此源数据行将音频数据写入混频器。 void drain 通过在清空数据行的内部缓冲区之前继续数据 I/O,排空数据行中的列队数据。 类 DataLine.InfoDataLine.Info(Class<?> lineClass, AudioFormat format) 根据指定

3、信息构造数据行的信息对象,这些信息包括单个音频格式。/原创代码package cwj.java.io;import java.io.File;import javax.sound.sampled.*;public class Music public static void main(Stringargs)File file = new File(sound/幸运符号.wav);AudioInputStream ais = null;try ais=AudioSystem.getAudioInputStream(file); catch (Exception e) / TODO: handle

4、 exceptione.printStackTrace;AudioFormat format = ais.getFormat;SourceDataLine sdl = null;try sdl = AudioSystem.getSourceDataLine(format);sdl.open(format); catch (LineUnavailableException e) / TODO Auto-generated catch blocke.printStackTrace;sdl.start;int n=0;byte bytes=new byte500;try while(n!=-1)n=

5、ais.read(bytes);if(n>=0)sdl.write(bytes, 0, n); catch (Exception e) / TODO: handle exceptione.printStackTrace;finallysdl.drain;sdl.close;/系统例子package cwj.java.io;import java.io.File;import java.io.IOException;import javax.sound.sampled.*;public class 声音操作public static void main(Stringargs)AePlayWa

6、ve apw = new AePlayWave(sound/tank.wav);apw.start;class AePlayWave extends Thread private String filename;public AePlayWave(String wavfile) filename = wavfile;public void run File soundFile = new File(filename);AudioInputStream audioInputStream = null;try audioInputStream = AudioSystem.getAudioInput

7、Stream(soundFile); catch (Exception e1) e1.printStackTrace;return;AudioFormat format = audioInputStream.getFormat;SourceDataLine auline = null;DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);try auline = (SourceDataLine) AudioSystem.getLine(info);auline.open(format); catch (Exce

8、ption e) e.printStackTrace;return;auline.start;int nBytesRead = 0;/这是缓冲byte abData = new byte512;try while (nBytesRead != -1) nBytesRead = audioInputStream.read(abData, 0, abData.length);if (nBytesRead >= 0)auline.write(abData, 0, nBytesRead); catch (IOException e) e.printStackTrace;return; finally auline.drain;auline.close;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号