react-native-background-timer 在 Android 上用于录制音频时不一致



我在 Android 的 React 原生项目中使用react-native-background-timer中的BackgroundTimer

我希望用户能够在设定的持续时间后录制音频(例如,在 30 分钟后录制 5 分钟(。

我遇到的问题是它"有时"有效。似乎操作系统经常杀死进程 - 特别是如果执行时间更长。

代码提取:

import BackgroundTimer from 'react-native-background-timer';
...
// Start a timer that runs once after X milliseconds
const timeout1 = BackgroundTimer.setTimeout(() => {
// Should work when when app is the the background
this.onStartRecord();
const timeout2 = BackgroundTimer.setTimeout(() => {
this.onStopRecord();
BackgroundTimer.clearTimeout(timeout2);
}, millisRecording);
// Clear first timeout
BackgroundTimer.clearTimeout(timeout1);
}, millisUntilRecordMoment);

代码的其余部分在这里: https://github.com/bentaly/listen/blob/master/App.tsx

我以前也尝试过在世博会上构建这个,但没有奏效。

有没有办法保证此操作会起作用?

您将有大约 ~40 秒的时间来调用函数并在后台状态或手机处于睡眠状态中调用完成处理程序。

如果您没有按时完成,iOS 将杀死您的应用程序。如果你不遵守规则,iOS会给你更少的醒来机会。有关后台模式的更详细用法,您可以查看以下教程

这是由于应用程序开发人员滥用此机制,杀死了客户的设备电池。

注意:看门狗超时为 ~40 秒。

最新更新