I'm recording the sound from device with AudioRecord and write it to .wav file and I need it to keep recording and writing in file even when the device goes to sleep. So I used wakeLock with PARTIAL_WAKE_LOCK argument. But after the device went asleep AudioRecord keep recording but there is no sound. When I opened a .wav file there is a silent from the moment when device went asleep.
audioRecord.startRecording();
isReading = true;
totalSizeInBytes = 0;
new Thread(new Runnable() {
@Override
public void run() {
short[] buffer = new short[bufferSize]; //buffer of shorts, getting from audioRecord
int readCount;
int state;
int check = 0;
//wakeLock initialization and acquiring
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp:RecordWakeLock");
wakeLock.acquire();
if (isWavFileCreated) {
while (isReading) {
Log.d(TAG, audioRecord.getRecordingState() + "");
readCount = audioRecord.read(buffer, 0, bufferSize);
writeWavFile(buffer);
}
writeWavFileHeader();
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
audioRecord.getRecordState() always return true. I don't know what'fs the problem.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…