为什么即使我使用WakeLock,当设备进入睡眠状态时,AudioRecord也无法录制声音? [英] Why AudioRecord is not recording sound when device fall asleep even when I use WakeLock?

查看:485
本文介绍了为什么即使我使用WakeLock,当设备进入睡眠状态时,AudioRecord也无法录制声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AudioRecord 录制设备中的声音,并将其写入 .wav 文件,即使设备移动,我也需要它来继续录制和写入文件睡觉.因此,我将wakeLock与 PARTIAL_WAKE_LOCK 参数一起使用.但是在设备进入睡眠状态后, AudioRecord 会继续录音,但是没有声音.当我打开 .wav 文件时,设备进入睡眠状态后便会保持静音状态.

  audioRecord.startRecording();isReading = true;totalSizeInBytes = 0;新线程(新Runnable(){@Override公共无效run(){short [] buffer =新的short [bufferSize];//短裤的缓冲区,从audioRecord获取int readCount;int状态int校验= 0;//wakeLock初始化和获取PowerManager pm =(PowerManager)getSystemService(Context.POWER_SERVICE);akeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyApp:RecordWakeLock");akeLock.acquire();如果(isWavFileCreated){while(isReading){Log.d(TAG,audioRecord.getRecordingState()+");readCount = audioRecord.read(buffer,0,bufferSize);writeWavFile(buffer);}writeWavFileHeader();尝试 {dos.close();} catch(IOException e){e.printStackTrace();}}}}).开始(); 

audioRecord.getRecordState()始终返回true.我不知道是什么问题.

解决方案

@Raskilas的答案

但是,它没有包含很多实际代码,因此我整理了一个Github Gist来演示该方法: https://gist.github.com/Venryx/e1f772b4c05b2da08e118ccd5cc162ff

这不是一个完全完成的项目(并且没有多余的东西),但是它包含了所有重要的内容,我可以确认它可以在我的设备(Mi Mix 3,Android 9)上正常工作,即使屏幕关闭.(我按下电源键并等待30分钟以上,但仍在记录麦克风输入.我之前的尝试都停止了约2分钟.)

有趣的是,我什至不需要激活唤醒锁或wifi锁.但是,如果您发现设备需要它,并且/或者为了防止其他功能被挂起,则Gist中也包含了这样做的功能.

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.

解决方案

The answer by @Raskilas works!

However, it doesn't include much of the actual code needed, so I've put together a Github Gist which demonstrates the approach: https://gist.github.com/Venryx/e1f772b4c05b2da08e118ccd5cc162ff

It's not a completely finished project (and has extra stuff that isn't needed), but it includes all the important bits, and I can confirm it's working on my device (Mi Mix 3, Android 9), even when its screen is turned off. (I press power and wait 30+ minutes, and it's still logging the microphone input. My previous attempts all stopped ~2 minutes in.)

Interestingly, I didn't even need to activate a wake-lock or wifi-lock; however, functions to do so are included in the Gist as well, in case you find it's needed for your device, and/or to prevent other functionality from being suspended.

这篇关于为什么即使我使用WakeLock,当设备进入睡眠状态时,AudioRecord也无法录制声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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