Skip to content

Commit

Permalink
Add start/stopPreview for Camera
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrevik committed Aug 27, 2017
1 parent e3fc277 commit 1bcf857
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ Returns whether or not the camera has flash capabilities.

Ends the current capture session for video captures. Only applies when the current `captureMode` is `video`.

#### `stopPreview()`

Stops the camera preview from running, and natively will make the current capture session pause.

#### `startPreview()`

Starts the camera preview again if previously stopped.

## Component static methods

#### `iOS` `Camera.checkDeviceAuthorizationStatus(): Promise`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,18 @@ public void stopCapture(final Promise promise) {
}
}

@ReactMethod
public void stopPreview(ReadableMap options) {
Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type"));
camera.stopPreview();
}

@ReactMethod
public void startPreview(ReadableMap options) {
Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type"));
camera.startPreview();
}

@ReactMethod
public void hasFlash(ReadableMap options, final Promise promise) {
Camera camera = RCTCamera.getInstance().acquireCameraInstance(options.getInt("type"));
Expand Down
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,28 @@ export default class Camera extends Component {
return CameraManager.capture(options);
}

startPreview() {
if (Platform.OS === 'android') {
const props = convertNativeProps(this.props);
CameraManager.startPreview({
type: props.type
});
} else {
CameraManager.startPreview();
}
}

stopPreview() {
if (Platform.OS === 'android') {
const props = convertNativeProps(this.props);
CameraManager.stopPreview({
type: props.type
});
} else {
CameraManager.stopPreview();
}
}

stopCapture() {
if (this.state.isRecording) {
this.setState({ isRecording: false });
Expand Down
22 changes: 22 additions & 0 deletions ios/RCTCameraManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,28 @@ - (id)init {
}
}

RCT_EXPORT_METHOD(stopPreview) {
#if TARGET_IPHONE_SIMULATOR
return;
#endif
dispatch_async(self.sessionQueue, ^{
if ([self.session isRunning]) {
[self.session stopRunning];
}
});
}

RCT_EXPORT_METHOD(startPreview) {
#if TARGET_IPHONE_SIMULATOR
return;
#endif
dispatch_async(self.sessionQueue, ^{
if (![self.session isRunning]) {
[self.session startRunning];
}
});
}

RCT_EXPORT_METHOD(stopCapture) {
if (self.movieFileOutput.recording) {
[self.movieFileOutput stopRecording];
Expand Down

0 comments on commit 1bcf857

Please sign in to comment.