Android AudioTrack/AudioRecord -wav文件读取3
生活随笔
收集整理的這篇文章主要介紹了
Android AudioTrack/AudioRecord -wav文件读取3
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
下面是一個網上一個大神寫的,在公司測過了,還不錯.
還可以寫一個構造函數:
initReader(InputStream is){ fis = new FileInputStream(is); bis = new BufferedInputStream(fis); }eg:call it as following : InputStream is=Activity.getResource().openRawResource(); InitReader(is);openRawResource(r.raw.wavname);上面漏了資源.
?
// filename: WaveFileReader.java // RobinTang // 2012-08-23import java.io.*;public class WaveFileReader {private String filename = null;private int[][] data = null;private int len = 0;private String chunkdescriptor = null;static private int lenchunkdescriptor = 4;private long chunksize = 0;static private int lenchunksize = 4;private String waveflag = null;static private int lenwaveflag = 4;private String fmtubchunk = null;static private int lenfmtubchunk = 4;private long subchunk1size = 0;static private int lensubchunk1size = 4;private int audioformat = 0;static private int lenaudioformat = 2;private int numchannels = 0;static private int lennumchannels = 2;private long samplerate = 0;static private int lensamplerate = 2;private long byterate = 0;static private int lenbyterate = 4;private int blockalign = 0;static private int lenblockling = 2;private int bitspersample = 0;static private int lenbitspersample = 2;private String datasubchunk = null;static private int lendatasubchunk = 4;private long subchunk2size = 0;static private int lensubchunk2size = 4;private FileInputStream fis = null;private BufferedInputStream bis = null;private boolean issuccess = false;public WaveFileReader(String filename) {this.initReader(filename);}// 判斷是否創建wav讀取器成功public boolean isSuccess() {return issuccess;}// 獲取每個采樣的編碼長度,8bit或者16bitpublic int getBitPerSample(){return this.bitspersample;}// 獲取采樣率public long getSampleRate(){return this.samplerate;}// 獲取聲道個數,1代表單聲道 2代表立體聲public int getNumChannels(){return this.numchannels;}// 獲取數據長度,也就是一共采樣多少個public int getDataLen(){return this.len;}// 獲取數據// 數據是一個二維數組,[n][m]代表第n個聲道的第m個采樣值public int[][] getData(){return this.data;}private void initReader(String filename){this.filename = filename;try {fis = new FileInputStream(this.filename);bis = new BufferedInputStream(fis);this.chunkdescriptor = readString(lenchunkdescriptor);if(!chunkdescriptor.endsWith("RIFF"))throw new IllegalArgumentException("RIFF miss, " + filename + " is not a wave file.");this.chunksize = readLong();this.waveflag = readString(lenwaveflag);if(!waveflag.endsWith("WAVE"))throw new IllegalArgumentException("WAVE miss, " + filename + " is not a wave file.");this.fmtubchunk = readString(lenfmtubchunk);if(!fmtubchunk.endsWith("fmt "))throw new IllegalArgumentException("fmt miss, " + filename + " is not a wave file.");this.subchunk1size = readLong();this.audioformat = readInt();this.numchannels = readInt();this.samplerate = readLong();this.byterate = readLong();this.blockalign = readInt();this.bitspersample = readInt();this.datasubchunk = readString(lendatasubchunk);if(!datasubchunk.endsWith("data"))throw new IllegalArgumentException("data miss, " + filename + " is not a wave file.");this.subchunk2size = readLong();this.len = (int)(this.subchunk2size/(this.bitspersample/8)/this.numchannels);this.data = new int[this.numchannels][this.len];for(int i=0; i<this.len; ++i){for(int n=0; n<this.numchannels; ++n){if(this.bitspersample == 8){this.data[n][i] = bis.read();}else if(this.bitspersample == 16){this.data[n][i] = this.readInt();}}}issuccess = true;} catch (Exception e) {e.printStackTrace();}finally{try{if(bis != null)bis.close();if(fis != null)fis.close();}catch(Exception e1){e1.printStackTrace();}}}private String readString(int len){byte[] buf = new byte[len];try {if(bis.read(buf)!=len)throw new IOException("no more data!!!");} catch (IOException e) {e.printStackTrace();}return new String(buf);}private int readInt(){byte[] buf = new byte[2];int res = 0;try {if(bis.read(buf)!=2)throw new IOException("no more data!!!");res = (buf[0]&0x000000FF) | (((int)buf[1])<<8);} catch (IOException e) {e.printStackTrace();}return res;}private long readLong(){long res = 0;try {long[] l = new long[4];for(int i=0; i<4; ++i){l[i] = bis.read();if(l[i]==-1){throw new IOException("no more data!!!");}}res = l[0] | (l[1]<<8) | (l[2]<<16) | (l[3]<<24);} catch (IOException e) {e.printStackTrace();}return res;}private byte[] readBytes(int len){byte[] buf = new byte[len];try {if(bis.read(buf)!=len)throw new IOException("no more data!!!");} catch (IOException e) {e.printStackTrace();}return buf;} }?
?
轉載于:https://www.cnblogs.com/MMLoveMeMM/articles/3612724.html
總結
以上是生活随笔為你收集整理的Android AudioTrack/AudioRecord -wav文件读取3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DrJava试用笔记
- 下一篇: 微信利用PHP创建自定义菜单的方法