我正在使用 BackgroundTimer
来自 react-native-background-timer
在 Android 的 React native 项目中。
我希望用户能够在设定的持续时间后录制音频(例如,在 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
我之前也尝试过在 Expo 中构建它,但没有成功。
有没有办法保证这个操作会奏效?
最佳答案
您将有大约 40 秒的时间来调用您的函数并在后台状态或手机处于 sleep 状态时调用完成处理程序。
如果你没有按时完成它,iOS 会杀死你的应用程序。如果你不遵守规则,iOS 会给你更少的醒来机会。有关背景模式的更详细用法,您可以查看以下教程
这是由于应用程序开发人员滥用此机制,耗尽了客户的设备电池。
注:看门狗超时约为 40 秒。
关于javascript - react-native-background-timer 在 Android 上不一致地用于录制音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62231218/