设置帧率应该 对输入设备进行设置 AVCaptureDevice

  1. - (AVCaptureDevice *)cameraDevice{
  2. if (!_cameraDevice) {
  3. _cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  4. }
  5. return _cameraDevice;
  6. }
  7. - (void)configActiveVideoMaxFrameDuration{
  8. /*
  9. 需要放到 addInput: 之后设置
  10. On iOS, the receiver's activeVideoMaxFrameDuration resets to its default value under the following conditions:
  11. - The receiver's activeFormat changes
  12. - The receiver's AVCaptureDeviceInput's session's sessionPreset changes
  13. - The receiver's AVCaptureDeviceInput is added to a session
  14. */
  15. NSArray *formats = self.cameraDevice.formats;
  16. NSLog(@"%@",formats);
  17. AVCaptureDeviceFormat *targetFormat = formats.lastObject;
  18. // for (AVCaptureDeviceFormat *format in formats) {
  19. // CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(format.formatDescription);
  20. //// 4032x3024
  21. // if (dimensions.width >= 4032) {
  22. // NSLog(@"%@-%d-%d",format, dimensions.width, dimensions.height);
  23. // targetFormat = format;
  24. // break;
  25. // }
  26. // }
  27. //
  28. NSError *error;
  29. [self.session beginConfiguration];
  30. [self.cameraDevice lockForConfiguration:&error];
  31. [self.cameraDevice setActiveFormat:targetFormat];
  32. self.cameraDevice.activeVideoMaxFrameDuration = CMTimeMake(1, 3);
  33. self.cameraDevice.activeVideoMinFrameDuration = CMTimeMake(1, 3);
  34. [self.cameraDevice unlockForConfiguration];
  35. [self.session commitConfiguration];
  36. }

On iOS, use of AVCaptureDevice’s setActiveFormat: and AVCaptureSession’s setSessionPreset: are mutually exclusive. If you set a capture device’s active format, the session to which it is attached changes its preset to AVCaptureSessionPresetInputPriority. Likewise if you set the AVCaptureSession’s sessionPreset property, the session assumes control of its input devices, and configures their activeFormat appropriately. Note that audio devices do not expose any user-configurable formats on iOS. To configure audio input on iOS, you should use the AVAudioSession APIs instead (see AVAudioSession.h).

  1. /*!
  2. @property activeFormat
  3. @abstract
  4. The currently active format of the receiver.
  5. @discussion
  6. This property can be used to get or set the currently active device format.
  7. -setActiveFormat: throws an NSInvalidArgumentException if set to a format not present in the formats array.
  8. -setActiveFormat: throws an NSGenericException if called without first obtaining exclusive access to the receiver using lockForConfiguration:.
  9. Clients can observe automatic changes to the receiver's activeFormat by key value observing this property.
  10. On iOS, use of AVCaptureDevice's setActiveFormat: and AVCaptureSession's setSessionPreset: are mutually exclusive. If you set a capture device's active format, the session to which it is attached changes its preset to AVCaptureSessionPresetInputPriority. Likewise if you set the AVCaptureSession's sessionPreset property, the session assumes control of its input devices, and configures their activeFormat appropriately. Note that audio devices do not expose any user-configurable formats on iOS. To configure audio input on iOS, you should use the AVAudioSession APIs instead (see AVAudioSession.h).
  11. The activeFormat, activeVideoMinFrameDuration, and activeVideoMaxFrameDuration properties may be set simultaneously by using AVCaptureSession's begin/commitConfiguration methods:
  12. [session beginConfiguration]; // the session to which the receiver's AVCaptureDeviceInput is added.
  13. if ( [device lockForConfiguration:&error] ) {
  14. [device setActiveFormat:newFormat];
  15. [device setActiveVideoMinFrameDuration:newMinDuration];
  16. [device setActiveVideoMaxFrameDuration:newMaxDuration];
  17. [device unlockForConfiguration];
  18. }
  19. [session commitConfiguration]; // The new format and frame rates are applied together in commitConfiguration
  20. Note that when configuring a session to use an active format intended for high resolution still photography and applying one or more of the following operations to an AVCaptureVideoDataOutput, the system may not meet the target framerate: zoom, orientation changes, format conversion.
  21. */
  22. @property(nonatomic, retain) AVCaptureDeviceFormat *activeFormat API_AVAILABLE(ios(7.0), macCatalyst(14.0)) API_UNAVAILABLE(tvos);