IOS Development --- video recording, ios --- recording

Source: Internet
Author: User

IOS Development --- video recording, ios --- recording

Today I have studied how to use the app to record videos. It seems quite simple. The AVFoundation framework is used, and the code is too dead. Just call it by step.

Share the Demo steps today


1. initialize the input device, which involves the front camera and microphone (imported to AVFoundation)

// 1. NSArray * devices = [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo]; // 2. initialize a camera input device (first is the rear camera, last is the front camera) AVCaptureDeviceInput * inputVideo = [AVCaptureDeviceInput deviceInputWithDevice: [devices firstObject] error: NULL]; // 3. create a microphone device AVCaptureDevice * deviceAudio = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeAudio]; // 4. initialize the microphone input device AVCaptureDeviceInput * inputAudio = [AVCaptureDeviceInput deviceInputWithDevice: deviceAudio error: NULL];

 

Ii. initialize video file output

// 5. initialize a movie file and output AVCaptureMovieFileOutput * output = [[AVCaptureMovieFileOutput alloc] init]; self. output = output; // Save the output to facilitate the following operations

 

3. initialize the session and add the input and output devices to the session.

1 // 6. initialize a session 2 AVCaptureSession * session = [[AVCaptureSession alloc] init]; 3 4 // 7. add the input and output devices to the session 5 if ([session canAddInput: inputVideo]) {6 [session addInput: inputVideo]; 7} 8 if ([session canAddInput: inputAudio]) {9 [session addInput: inputAudio]; 10} 11 if ([session canAddOutput: output]) {12 [session addOutput: output]; 13}

 

4. Add a video preview layer, set the size, and add it to the layer of the controller view.

1 // 8. create a preview coating 2 AVCaptureVideoPreviewLayer * preLayer = [AVCaptureVideoPreviewLayer layerWithSession: session]; 3 // set the layer size 4 preLayer. frame = self. view. bounds; 5 // Add to view 6 [self. view. layer addSublayer: preLayer];

 

5. Start a session

// 9. Start the session [session startRunning];

 

6. Add a button: Click start to stop recording the video and set the video recording proxy.

1-(IBAction) clickVideoBtn :( UIButton *) sender {2 // determine whether the recording is ongoing. if the recording is ongoing, stop and set the button title 3 if ([self. output isRecording]) {4 [self. output stopRecording]; 5 [sender setTitle: @ "Recording" forState: UIControlStateNormal]; 6 return; 7} 8 9 // title10 [sender setTitle: @ "stop" forState: UIControlStateNormal]; 11 12 // 10. start recording video 13 // set the path for saving the recorded video 14 NSString * path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ). lastObject stringByAppendingPathComponent: @ "myVidio. mov "]; 15 16 // url17 NSURL * url = [NSURL fileURLWithPath: path]; 18 19 // start recording, and set the Controller as the recording proxy 20 [self. output startRecordingToOutputFileURL: url recordingDelegate: self]; 21 22}

 

7. Implement the proxy method (here, only one proxy method is implemented, and others are set based on your needs)

1 # pragma mark-success // recording completion proxy 3-(void) captureOutput :( AVCaptureFileOutput *) captureOutput failed :( NSURL *) outputFileURL fromConnections :( NSArray *) connections error :( NSError *) error4 {5 NSLog (@ "complete recording, you can perform further processing on your own"); 6}

 

8. Real machine running, hey

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.