录像、录音和拍照

    现在我们使用的手机,无论是苹果还是安卓,都能够录音、拍照和录像,而且感觉这是在正常不过的了,不仅如此,现在的手机还能够修音,修图在这里就不介绍了,这里主要介绍如何实现录音、拍照和录像的功能。

一、录音

     录音的介绍:使用的框架和音乐播放器一样使用的AVFoundation

     录音使用到的类

   (1)AVAudioRecord(输入)

   (2)相关的设置属性

 <1>AVNumberOfChannelsKey:通道数

 <2>AVSampleRateKey:采样率 44100

 <3>AVLinearPCMBitDepthKey:比特率 设置成16 32

 <4>AVEncoderAudioQualityKey:质量

 <5>AVEncoderBitRateKey:比特采样率 128000

  录音的步骤

  1、初始化了录音对象

  2、开始录音

  3、停止录音


具体代码

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioRecorderDelegate>

{

    AVAudioRecorder *audionRecorder;

}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(100100100100);

    [button setTitle:@"TICK" forState:UIControlStateNormal];

    button.backgroundColor = [UIColor brownColor];

    [button addTarget:self action:@selector(startAudioRecorder:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

}


- (void)startAudioRecorder:(UIButton *)sender{

    sender.selected = !sender.selected;

    if (sender.selected != YES) {

        [audionRecorder stop];//停止录音

    }    

//    URL是本地的URLaudioRecorder

    NSString *name = [NSString stringWithFormat:@"%d.aiff",(int)[NSDatedate].timeIntervalSince1970];

    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectoryNSUserDomainMaskYESlastObjectstringByAppendingPathComponent:name];

  //   1、初始化录音机

    NSError *error;

    audionRecorder = [[AVAudioRecorder allocinitWithURL:[NSURL URLWithString:path] settings:@{AVNumberOfChannelsKey:@2,AVSampleRateKey:@44100,AVLinearPCMBitDepthKey:@32,AVEncoderAudioQualityKey:@(AVAudioQualityMax),AVEncoderBitRateKey:@128000} error:&error];

/*

 AVNumberOfChannelsKey:通道数,通常为双声道 2

 AVSampleRateKey:采样率 HZ 通常设置为44100 44.1KHZ

 AVLinearPCMBitDepthKey:比特率,通常为8 16 24 32

 AVEncoderAudioQualityKey:声音质量,参数是一个枚举

 AVAudioQualityMin    = 0,最小的质量

  AVAudioQualityLow    = 0x20,比较低的质量

 AVAudioQualityMedium = 0x40,中间的质量

 AVAudioQualityHigh   = 0x60,高的质量

 AVAudioQualityMax    = 0x7F最好的质量

 AVEncoderBitRateKey:音频编码的比特率 单位BPS(传输速率)128000bps

 */

    [audionRecorder prepareToRecord];//欲录音

    [audionRecorder record];//开始录音

    audionRecorder.delegate = self;

    NSLog(@"%@",path);

}

//录音结束的时候调用

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{

//    NSLog(@"录音结束");

    

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectoryNSUserDomainMaskYES)lastObject];

//    文件操作的类

    NSFileManager *fileManager = [NSFileManager defaultManager];

//    获得当前文件的所有子文件subpathsAtPath

    NSArray *pathList = [fileManager subpathsAtPath:path];

//    NSLog(@"%@",pathList);

//    NSString *path1 = pathList[2];

//    NSLog(@"%@",path1.pathExtension);

    

//    需要只获得录音文件

    NSMutableArray *audioPathList = [NSMutableArray array];

//    遍历所有这个文件夹下面的子文件

    for (NSString *audioPath in pathList) {

//        通过文件的扩展名(尾缀)来区分是不是录音文件

        if ([audioPath.pathExtension isEqualToString:@"aiff"]) {

//            把筛选出来的文件放到数组,得到所有的音频wenjian

            [audioPathList addObject:audioPath];

       }

    }

      NSLog(@"%@",audioPathList);

}


录像和拍照的使用很相似,在这里就将它们两个一块讲了
录像、拍照
录像和拍照使用的框架是UIKit框架,使用的类是:UIImagePickerController

介绍一些属性和方法

区分选择摄像头还是相册:sourceType

 UIImagePickerControllerSourceTypePhotoLibrary,

 保存的相册

 UIImagePickerControllerSourceTypeCamera,摄像头

 UIImagePickerControllerSourceTypeSavedPhotosAlbum相册

 

 区分录像和拍照cameraCaptureMode

  拍照:UIImagePickerControllerCameraCaptureModePhoto

  录像:UIImagePickerControllerCameraCaptureModeVideo

 

 设置视频清晰度

 UIImagePickerControllerQualityTypeHigh高清

 UIImagePickerControllerQualityTypeMedium普通

 UIImagePickerControllerQualityTypeLow

 UIImagePickerControllerQualityType 640x480

 UIImagePickerControllerQualityTypeIFrame 1280x720

 UIImagePickerControllerQualityTypeIFrame 960x540

 

 设置视频最大的持续时间

 videoMaximumDuration 默认最大十分钟

 

 

 区分前后摄像头:cameraDevice

 前置:UIImagePickerControllerCameraDeviceFront

 后置:UIImagePickerControllerCameraDeviceRear

 

 是否显示控制控件:showsCameraControls

 默认YES显示控制控件

 

 设置拍照:takePicture

 

 录像:startVideoCapture

 停止:stopVideoCapture

 

 闪光模式:cameraFlashMode,默认自动

 UIImagePickerControllerCameraFlashModeOff  = -1,关闭

 UIImagePickerControllerCameraFlashModeAuto = 0,自动

 UIImagePickerControllerCameraFlashModeOn   = 1

 开启

 

 设置调用摄像头视图页面 覆盖视图

 cameraOverlayView

 

 设置拍照页面的形态 camereViewTransform

 

 代理:需要导入两个代理

 @property(nullable,nonatomic,weak)      id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;

 

 代理方法:不区分录像和拍照

采集完成之后调用 不区分拍照和录像

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;

 采集取消的时候调用

 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;



具体代码

#import "ViewController.h"

#import <AVKit/AVKit.h>

#import <AVFoundation/AVFoundation.h>

#import <MobileCoreServices/MobileCoreServices.h>


#define SCREEN_BOUNDS [UIScreen mainScreen].bounds//屏幕尺寸

#define SCREEN_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)//屏幕宽

#define SCREEN_HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds)//屏幕高


@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

{

    UILabel *lable;//显示拍照或者录像的控件

    BOOL isMovie;//判断摄像的依据

    

    UIImageView *imageView;//显示拍照录像结果的视图

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(100100100100);

    [button setTitle:@"Camera" forState:UIControlStateNormal];

    button.backgroundColor = [UIColor brownColor];

    [button addTarget:self action:@selector(doit) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

    UISwitch *medioSwitch = [[UISwitch allocinitWithFrame:CGRectMake(SCREEN_WIDTH-100505030)];

    [medioSwitch addTarget:self action:@selector(changeMedio:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:medioSwitch];

    

    lable = [[UILabel allocinitWithFrame:CGRectMake(CGRectGetMinX(medioSwitch.frame), CGRectGetMinY(medioSwitch.frame)-305050)];

    lable.textAlignment = NSTextAlignmentCenter;

    lable.textColor = [UIColor purpleColor];

    lable.text = @"拍照";

    [self.view addSubview:lable];

    

    imageView = [[UIImageView allocinitWithFrame:CGRectMake(100150200300)];

    [self.view addSubview:imageView];

}


- (void)changeMedio:(UISwitch *)sender{


    lable.text = sender.isOn != YES ? @"拍照":@"录像";


    isMovie = sender.isOn;


}


- (void)doit{

    //    创建录像、拍照对象

    UIImagePickerController *pickImageController = [[UIImagePickerController allocinit];

    if (isMovie == YES) {

        

 //    选择摄像头设备

//     默认选择相册

    pickImageController.sourceType = UIImagePickerControllerSourceTypeCamera;

    

//    设置选择录像

//    默认拍照 选择cameraCaptureMode录像,一定要选择媒体的类型 不然会崩溃

    

//    选择媒体的类型

//    拍照的时候不选择媒体类型,不会崩溃,因为默认设置是kUTTypeImage

//    kUTTypeImage包含在MobileCoreServices框架中

//    MobileCoreServices需要的内容不是OC中字符串的类型,需要强转

    

//    mediaTypes录制视频,类型要选择movie,因为movie里面包含了audiovideo

    pickImageController.mediaTypes = @[(NSString *)kUTTypeMovie];

    

    pickImageController.cameraCaptureMode =  UIImagePickerControllerCameraCaptureModeVideo;

    

     }

//    delegate里面包含两个协议

//    UINavigationBarDelegate

//    UIImagePickerControllerDelegate

    pickImageController.delegate = self;

    

    [self presentViewController:pickImageController animated:YES completion:nil];


}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{


    NSLog(@"完成");

    

    NSLog(@">>>>%@",info);

   

    

    if ([info [UIImagePickerControllerMediaTypeisEqualToString:(NSString *)kUTTypeMovie]) {

        NSLog(@"录像。。。。。");

        

        NSLog(@"******%@",[info[UIImagePickerControllerMediaURLpath]);

        

//        URL转换成path

        NSString *path = (NSString *)[info[UIImagePickerControllerMediaURLpath];

//        UIImagePickerControllerMediaURL

//        保存视频到URL

        UISaveVideoAtPathToSavedPhotosAlbum(path, self@selector(video:didFinishSavingWithError:contextInfo:), nil);

    }

    

    if ([info [UIImagePickerControllerMediaType ] isEqualToString:(NSString *)kUTTypeImage]) {

        NSLog(@"拍照了。。。。。");

        

//        UIImagePickerControllerOriginalImage

        

//        NSLog(@"%@",info [UIImagePickerControllerOriginalImage]);

        

        UIImage *finishImage = info[UIImagePickerControllerOriginalImage];

        

        imageView.image = nil;

        imageView.image = finishImage;

      

//        .jpeg的图片转换成二进制

     NSData *imageData = UIImageJPEGRepresentation(finishImage, 0.1);

//        .png的图片转换成二进制

    NSData *imageData1 = UIImagePNGRepresentation(finishImage);

        

//        通过这个方法将图片保存到相册

        UIImageWriteToSavedPhotosAlbum(finishImage, self@selector(image:didFinishSavingWithError:contextInfo:), nil);

    }

 [self dismissViewControllerAnimated:YES completion:nil];

}

//视频保存到相册之后调用

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void*)contextInfo{

    

    AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:videoPath]];

    

    AVPlayerViewController *palyerVC = [[AVPlayerViewController allocinit];

    

//    [self.view addSubview:palyerVC.view];

    palyerVC.player = player;

    

    [self presentViewController:palyerVC animated:YES completion:nil];

    

    

    [palyerVC.player play];

}

//图片保存到相册之后调用

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void*)contextInfo{


    NSLog(@"保存图片成功");


}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    NSLog(@"取消");

    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值