Skip to content

Commit

Permalink
feat(iOS): allow for audio session to be kept (react-native-camera#2636)
Browse files Browse the repository at this point in the history
* allow for audio session to be kept even after unmounts

* readme typo
  • Loading branch information
cristianoccazinsp authored and sibelius committed Dec 10, 2019
1 parent 8c6a26f commit fe5d11d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
8 changes: 7 additions & 1 deletion docs/RNCamera.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ Values: boolean `true` (default) | `false`
Specifies if audio recording permissions should be requested.
Make sure to follow README instructions for audio recording permissions [here](README.md).

### iOS `keepAudioSession`

Values: boolean `true` | `false` (false)

(iOS Only) When the camera is unmounted, it will release any audio session it acquired (if `captureAudio=true`) so other media can continue playing. However, this might not be always desirable (e.g., if video is played afterwards) and can be disabled by setting it to `true`. Setting this to `true`, means your app will not release the audio session. Note: other apps might still "steal" the audio session from your app.

### `flashMode`

Values: `RNCamera.Constants.FlashMode.off` (default), `RNCamera.Constants.FlashMode.on`, `RNCamera.Constants.FlashMode.auto` or `RNCamera.Constants.FlashMode.torch`.
Expand Down Expand Up @@ -698,7 +704,7 @@ A rewritten version of `react-native-barcode-mask` using `Hooks` and `Reanimated
- Customizable
- Provide custom hook to "scan barcode within finder area"

Read more about it here [@nartc/react-native-barcode-mask](https://github.com/nartc/react-native-barcode-mask)
Read more about it here [@nartc/react-native-barcode-mask](https://github.com/nartc/react-native-barcode-mask)

## Testing

Expand Down
1 change: 1 addition & 0 deletions ios/RN/RNCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
@property(nonatomic, assign) BOOL canDetectFaces;
@property(nonatomic, assign) BOOL canDetectBarcodes;
@property(nonatomic, assign) BOOL captureAudio;
@property(nonatomic, assign) BOOL keepAudioSession;
@property(nonatomic, assign) CGRect rectOfInterest;
@property(assign, nonatomic) AVVideoCodecType videoCodecType;
@property(assign, nonatomic)
Expand Down
15 changes: 9 additions & 6 deletions ios/RN/RNCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -1342,19 +1342,22 @@ - (void)removeAudioCaptureSessionInput

// Deactivate our audio session so other audio can resume
// playing, if any. E.g., background music.
NSError *error = nil;
// unless told not to
if(!self.keepAudioSession){
NSError *error = nil;

BOOL setInactive = [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
BOOL setInactive = [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];

if (!setInactive) {
RCTLogWarn(@"Audio device could not set inactive: %s: %@", __func__, error);
if (!setInactive) {
RCTLogWarn(@"Audio device could not set inactive: %s: %@", __func__, error);
}
}

self.audioCaptureDeviceInput = nil;

// inform that audio was interrupted
if(audioRemoved && self.onAudioInterrupted){
self.onAudioInterrupted(nil);
self.onAudioInterrupted(nil);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions ios/RN/RNCameraManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ + (NSDictionary *)barcodeDetectorConstants
[view updateCaptureAudio];
}

RCT_CUSTOM_VIEW_PROPERTY(keepAudioSession, BOOL, RNCamera)
{
[view setKeepAudioSession:[RCTConvert BOOL:json]];
}

RCT_CUSTOM_VIEW_PROPERTY(rectOfInterest, CGRect, RNCamera)
{
[view setRectOfInterest: [RCTConvert CGRect:json]];
Expand Down
3 changes: 3 additions & 0 deletions src/RNCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ type PropsType = typeof View.props & {
onFacesDetected?: ({ faces: Array<TrackedFaceFeature> }) => void,
onTextRecognized?: ({ textBlocks: Array<TrackedTextFeature> }) => void,
captureAudio?: boolean,
keepAudioSession?: boolean,
useCamera2Api?: boolean,
playSoundOnCapture?: boolean,
videoStabilizationMode?: number | string,
Expand Down Expand Up @@ -417,6 +418,7 @@ export default class Camera extends React.Component<PropsType, StateType> {
notAuthorizedView: PropTypes.element,
pendingAuthorizationView: PropTypes.element,
captureAudio: PropTypes.bool,
keepAudioSession: PropTypes.bool,
useCamera2Api: PropTypes.bool,
playSoundOnCapture: PropTypes.bool,
videoStabilizationMode: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
Expand Down Expand Up @@ -466,6 +468,7 @@ export default class Camera extends React.Component<PropsType, StateType> {
</View>
),
captureAudio: true,
keepAudioSession: false,
useCamera2Api: false,
playSoundOnCapture: false,
pictureSize: 'None',
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ export interface RNCameraProps {

// -- IOS ONLY PROPS
defaultVideoQuality?: keyof VideoQuality;
/* if true, audio session will not be released on component unmount */
keepAudioSession?: boolean;
}

interface Point<T = number> {
Expand Down

0 comments on commit fe5d11d

Please sign in to comment.