私のアプリケーションでは、まず特定のファイルからオーディオ データを読み取り、それをバッファに格納する必要があります。その後、加工などに使用します。問題は、その内容を印刷しようとすると、0が返されることです:
String data = String.valueOf(buffer[50]+" "+buffer[100]+" "+buffer[200]+buffer[599]);
display.setText(data);
上記のステートメントを印刷すると、すべてゼロになります!...なぜ???
これが私のコードです:
public void readAudioDataFromFile() {
String filePath2 = "/storage/sdcard0/Sounds/originalFile.pcm";
FileInputStream ins = null;
File file = new File(filePath2);
int size = (int) file.length();
byte [] buffer=new byte [size];
try {
ins= new FileInputStream(filePath2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
ins.read(buffer, 0,size);
int count =0;
for(int i=0; i<buffer.length; i++)
{
if(buffer[i]!=0)
count++;
}
String data = String.valueOf((count);
display.setText(data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
奇妙なことは、次のステートメントを試したときです。
int x= ins.read(buffer, 0,size);
xを印刷すると、ファイルサイズに等しい数値が出ましたが、
また、コードでわかるように「カウント」を出力しようとすると、出力も得られました!
つまり、バッファが空ではないということではありませんか??? なぜそれが空でない場合、その要素を印刷しようとするとゼロになるのはなぜですか???