解决录制结束后,无法重新预览相机问题

This commit is contained in:
2026-06-05 15:10:03 +08:00
parent a39fcdb929
commit 016aad49b7
2 changed files with 49 additions and 12 deletions

View File

@@ -161,22 +161,31 @@ class RecordingPlatformHandler(
controller.stopRecording { path -> controller.stopRecording { path ->
RecordingSession.stopForeground(activity) RecordingSession.stopForeground(activity)
DoNotDisturbHelper.disable(activity) DoNotDisturbHelper.disable(activity)
mainHandler.post { val previewView = activity.recordingPreviewView
val gallerySaved = path != null && controller.status.state != RecordingState.ERROR if (previewView == null) {
val payload = mainHandler.post { deliverStopResult(result, path) }
mutableMapOf<String, Any?>( return@stopRecording
"outputPath" to path, }
"status" to controller.status.toMap(), controller.rebindForRecording(activity, previewView) { _ ->
"gallerySaved" to gallerySaved, mainHandler.post { deliverStopResult(result, path) }
)
if (!gallerySaved) {
payload["galleryErrorMessage"] = controller.status.message ?: "保存到相册失败"
}
result.success(payload)
} }
} }
} }
private fun deliverStopResult(result: MethodChannel.Result, path: String?) {
val gallerySaved = path != null && controller.status.state != RecordingState.ERROR
val payload =
mutableMapOf<String, Any?>(
"outputPath" to path,
"status" to controller.status.toMap(),
"gallerySaved" to gallerySaved,
)
if (!gallerySaved) {
payload["galleryErrorMessage"] = controller.status.message ?: "保存到相册失败"
}
result.success(payload)
}
private fun setImmersiveMode(enabled: Boolean) { private fun setImmersiveMode(enabled: Boolean) {
val window = activity.window val window = activity.window
WindowCompat.setDecorFitsSystemWindows(window, !enabled) WindowCompat.setDecorFitsSystemWindows(window, !enabled)

View File

@@ -196,6 +196,32 @@ class RecordingViewModel extends Notifier<RecordingModel> {
throw StateError('initializePreview retry exhausted'); throw StateError('initializePreview retry exhausted');
} }
/// 停止录制后重新绑定相机预览(重置 isPreviewReady 以显示加载遮罩)。
Future<void> restorePreview() async {
if (!RecordingPlatform.isSupported) return;
_updateSession((s) => s.copyWith(isPreviewReady: false, errorMessage: null));
try {
final status = await _initializePreviewWithRetry();
_updateSession(
(s) => s.copyWith(
status: status,
isPreviewReady: status.state == RecordingState.previewing,
errorMessage: status.state == RecordingState.previewing
? null
: (status.message ?? '相机预览初始化失败'),
),
);
} on PlatformException catch (error) {
_updateSession(
(s) => s.copyWith(
isPreviewReady: false,
errorMessage: error.message ?? '相机预览初始化失败',
),
);
}
}
List<Permission> _galleryPermissions() { List<Permission> _galleryPermissions() {
if (Platform.isIOS) { if (Platform.isIOS) {
return [Permission.photosAddOnly, Permission.photos]; return [Permission.photosAddOnly, Permission.photos];
@@ -280,6 +306,8 @@ class RecordingViewModel extends Notifier<RecordingModel> {
_updateSession( _updateSession(
(s) => s.copyWith(errorMessage: error.message ?? '停止录制失败'), (s) => s.copyWith(errorMessage: error.message ?? '停止录制失败'),
); );
} finally {
await restorePreview();
} }
} }