iOS 视频的录制、合成以及播放

根据项目要求,视频可以暂停然后继续录制。选择了视频合成。后面有链接

导入AVFoundation库文件用于支持:

AVCaptureSession 链接输入输出设备
AVCaptureDeviceInput 从设备获取输入设备
AVCaptureMovieFileOutput 视频输出
AVCaptureVideoPreviewLayer 录制预览layer

AVMutableComposition 根据MediaType返回音视频轨道
AVMutableVideoComposition 设施输出内容属性,大小,帧数。。。
AVAssetTrack 素材通道
AVURLAsset 获取音视频信息
AVAssetExportSession 输出视频

导入MediaPlayer库用于支持:
MPMoviePlayerController 视频播放控件

info.plist文件需要添加下面内容获取相应权限
Privacy - Camera Usage Description (是否允许此App使用你的相机?)
Privacy - Microphone Usage Description (是否允许此App使用你的麦克风?)

视频合成过程需要时间,这里没做处理,可以根据自己需要添加一个UIActivityIndicatorView

//
//  ViewController.m
//  VideoRecordText
//
//

#import "ViewController.h"


#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height


@interface ViewController ()<AVCaptureFileOutputRecordingDelegate>

@property (strong,nonatomic) AVCaptureSession *captureSession;//负责输入和输出设置之间的数据传递
@property (strong,nonatomic) AVCaptureDeviceInput *captureDeviceInput;//负责从AVCaptureDevice获得输入数据
@property (strong,nonatomic) AVCaptureMovieFileOutput *captureMovieFileOutput;//视频输出流
@property (strong,nonatomic) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;//相机拍摄预览图层

@property (nonatomic,strong)NSString * fileName;//文件夹名
@property (nonatomic,assign)int fileNum;//文件名
@property (nonatomic,strong)NSMutableArray * fileUrlArr;//文件路径数组


@property (nonatomic,strong)MPMoviePlayerController * mediaV;//播放器

@property (nonatomic,strong)UIView * playBcgV;//播放页面

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _fileUrlArr = [[NSMutableArray alloc]init];
    _fileName = [NSString stringWithFormat:@"%d",(int)[[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSince1970]];
    _fileNum = 0;

    [self createPlayBcgV];//播放视图

    [self createUI];

    // Do any additional setup after loading the view, typically from a nib.
}

-(void)createUI{


    UIView * layerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-140)];

    layerView.backgroundColor = [UIColor blackColor];


    //初始化会话
    _captureSession=[[AVCaptureSession alloc]init];
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        if (granted) {
        }else{
            NSLog(@"未获取摄像头权限");
        }
    }];
    //获得输入设备(视频)
    AVCaptureDevice *captureDevice=[self getCameraDeviceWithPosition:AVCaptureDevicePositionBack];//取得后置摄像头
    if (!captureDevice) {
        NSLog(@"取得后置摄像头时出现问题.");
        return;
    }

    NSError
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值