0
A
回答
0
这里是我的代码 -
Here is my code :-
#pragma mark - Configure Video Recorder method
-(void)configureVideoRecorder{
//---------------------------------
//----- SETUP CAPTURE SESSION -----
//---------------------------------
NSLog(@"Setting up capture session");
CaptureSession = [[AVCaptureSession alloc] init];
[CaptureSession beginConfiguration];
//----- ADD INPUTS -----
NSLog(@"Adding video input");
//ADD VIDEO INPUT
AVCaptureDevice *VideoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (VideoDevice)
{
NSError *error;
VideoInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:VideoDevice error:&error];
if (!error)
{
if ([CaptureSession canAddInput:VideoInputDevice])
[CaptureSession addInput:VideoInputDevice];
else
NSLog(@"Couldn't add video input");
}
else
{
NSLog(@"Couldn't create video input");
}
}
else
{
NSLog(@"Couldn't create video capture device");
}
//ADD AUDIO INPUT
NSLog(@"Adding audio input");
AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
NSError *error = nil;
AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
if (audioInput)
{
[CaptureSession addInput:audioInput];
}
//----- ADD OUTPUTS -----
//ADD VIDEO PREVIEW LAYER
NSLog(@"Adding video preview layer");
[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:CaptureSession]];
PreviewLayer.orientation = AVCaptureVideoOrientationPortrait; //<<SET ORIENTATION. You can deliberatly set this wrong to flip the image and may actually need to set it wrong to get the right image
[[self PreviewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
//ADD MOVIE FILE OUTPUT
NSLog(@"Adding movie file output");
MovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
Float64 TotalSeconds = 60; //Total seconds
int32_t preferredTimeScale = 30; //Frames per second
CMTime maxDuration = CMTimeMakeWithSeconds(TotalSeconds, preferredTimeScale); //<<SET MAX DURATION
MovieFileOutput.maxRecordedDuration = maxDuration;
MovieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024; //<<SET MIN FREE SPACE IN BYTES FOR RECORDING TO CONTINUE ON A VOLUME
if ([CaptureSession canAddOutput:MovieFileOutput])
[CaptureSession addOutput:MovieFileOutput];
//SET THE CONNECTION PROPERTIES (output properties)
[self CameraSetOutputProperties]; //(We call a method as it also has to be done after changing camera)
//----- SET THE IMAGE QUALITY/RESOLUTION -----
//Options:
// AVCaptureSessionPresetHigh - Highest recording quality (varies per device)
// AVCaptureSessionPresetMedium - Suitable for WiFi sharing (actual values may change)
// AVCaptureSessionPresetLow - Suitable for 3G sharing (actual values may change)
// AVCaptureSessionPreset640x480 - 640x480 VGA (check its supported before setting it)
// AVCaptureSessionPreset1280x720 - 1280x720 720p HD (check its supported before setting it)
// AVCaptureSessionPresetPhoto - Full photo resolution (not supported for video output)
NSLog(@"Setting image quality");
[CaptureSession setSessionPreset:AVCaptureSessionPresetMedium];
// if ([CaptureSession canSetSessionPreset:AVCaptureSessionPreset640x480]) //Check size based configs are supported before setting them
// [CaptureSession setSessionPreset:AVCaptureSessionPreset640x480];
//----- DISPLAY THE PREVIEW LAYER -----
//Display it full screen under out view controller existing controls
NSLog(@"Display the preview layer");
CGRect layerRect = [[[self view] layer] bounds];
[PreviewLayer setBounds:layerRect];
[PreviewLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),
CGRectGetMidY(layerRect))];
//[[[self view] layer] addSublayer:[[self CaptureManager] previewLayer]];
//We use this instead so it goes on a layer behind our UI controls (avoids us having to manually bring each control to the front):
UIView *CameraView = [[UIView alloc] init];
[[self view] addSubview:CameraView];
[self.view sendSubviewToBack:CameraView];
[[CameraView layer] addSublayer:PreviewLayer];
[CaptureSession commitConfiguration];
//----- START THE CAPTURE SESSION RUNNING -----
[CaptureSession startRunning];
}
#pragma mark - CAMERA SET OUTPUT PROPERTIES method
- (void) CameraSetOutputProperties
{
//SET THE CONNECTION PROPERTIES (output properties)
AVCaptureConnection *CaptureConnection = [MovieFileOutput connectionWithMediaType:AVMediaTypeVideo];
//Set landscape (if required)
if ([CaptureConnection isVideoOrientationSupported])
{
AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationPortrait; //<<<<<SET VIDEO ORIENTATION IF LANDSCAPE
[CaptureConnection setVideoOrientation:orientation];
}
//Set frame rate (if requried)
CMTimeShow(CaptureConnection.videoMinFrameDuration);
CMTimeShow(CaptureConnection.videoMaxFrameDuration);
if (CaptureConnection.supportsVideoMinFrameDuration)
CaptureConnection.videoMinFrameDuration = CMTimeMake(1, CAPTURE_FRAMES_PER_SECOND);
if (CaptureConnection.supportsVideoMaxFrameDuration)
CaptureConnection.videoMaxFrameDuration = CMTimeMake(1, CAPTURE_FRAMES_PER_SECOND);
CMTimeShow(CaptureConnection.videoMinFrameDuration);
CMTimeShow(CaptureConnection.videoMaxFrameDuration);
}
#pragma mark - GET CAMERA IN SPECIFIED POSITION IF IT EXISTS method
- (AVCaptureDevice *) CameraWithPosition:(AVCaptureDevicePosition) Position
{
NSArray *Devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *Device in Devices)
{
if ([Device position] == Position)
{
return Device;
}
}
return nil;
}
#pragma mark - START STOP RECORDING method
- (void)recordVideo
{
if (!WeAreRecording)
{
//----- START RECORDING -----
NSLog(@"START RECORDING");
WeAreRecording = YES;
//Create temporary URL to record to
NSString *outputPath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
outputPath = [documentsDirectory stringByAppendingFormat:@"/firstVideo.mp4"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath])
{
NSError *error;
if ([fileManager removeItemAtPath:outputPath error:&error] == NO)
{
//Error - handle if requried
}
}
//Start recording
[MovieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
}
else
{
//----- STOP RECORDING -----
NSLog(@"STOP RECORDING");
WeAreRecording = NO;
[audioPlayer stop];
[MovieFileOutput stopRecording];
}
}
#pragma mark - AVCaptureFileOutput didStartRecordingToOutputFileAtURL method
-(void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections{
[self playSound:selectedMp3];
}
#pragma mark - DID FINISH RECORDING TO OUTPUT FILE AT URL method
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
fromConnections:(NSArray *)connections
error:(NSError *)error
{
NSLog(@"didFinishRecordingToOutputFileAtURL - enter");
BOOL RecordedSuccessfully = YES;
if ([error code] != noErr)
{
// A problem occurred: Find out if the recording was successful.
id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
if (value)
{
RecordedSuccessfully = [value boolValue];
}
}
if (RecordedSuccessfully)
{
//----- RECORDED SUCESSFULLY -----
NSLog(@"didFinishRecordingToOutputFileAtURL - success");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Video recorded successfully" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
alert.tag = 1;
[alert show];
}
}
1
试试这个
[self.avcSession setSessionPreset:AVCaptureSessionPreset640x480];
+0
我已经试过了,但没有得到帮助。 –
+0
哥们后ur代码。所以我们可以帮助你更多 – vaibby
相关问题
- 1. 使用AVFoundation进行视频录制
- 2. 使用AVFoundation Framework iPhone进行视频录制?
- 3. 在iOS中使用AVFoundation录制视频
- 4. 使用AVFoundation暂停视频录制
- 5. 使用AVFoundation Swift录制视频
- 6. 视频不再录制音频(PBJVision/AVFoundation)
- 7. 使用AVFoundation进行录制时,可以使用视频文件“chunking”/ segmenting吗?
- 8. 播放用AVFoundation录制的视频
- 9. 使用GPUImage进行视频录制Android
- 10. 使用AVFoundation逐帧录制音频和视频
- 11. 使用AVfoundation框架将视频保存到iPhone库
- 12. 摄像头的视频帧在iOS中使用AVFoundation框架?
- 13. 使用AVFoundation在iPhone上录制视频时出现错误
- 14. 使用AVFoundation暂停/恢复视频录制
- 15. 我可以使用AVFoundation在iPhone 3G上录制视频吗?
- 16. 使用AVFoundation录制视频会停止播放声音
- 17. 使用AVFoundation Framework(AVCaptureSession)在iPhone中录制视频?
- 18. 如何使用AVFoundation在x轴上翻转录制的视频?
- 19. 使用AVFoundation录制后无法播放视频
- 20. 使用AVFoundation/QTKit一次录制多个视频
- 21. 使用目标c中的AVFoundation进行视频编辑
- 22. AVFoundation框架和Mediaplyer框架:在iOS应用程序中播放音频/视频的最佳框架是哪个?
- 23. 使用闪光灯进行音频/视频录制
- 24. 视频与AVFoundation
- 25. 在C#中使用Aforge框架录制视频?
- 26. 斯威夫特IOS录制视频和音频AVFoundation
- 27. 期运用AVFoundation录制视频,然后将其上传到parse.com
- 28. AVFoundation捕获视频
- 29. 无法使用AVFoundation将录制的视频上传到服务器 - 使用AVFoundation将Swift 3
- 30. AVFoundation框架错误
具有u使用AVCaptureSessionPreset640x480这个? – vaibby