android 9.0 后台录音,不限制1分钟
录音SoundRecorder默认情况下,进入后台和锁屏是会暂停录音的,有时候我们希望进入后台和锁屏后仍然可以录音,那就需要对录音SoundRecorder做出相应的修改。
根据现象,我们第一时间应该考虑的是从生命周期出发,因为进入后台后,录音就停止了,猜测应该是在onPause或者是onStop中做了什么处理,通过查看SoundRecorder.java 中onPause和onStop中的逻辑确实是做了处理,具体修改如下:
一、修改SoundRecorder.java,android/packages/apps/SoundRecorder/src/com/android/soundrecorder/SoundRecorder.java
1、onPause方法中,将停止录音的逻辑屏蔽掉,修改如下
@Override
protected void onPause() {
// Stop listening for phone state changes.
/*for(int i = 0; i < mPhoneCount; i++) {
// adapt case: disabled telephony feature or activate card failure
if (null != mPhoneStateListener[i]) {
mTelephonyManager.listen(mPhoneStateListener[i],
PhoneStateListener.LISTEN_NONE);
}
}
mRecorder.stop();
// if dialog is shown, dialog processing the logic.
if (!mRenameDialogShown) {
if (mRecorder.sampleLength() > 0) {
mRecorderStop = true;
saveSample(true);
} else {
mRecorder.delete();
}
}*/
super.onPause();
}
2、onStop方法中,屏蔽掉停止录音的逻辑,修改如下:
@Override
public void onStop() {
//mRecorder.stop();
super.onStop();
}
3、理论上在onPause和onStop方法中屏蔽掉相关逻辑后,已经达到了目的,但是相关的释放处理还是需要做的,在onDestory方法中,加上刚才屏蔽掉的逻辑代码,具体如下:
/*
* Called on destroy to unregister the SD card mount event receiver.
*/
@Override
public void onDestroy() {
/hpe add start
for(int i = 0; i < mPhoneCount; i++) {
// adapt case: disabled telephony feature or activate card failure
if (null != mPhoneStateListener[i]) {
mTelephonyManager.listen(mPhoneStateListener[i],
PhoneStateListener.LISTEN_NONE);
}
}
mRecorder.stop();
// if dialog is shown, dialog processing the logic.
if (!mRenameDialogShown) {
if (mRecorder.sampleLength() > 0) {
mRecorderStop = true;
saveSample(true);
} else {
mRecorder.delete();
}
}
//hpe add end
if (mSDCardMountEventReceiver != null) {
unregisterReceiver(mSDCardMountEventReceiver);
mSDCardMountEventReceiver = null;
}
if (mPowerOffReceiver != null) {
unregisterReceiver(mPowerOffReceiver);
mPowerOffReceiver = null;
}
unregisterReceiver(mMountReceiver);
if (null != mProgressDialog && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
if (mMsgHandler.hasMessages(MSG_DISMISS_PROGRESS_DIALOG)) {
mMsgHandler.removeMessages(MSG_DISMISS_PROGRESS_DIALOG);
}
super.onDestroy();
}
3、因为关屏录音也不能暂停,所以在power按键的广播监听中需要做如下修改,只有关闭关机操作才停止录音,如果只是按下电源键就不做处理:
private void registerPowerOffListener() {
if (mPowerOffReceiver == null) {
mPowerOffReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//hpe modify start
if (action.equals(Intent.ACTION_SHUTDOWN)) {
if (mRecorder != null) {
mRecorder.stop();
}
mRecorder.delete();
}
//hpe modify end
}
};
IntentFilter iFilter = new IntentFilter();
iFilter.addAction(Intent.ACTION_SHUTDOWN);
iFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(mPowerOffReceiver, iFilter);
}
}
4.一分钟之后,后台录音,由于谷歌最新限制,所有录音停止。增加以下修改。
frameworks\av\services\audiopolicy\service\AudioPolicyService.cpp
void AudioPolicyService::UidPolicy::notifyService(uid_t uid, bool active) {
sp<AudioPolicyService> service = mService.promote();
if (service != nullptr) {
active = true;//add this
service->setRecordSilenced(uid, !active);
}
}
本文地址:https://blog.csdn.net/wed110/article/details/109646753