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-23

import 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或者16bit
    public 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值