Your first video app: show a video preview on screen
2013-07-21 16:34
330 查看
This is a very basic demo showing video preview on screen using AVFoundation framework. To learn more about development with AVFoundation, visit http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html
1. Create a single view iOS project with XCode
2. Add the following line to ViewController.m
#import <AVFoundation/AVFoundation.h>
3. Add the following codes to ViewDidLoad() function in ViewController.m
// create and initialize AVCaptureSession instance
AVCaptureSession *session = [[AVCaptureSession
alloc] init];
[session setSessionPreset:AVCaptureSessionPresetHigh];
AVCaptureDevice *device = [AVCaptureDevice
defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput
deviceInputWithDevice:device
error:&error];
if (deviceInput) {
[session addInput:deviceInput];
// show video preview layer
[self
showVideoPreviewWithSession:session];
[session startRunning];
}
else {
// handle failure
}
4. Add the following function to ViewController.m
- (void)showVideoPreviewWithSession:(AVCaptureSession *)session
{
AVCaptureVideoPreviewLayer *previewLayer =
nil;
previewLayer = [[AVCaptureVideoPreviewLayer
alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[self
view] layer];
[rootLayer setMasksToBounds:YES];
[previewLayer setFrame:CGRectMake(-70,
0, rootLayer.bounds.size.height, rootLayer.bounds.size.height)];
[rootLayer insertSublayer:previewLayer
atIndex:0];
}
5. Double click project name to open project settings -> Build Phases, click "+" in Link Binary with Libraries section and add AVFoundation.framework to linking libraries. By default, XCode only includes some basic libraries. Without proper configuration,
the builder will give some linker error.
6. Build and run the app on iOS device. (NOTE: This works on real iOS device, showing the video stream preview on screen when you start the application The iphone simulator does not show anything but a white screen. Not sure if it is possible with simulator.
1. Create a single view iOS project with XCode
2. Add the following line to ViewController.m
#import <AVFoundation/AVFoundation.h>
3. Add the following codes to ViewDidLoad() function in ViewController.m
// create and initialize AVCaptureSession instance
AVCaptureSession *session = [[AVCaptureSession
alloc] init];
[session setSessionPreset:AVCaptureSessionPresetHigh];
AVCaptureDevice *device = [AVCaptureDevice
defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput
deviceInputWithDevice:device
error:&error];
if (deviceInput) {
[session addInput:deviceInput];
// show video preview layer
[self
showVideoPreviewWithSession:session];
[session startRunning];
}
else {
// handle failure
}
4. Add the following function to ViewController.m
- (void)showVideoPreviewWithSession:(AVCaptureSession *)session
{
AVCaptureVideoPreviewLayer *previewLayer =
nil;
previewLayer = [[AVCaptureVideoPreviewLayer
alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[self
view] layer];
[rootLayer setMasksToBounds:YES];
[previewLayer setFrame:CGRectMake(-70,
0, rootLayer.bounds.size.height, rootLayer.bounds.size.height)];
[rootLayer insertSublayer:previewLayer
atIndex:0];
}
5. Double click project name to open project settings -> Build Phases, click "+" in Link Binary with Libraries section and add AVFoundation.framework to linking libraries. By default, XCode only includes some basic libraries. Without proper configuration,
the builder will give some linker error.
6. Build and run the app on iOS device. (NOTE: This works on real iOS device, showing the video stream preview on screen when you start the application The iphone simulator does not show anything but a white screen. Not sure if it is possible with simulator.
相关文章推荐
- Build Your First Mobile App With Ionic 2 & Angular 2 - Part 3
- Building Your First App
- Your First iOS app(11)--Tutorial: Add Data(一)
- Installing your app on your Windows RT device
- Creating Your First Mac App--Implementing Action Methods 实现动作方法
- Verify the Developer App certificate for your account is trusted on your device
- Update your app to take advantage of the larger aspect ratio on new Android flagship devices
- how to fix some resource like video not show on iphone 6 but show on simulator
- Building your First App
- video : Write and Submit your first Linux kernel Patch
- Writing your first Django app--Django 第一步
- Build Your First Mobile App With Ionic 2 & Angular 2 - Part 4
- EF6 Code First & Auto Migration on Appharbor
- How to take screenshots on your Samsung Galaxy Tab 10.1
- Building Your First App笔记
- 搭建自己的Django应用(3)Writing your first Django app, part 3
- A Step-by-Step Guide to Your First AngularJS App
- Your First iOS App
- Build Your First Mobile App With Ionic 2 & Angular 2 - Part 5
- Your First Step to the Silverlight Voice/Video Chatting Client/Server