feat(iOS): allow for audio session to be kept (#2636)

* allow for audio session to be kept even after unmounts

* readme typo
This commit is contained in:
cristianoccazinsp 2019-12-10 11:02:45 -03:00 committed by Sibelius Seraphini
parent 8c6a26f289
commit fe5d11d12f
6 changed files with 27 additions and 7 deletions

View File

@ -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`.
@ -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

View File

@ -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)

View File

@ -1342,19 +1342,22 @@ BOOL _sessionInterrupted = NO;
// 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);
}
}
}

View File

@ -305,6 +305,11 @@ RCT_CUSTOM_VIEW_PROPERTY(captureAudio, BOOL, RNCamera)
[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]];

View File

@ -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,
@ -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]),
@ -466,6 +468,7 @@ export default class Camera extends React.Component<PropsType, StateType> {
</View>
),
captureAudio: true,
keepAudioSession: false,
useCamera2Api: false,
playSoundOnCapture: false,
pictureSize: 'None',

2
types/index.d.ts vendored
View File

@ -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> {