Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit cd40169

Browse files
juliocbcottaMichael Klimushyn
authored andcommitted
fix android crash when pausing or resuming video on APIs lower than 24. (#2029)
This PR fixes a crash when pause/resume methods are called in APIs lower than 24 on Android.
1 parent a941127 commit cd40169

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

packages/camera/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.4+1
2+
3+
* Fix Android pause and resume video crash when executing in APIs below 24.
4+
15
## 0.5.4
26

37
* Add feature to pause and resume video recording.

packages/camera/android/src/main/java/io/flutter/plugins/camera/Camera.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.media.Image;
2222
import android.media.ImageReader;
2323
import android.media.MediaRecorder;
24+
import android.os.Build;
2425
import android.util.Size;
2526
import android.view.OrientationEventListener;
2627
import android.view.Surface;
@@ -395,7 +396,12 @@ public void pauseVideoRecording(@NonNull final Result result) {
395396
}
396397

397398
try {
398-
mediaRecorder.pause();
399+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
400+
mediaRecorder.pause();
401+
} else {
402+
result.error("videoRecordingFailed", "pauseVideoRecording requires Android API +24.", null);
403+
return;
404+
}
399405
} catch (IllegalStateException e) {
400406
result.error("videoRecordingFailed", e.getMessage(), null);
401407
return;
@@ -411,7 +417,13 @@ public void resumeVideoRecording(@NonNull final Result result) {
411417
}
412418

413419
try {
414-
mediaRecorder.resume();
420+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
421+
mediaRecorder.resume();
422+
} else {
423+
result.error(
424+
"videoRecordingFailed", "resumeVideoRecording requires Android API +24.", null);
425+
return;
426+
}
415427
} catch (IllegalStateException e) {
416428
result.error("videoRecordingFailed", e.getMessage(), null);
417429
return;

packages/camera/example/lib/main.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
393393
await controller.pauseVideoRecording();
394394
} on CameraException catch (e) {
395395
_showCameraException(e);
396-
return null;
396+
rethrow;
397397
}
398398
}
399399

@@ -406,7 +406,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
406406
await controller.resumeVideoRecording();
407407
} on CameraException catch (e) {
408408
_showCameraException(e);
409-
return null;
409+
rethrow;
410410
}
411411
}
412412

@@ -477,6 +477,7 @@ List<CameraDescription> cameras;
477477
Future<void> main() async {
478478
// Fetch the available cameras before initializing the app.
479479
try {
480+
WidgetsFlutterBinding.ensureInitialized();
480481
cameras = await availableCameras();
481482
} on CameraException catch (e) {
482483
logError(e.code, e.description);

packages/camera/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera
22
description: A Flutter plugin for getting information about and controlling the
33
camera on Android and iOS. Supports previewing the camera feed, capturing images, capturing video,
44
and streaming image buffers to dart.
5-
version: 0.5.4
5+
version: 0.5.4+1
66

77
authors:
88
- Flutter Team <flutter-dev@googlegroups.com>

0 commit comments

Comments
 (0)