ios实现拍摄视频与显示在界面上

1、添加录音和拍摄权限

NSMicrophoneUsageDescription

Privacy - Camera Usage Description

2、代码

objectivec 复制代码
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MobileCoreServices/MobileCoreServices.h>

// 接下来是你的 ViewController 的实现代码,不需要修改。


@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) NSURL *videoURL;
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerLayer *playerLayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建显示视频的视图
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];
    [self.view addSubview:self.imageView];
    
    // 创建拍摄视频按钮
    UIButton *recordVideoButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [recordVideoButton setTitle:@"拍摄视频" forState:UIControlStateNormal];
    recordVideoButton.frame = CGRectMake(50, 350, 100, 50);
    [recordVideoButton addTarget:self action:@selector(recordVideo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:recordVideoButton];
}

- (void)recordVideo {
    // 检查相机是否可用
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error" message:@"相机不可用" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
        return;
    }
    
    // 创建 UIImagePickerController
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.mediaTypes = @[(NSString *)kUTTypeMovie];
    imagePicker.delegate = self;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

// 拍摄视频完成后调用的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
    NSString *mediaType = info[UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
        self.videoURL = info[UIImagePickerControllerMediaURL];
        [self displayVideo];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

// 用户取消拍摄视频时调用的代理方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

// 显示拍摄的视频
- (void)displayVideo {
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:self.videoURL];
    self.player = [AVPlayer playerWithPlayerItem:playerItem];
    self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playerLayer.frame = CGRectMake(50, 100, 200, 200);
    [self.view.layer addSublayer:self.playerLayer];
    [self.player play];
}

@end
相关推荐
LCG元3 小时前
Vue.js组件开发-实现对视频预览
前端·vue.js·音视频
DogDaoDao9 小时前
H266/VVC 量化编码中量化矩阵 QM 技术
音视频·视频编解码·h266·vvc·量化编码·vvenc·量化矩阵 qm
taopi202410 小时前
ios打包:uuid与udid
ios·iphone·ipad
有Li18 小时前
2D 超声心动图视频到 3D 心脏形状重建的临床应用| 文献速递-医学影像人工智能进展
人工智能·3d·音视频
XuanRanDev19 小时前
【音视频处理】FFmpeg for Windows 安装教程
windows·ffmpeg·音视频
小鹿撞出了脑震荡21 小时前
Effective Objective-C 2.0 读书笔记——关联对象
开发语言·ios·objective-c
小鹿撞出了脑震荡21 小时前
Effective Objective-C 2.0 读书笔记—— objc_msgSend
ios·objective-c·xcode
fareast_mzh21 小时前
Customize ringtone on your iPhone
ios·iphone
程序猿玖月柒1 天前
常见的多媒体框架(FFmpeg GStreamer DirectShow AVFoundation OpenMax)
ffmpeg·音视频·gstreamer·openmax·directshow·avfoundation
源代码杀手1 天前
【以音频软件FFmpeg为例】通过Python脚本将软件路径添加到Windows系统环境变量中的实现与原理分析
windows·python·音视频