安卓:读wav文件并显示它的价值 [英] Android: Reading wav file and displaying its values

查看:140
本文介绍了安卓:读wav文件并显示它的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个WAV文件读取数据,这样我可以绘制它作为一个波形。以下是我已经尝试了code:

Hi i'm trying to read the data in a wav file so that i can plot it as a waveform. Following is the code that i have tried:

    try {
        RandomAccessFile file = new RandomAccessFile(myFile,"r");
        while(file.read()>-1){
         byte b1 = (byte) file.read();
         byte b2 = (byte) file.read();
         Log.d(TAG,"DATA:"+ (double) (b2 << 8 | b1 & 0xFF) / 32767.0);
        }
        file.close();

    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

我的输出是-1和+1之间最初的范围值,但后来它仅给出了数据:0这似乎是一个无限循环,并有一个强制关闭。据我到达文件的末尾时循环应该终止。任何人都可以请提出了一些循环终止条件。请帮助....谢谢你是进步...

My output is a value that ranges between -1 and +1 initially but later it gives only DATA:0 it seems like an infinite loop and there is a force close. According to me the loop should terminate when the end of file is reached. Can anyone pls suggest some terminating condition for the loop. Please Help.... Thanks is advance...

推荐答案

这code段有以下的问题:

This code snippet has the following issues:

1)使用 file.read()-1个在while循环中你还是读取的字节,所以这code读取在每个迭代上3个字节。

1) Using file.read()>-1 in the while loop you still read bytes, so this code reads 3 bytes on every iteration.

2)WAV文件可以是(并且经常是)RIFF容器与WAV块。此外,它不能保证,价值观是连接在整数codeD(我见过WAV浮动编码)。字节顺序似乎是确定的,但集装箱的元信息也将被打印出来。

2) WAV files may be (and very often are) RIFF containers with WAV blocks. Also, it is not guaranteed that values are encoded in integers (I've seen float encoding in WAV). Byte order seems to be ok, but containers' metainformation will be printed also.

我不是在Java中的专家,但运行你的前pression用字节(-1,-1)给出了非常接近0的值。

I'm not an expert in Java, but running your expression with bytes (-1, -1) gives a value very close to 0.

要检查文件结束,检查是否有字节是-1:,而((B1 = -1)及及(B2 = -1)!)

To check for file termination, check if any of bytes are -1: while ((b1 != -1) && (b2 != -1))

另外,从 INT 转换字节好像是的错误:字节签名,这样你会得到价值从-128到127,这是不是你想要的(0..255)。

Also, the conversion from int to byte seems to be wrong: byte is signed, so you'll get values from -128 to 127, which is not what you want (0..255).

这篇关于安卓:读wav文件并显示它的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆