我正在尝试向我的Android应用添加波形图,它显示当前正在播放的音频文件的波形数据。 我目前正在尝试编写一种方法来构建具有波形文件幅度的arraylist(每100毫秒音频长度1个振幅),但是完成运行需要很长时间(分钟)。这是非常低效的。
这是代码:
public ArrayList <Integer> buildAudioWaveData(Recording recording){
final Recording finalRecording = recording;
(new Thread(){
@Override
public void run(){
File recFile = finalRecording.getFile();
ArrayList <Integer> dataSeries = new ArrayList<Integer>();
try {
InputStream bis = new BufferedInputStream(new FileInputStream(recFile));
DataInputStream dis = new DataInputStream(bis);
long sampleRate = finalRecording.getSampleRate(new RandomAccessFile(recFile, "rw"));
long samplesPerDatum = sampleRate / 10; // One sample for every 100 ms.
long fileLengthInBytes = recFile.length();
long fileDataRemaining = fileLengthInBytes / 2; // 16 bit wave file = 2 bytes per sample.
int max = 0;
while(fileDataRemaining > 0){
if(fileDataRemaining > samplesPerDatum) {
for (int i = 0; i < samplesPerDatum; i++) {
short temp = dis.readShort();
if (temp > max) {
max = temp;
}
}
Log.i("temp", Integer.toString(max));
dataSeries.add(max);
max = 0;
}
fileDataRemaining -= samplesPerDatum;
}
int x = 0;
}catch(Exception e){
}
}
}).start();
return null;
}
有没有人知道我可以为我的图形生成数组的更有效方法?
先谢谢你们。 科里B:)