在录音时,不能息屏

This commit is contained in:
zhonghua.li
2026-04-18 22:41:20 +08:00
parent 811f5ad16b
commit e7976c0993
51 changed files with 179 additions and 4 deletions

View File

@@ -133,6 +133,10 @@
<text>结束服务</text>
</view>
</view>
<view v-if="isRecordingItem(item.id)" class="recording-realtime">
<text>已录音{{ formatRecordingElapsed(recordingElapsedSeconds) }}</text>
<text>当前大小{{ formatRecordingSize(recordingEstimatedSizeBytes) }}</text>
</view>
</view>
<view class="service-status-empty" v-if="!serviceStatusLoading && !serviceStatusList.length">
<text>{{ emptyListHint }}</text>
@@ -268,7 +272,11 @@ export default {
recorderSupported: false,
recorderManager: null,
recordingServiceId: null,
recordingContext: null
recordingContext: null,
recordingElapsedSeconds: 0,
recordingEstimatedSizeBytes: 0,
recordingTicker: null,
keepScreenOnEnabled: false
}
},
mounted() {
@@ -280,6 +288,8 @@ export default {
this.fetchServiceStatusList();
},
beforeUnmount() {
this.stopRecordingTicker();
this.setKeepScreenOn(false);
if (this.recorderManager && this.recordingServiceId) {
try {
this.recorderManager.stop();
@@ -300,6 +310,8 @@ export default {
const ctx = this.recordingContext;
this.recordingServiceId = null;
this.recordingContext = null;
this.stopRecordingTicker();
this.setKeepScreenOn(false);
const tempFilePath = res.tempFilePath || '';
if (!ctx || !ctx.id) {
return;
@@ -314,9 +326,60 @@ export default {
this.recorderManager.onError(() => {
this.recordingServiceId = null;
this.recordingContext = null;
this.stopRecordingTicker();
this.setKeepScreenOn(false);
uni.showToast({ title: '录音出错', icon: 'none' });
});
},
setKeepScreenOn(enabled) {
if (typeof uni.setKeepScreenOn !== 'function') {
return;
}
uni.setKeepScreenOn({
keepScreenOn: !!enabled,
success: () => {
this.keepScreenOnEnabled = !!enabled;
},
fail: () => {
this.keepScreenOnEnabled = false;
},
});
},
startRecordingTicker() {
this.stopRecordingTicker();
this.recordingElapsedSeconds = 0;
this.recordingEstimatedSizeBytes = 0;
// 96kbpsencodeBitRate约等于每秒 12000 字节
const bytesPerSecond = 96000 / 8;
this.recordingTicker = setInterval(() => {
this.recordingElapsedSeconds += 1;
this.recordingEstimatedSizeBytes = Math.floor(this.recordingElapsedSeconds * bytesPerSecond);
}, 1000);
},
stopRecordingTicker() {
if (this.recordingTicker) {
clearInterval(this.recordingTicker);
}
this.recordingTicker = null;
this.recordingElapsedSeconds = 0;
this.recordingEstimatedSizeBytes = 0;
},
formatRecordingElapsed(seconds) {
const safeSeconds = Math.max(0, Number(seconds) || 0);
const minutes = String(Math.floor(safeSeconds / 60)).padStart(2, '0');
const remainSeconds = String(safeSeconds % 60).padStart(2, '0');
return `${minutes}:${remainSeconds}`;
},
formatRecordingSize(bytes) {
const size = Math.max(0, Number(bytes) || 0);
if (size < 1024) {
return `${size} B`;
}
if (size < 1024 * 1024) {
return `${(size / 1024).toFixed(1)} KB`;
}
return `${(size / (1024 * 1024)).toFixed(2)} MB`;
},
isRecordingItem(recordId) {
if (!recordId || !this.recordingServiceId) {
return false;
@@ -356,9 +419,13 @@ export default {
encodeBitRate: 96000,
format: 'mp3'
});
this.setKeepScreenOn(true);
this.startRecordingTicker();
} catch (e) {
this.recordingServiceId = null;
this.recordingContext = null;
this.stopRecordingTicker();
this.setKeepScreenOn(false);
uni.showToast({ title: '无法开始录音', icon: 'none' });
}
},
@@ -370,6 +437,8 @@ export default {
if (String(item.id) !== this.recordingServiceId) {
return;
}
this.stopRecordingTicker();
this.setKeepScreenOn(false);
this.recorderManager.stop();
},
/**
@@ -1449,6 +1518,18 @@ export default {
color: #999;
font-size: 28rpx;
}
.recording-realtime {
margin-top: 12rpx;
display: flex;
align-items: center;
gap: 20rpx;
}
.recording-realtime text {
font-size: 24rpx;
color: #FF3B30;
}
/* 补录弹窗样式 */
.supplement-dialog-mask {