Commit 7709d211 by liimin

feat(recorder): 添加录音超时自动停止功能

- 新增 TimeoutService uts 文件实现超时监控逻辑
- 在 RecordService 中集成超时服务实例
- 实现单例模式的超时服务管理
- 添加触摸事件监听和防抖机制
- 设置默认 40 分钟无操作自动停止录音
- 优化日志输出格式和错误处理机制
1 parent 647dca8d
......@@ -3,14 +3,13 @@ import { commit } from "@/store/index.uts";
// import { checkPermission, requestPermissions } from "@/uni_modules/permission-handler-plus";
import { handleError } from "../../utils/error.uts";
// import TimeoutService from './TimeoutService.uts'
import { ensurePermission } from "../../utils/permission.uts"; // 不再使用,已改为 uni-app 原生 API
import { logger } from "../logger/index.uts";
// import { sleep } from './helper.uts' // 文件不存在,先注释掉
import AudioManager from "./AudioManager.uts";
import DBRecord from "./DBRecord.uts";
import { RecorderStrategyFactory } from "./strategies/RecorderStrategyFactory.uts";
import TempRecord from "./TempRecord.uts";
import TimeoutService from "./TimeoutService.uts";
import { BusiType, RecordData, RecordFile } from "./types";
import { IRecorderStrategy } from "./types/strategy.uts";
......@@ -21,6 +20,8 @@ export class RecordService {
businessHandler: IRecorderStrategy = RecorderStrategyFactory.getDefaultStrategy();
record: null | TempRecord = null;
tempRecordDto: null | RecordData = null;
timeoutService: null | TimeoutService = null;
constructor() {
this.init();
}
......@@ -59,7 +60,7 @@ export class RecordService {
}
init() {
try {
logger.info("【录音服务RecordService】 == 初始化", "RecordService");
logger.info("【录音服务 RecordService】 == 初始化", "RecordService");
this.audioManager = new AudioManager(
(res:any) => { this.onAudioStart(res); },
(res) => { this.onAudioStop(res); },
......@@ -74,10 +75,13 @@ export class RecordService {
// return this.businessHandler.getDynamicAttributes();
// }
);
this.timeoutService = TimeoutService.getInstance(async () => {
await this.stopAudio();
});
this.triggerTempNumber();
} catch (error) {
handleError(error, "RecordService", "【录音服务RecordService】 == 初始化异常:");
handleError(error, "RecordService", "【录音服务 RecordService】 == 初始化异常:");
}
}
// initReception() {
......@@ -194,7 +198,7 @@ export class RecordService {
// this.initReception();
}
this.audioManager?.start();
// this.timeoutService!.start()
this.timeoutService?.start();
return true; // 返回 true 表示成功启动
} catch (error) {
handleError(error, "RecordService", "【录音服务RecordService】 == startAudio 异常:");
......@@ -209,6 +213,7 @@ export class RecordService {
stop() {
this.record?.clearLoopTimer();
this.stopAudioRecord();
this.timeoutService?.stop();
}
async stopAudio(isNotSave: boolean = false) {
......
import { logger } from "../logger";
/**
* 超时服务类
* 用于监控用户操作,如果在指定时间内无操作则自动停止录音
*/
export class TimeoutService {
static instance: null | TimeoutService = null;
__timerId: null | number = null;
_debounceTimer: null | number = null;
_lastActiveTime: number = 0;
readonly CHECK_INTERVAL: number;
readonly INTERVAL: number = 1;// 1 分钟检查一次
readonly MINIUTE: number = 60 * 1000;
onStop: (() => Promise<void>) | null = null;
readonly TIMEOUT: number = 1; // 40 分钟
readonly TIMER_DURATION: number;
constructor(onStop: () => Promise<void>) {
this.onStop = onStop;
this.TIMER_DURATION = this.TIMEOUT * this.MINIUTE;
this.CHECK_INTERVAL = this.INTERVAL * this.MINIUTE;
}
/**
* 获取单例实例
*/
static getInstance(onStop: () => Promise<void>): TimeoutService {
if (TimeoutService.instance === null) {
TimeoutService.instance = new TimeoutService(onStop);
}
return TimeoutService.instance;
}
/**
* 启动内部监听定时器
*/
__watchStart(): void {
const timerId = setInterval(() => {
const now = Date.now();
// 直接计算时间差,比维护 counter 计数器更准确、更简单
if (now - this._lastActiveTime >= this.TIMER_DURATION) {
this.__watchStop();
if (this.__timerId === null) {
logger.info(`== 检测到${this.TIMEOUT}分钟闲置未操作,自动停止录音 ==`);
}
}
}, this.CHECK_INTERVAL);
this.__timerId = timerId;
if (timerId !== null) {
logger.info("== 开启闲置检测 ==");
}
}
/**
* 停止监听并触发录音停止
*/
async __watchStop(): Promise<void> {
await this.stopRecorder();
this.endWatchClick();
}
/**
* 结束监听点击,清除定时器
*/
endWatchClick(): void {
const timerId = this.__timerId;
if (timerId !== null) {
clearInterval(timerId);
this.__timerId = null;
}
this._lastActiveTime = 0;
logger.info("== 停止闲置检测 ==");
}
/**
* 处理触摸开始事件(防抖)
* 重置活跃时间
*/
handleTouchStart = (): void => {
// 实现防抖
const debounceTimer = this._debounceTimer;
if (debounceTimer !== null) {
clearTimeout(debounceTimer);
}
const timerId = setTimeout(() => {
this._lastActiveTime = Date.now();
logger.info("== 用户操作,重置超时计时器 ==");
}, 1000);
this._debounceTimer = timerId;
};
/**
* 启动超时服务
* 监听 reset_timeout 事件并开始监控
*/
start(): void {
uni.$on("reset_timeout", this.handleTouchStart);
this.watch();
}
/**
* 停止超时服务
* 移除事件监听并清除定时器
*/
stop(): void {
uni.$off("reset_timeout", this.handleTouchStart);
this.endWatchClick();
}
/**
* 异步停止录音
*/
async stopRecorder(): Promise<void> {
const callback = this.onStop;
if (callback !== null) {
return await callback();
}
}
/**
* 初始化或启动监听
* 更新活跃时间并启动周期性检查
*/
watch(): void {
// 1. 无论是否已启动,先更新活跃时间
this._lastActiveTime = Date.now();
// 2. 如果定时器已在运行,直接返回,避免重复创建
if (this.__timerId !== null) return;
// 3. 启动周期性检查
this.__watchStart();
}
}
export default TimeoutService;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!