0
我创建了一个对象,它连续从麦克风读取输入,但我遇到了一些麻烦。我需要能够暂停并启动它,所以我已经实现了这些方法,但问题在于,在旧手机(Galaxy Y)上它第一次启动时效果很好,但在暂停后由于OutOfMemoryException,它再次启动它会迅速崩溃。AudioRecord内存不足异常
@Override
public void run() {
while (mIsRunning) {
synchronized (mThread) {
while (mIsPaused) {
try {
mThread.wait();
} catch (InterruptedException e) {
}
}
}
double sum = 0;
int readSize = mRecorder.read(mBuffer, 0, mBuffer.length);
if (readSize > 0) {
short[] samples = new short[readSize];
for (int i = 0; i < readSize; i++) {
sum += mBuffer[i] * mBuffer[i];
samples[i] = mBuffer[i];
}
final int amplitude = (int) Math.sqrt((sum/readSize));
Message message = new Message();
message.arg1 = amplitude;
message.arg2 = readSize;
message.obj = samples;
message.what = 0;
if (mHandler != null)
mHandler.sendMessage(message);
}
}
}
public void start() {
if (!mIsRunning) {
mIsRunning = true;
mThread.start();
} else synchronized (mThread) {
mIsPaused = false;
mThread.notifyAll();
}
mRecorder.startRecording();
}
public void pause() {
synchronized (mThread) {
mIsPaused = true;
}
mRecorder.stop();
}
所以,在第一时间一切运作良好,但之后,我呼吁AudioRecord停止(),并开始读取输入我又很快耗尽内存。有想法该怎么解决这个吗?我正在考虑不停止录制,只是不通过处理程序发送数据,但我不知道。谢谢。