本文共 6916 字,大约阅读时间需要 23 分钟。
1.上层API
对于仅仅想直接播放个音频或视频的需求,没有必要使用底层的API,这些API可以简单的直接调用一些类似start,stop,设置一些资源文件路径就可以做录制与播放.
视频播放:AVPlayer
视频录制:UIImagePickerController 音频播放:AVAudioRecorder 音频录制:AVAudioPlayer 以上API如需了解使用自行百度,简单易用.2.中层API
如果想采集到视频帧,音频帧加以利用,需要使用一些中层的API,对于采集而言,视频最底层即AVCaptureSession,我们可以通过回调函数拿到指定格式的音频视频.录制基本也同理.但对音频而言,如果不对音频有太多具体设置,仅仅做同步,可以直接使用AVCaputureSession中的音频采集.
音视频采集: AVCaptureSession
原理篇: 掘金, 简书,博客 实战篇: 掘金,简书,博客 音视频录制: AVAssetWriter,AVCaptureMovieFileOutput - 掘金, 简书,博客 3.底层,功能最多的API音频采集方面还可以使用更底层,更好用的Audio Queue, Audio Unit.
音频采集/播放: Audio Queue, Audio Unit
音频录制: Audio File -简书,掘金,博客 Audio Queue与Audio Unit的选择Audio Queue: 以队列的形式高效采集音频数据,可以直接设置采集PCM,AAC等压缩,未压缩类型格式音频数据.
原理篇:掘金,简书,博客 实战篇:简书,博客, 掘金 Audio Unit: 比起Audio Queue, 提供更加专业音频处理,支持混合,均衡,格式转换和实时输入/输出,用于录制,播放,离线渲染和实时对话,例如VoIP(互联网协议语音).可以从iOS应用程序动态加载和使用它. 原理篇:掘金,简书,博客 实战篇:掘金,简书,博客AVAudioRecorder类是专门处理录音操作,可以在你的应用程序提供音频记录功能。录音只能保存到本地文件。它包含在AVFoundation FrameWork库中。
在创建录音机时除了指定路径外还必须指定录音设置信息,因为录音机必须知道录音文件的格式、采样率、通道数等信息。//// ViewController.m// 录音//// Created by on 2020/2/25.// Copyright © 2020 Shae. All rights reserved.//#import "ViewController.h"#import@interface ViewController ()@property (nonatomic,strong)NSURL *url;@property (nonatomic,strong)AVAudioRecorder *recorder;@end@implementation ViewController- (NSURL *)url{ if (_url==nil) { NSString *tmpDir=NSTemporaryDirectory();//获取沙盒的TemporaryDirectory路径 NSString *urlPath=[tmpDir stringByAppendingString:@"record.caf"]; _url=[NSURL fileURLWithPath:urlPath]; NSLog(@"_url:%@",_url); } return _url;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (IBAction)startRecord:(UIButton *)sender { NSLog(@"开始录音"); NSError *error=nil;// //1.激活AVAudioSession AVAudioSession *session=[AVAudioSession sharedInstance];//单例类 [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; if (session !=nil) { [session setActive:YES error:nil]; }else{ NSLog(@"session error :%@",error); } //2.设置AVAudioSession类的属性 NSDictionary *recoderSettings=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithFloat:16000.0],AVSampleRateKey,[NSNumber numberWithInt:kAudioFormatAppleIMA4],AVFormatIDKey,[NSNumber numberWithInt:1],AVNumberOfChannelsKey,[NSNumber numberWithInt:AVAudioQualityMax],AVEncoderAudioQualityKey, nil];//采样率,格式,Channel,采样质量 //3.初始化recod对象 self.recorder=[[AVAudioRecorder alloc]initWithURL:self.url settings:recoderSettings error:nil]; //4.开始录音 [self.recorder record];}- (IBAction)endRecord:(UIButton *)sender { NSLog(@"结束录音"); [self.recorder stop]; self.recorder=nil;//停止录音需要将现有AVAudioRecorder释放掉,不然再次录音会录到之前的文件里}@end
缺点:AVAudioPlayer不支持加载网络媒体流,只能播放本地文件
优点:能够精确控制播放进度、音量、播放速度等属性 (1) 初始化AVAudioPlayer对象,此时通常指定本地文件路径 (2) 设置播放器属性,例如重复次数、音量大小等 (3) 调用play方法播放//// ViewController.m// 录音//// Created by 谢鑫 on 2020/2/25.// Copyright © 2020 Shae. All rights reserved.//#import "ViewController.h"#import@interface ViewController () @property (nonatomic,strong)NSURL *url;@property (nonatomic,strong)AVAudioRecorder *recorder;@property (nonatomic,strong)AVAudioPlayer *player;@end@implementation ViewController- (NSURL *)url{ if (_url==nil) { NSString *tmpDir=NSTemporaryDirectory();//获取沙盒的TemporaryDirectory路径 NSString *urlPath=[tmpDir stringByAppendingString:@"record.caf"]; _url=[NSURL fileURLWithPath:urlPath]; NSLog(@"_url:%@",_url); } return _url;}- (AVAudioPlayer *)player{ if (_player==nil) { _player=[[AVAudioPlayer alloc] initWithContentsOfURL:self.url error:nil]; _player.volume=1.0; _player.delegate=self; } return _player;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (IBAction)startRecord:(UIButton *)sender { NSLog(@"开始录音"); NSError *error=nil;// //1.激活AVAudioSession AVAudioSession *session=[AVAudioSession sharedInstance];//单例类 [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; if (session !=nil) { [session setActive:YES error:nil]; }else{ NSLog(@"session error :%@",error); } //2.设置AVAudioSession类的属性 NSDictionary *recoderSettings=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithFloat:16000.0],AVSampleRateKey,[NSNumber numberWithInt:kAudioFormatAppleIMA4],AVFormatIDKey,[NSNumber numberWithInt:1],AVNumberOfChannelsKey,[NSNumber numberWithInt:AVAudioQualityMax],AVEncoderAudioQualityKey, nil];//采样率,格式,Channel,采样质量 //3.初始化recod对象 self.recorder=[[AVAudioRecorder alloc]initWithURL:self.url settings:recoderSettings error:nil]; //4.开始录音 [self.recorder record];}- (IBAction)endRecord:(UIButton *)sender { NSLog(@"结束录音"); [self.recorder stop]; self.recorder=nil;//停止录音需要将现有AVAudioRecorder释放掉,不然再次录音会录到之前的文件里}- (IBAction)playRecord:(UIButton *)sender { [self.player play];}#pragma AVAudioPlayer的代理方法- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ NSLog(@"%s",__func__); self.player=nil;}@end
如果自定义播放器可参考:https://blog.csdn.net/qq_18505715/article/details/79868677
微信录音是长按录音,所以要用到长按手势。
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToRecord:)]; [_longPressBtn addGestureRecognizer:longPress];
# pragma mark --自定义方法-(void)longPressToRecord:(UILongPressGestureRecognizer *)gesture{ if(gesture.state==UIGestureRecognizerStateBegan){ //开始录音 [self startRecord]; }else{ //结束录音 [self endRecord]; } }
NSSlider控制音量
//// ViewController.m// 播放音频//// Created by 谢鑫 on 2020/2/25.// Copyright © 2020 Shae. All rights reserved.//#import "ViewController.h"#import@interface ViewController ()@property (nonatomic,strong)AVAudioPlayer *player;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (AVAudioPlayer *)player{ NSString *urlString=[[NSBundle mainBundle]pathForResource:@"bg" ofType:@"mp3"]; NSURL *url=[NSURL fileURLWithPath:urlString]; if (_player==nil) { _player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; } return _player;}- (IBAction)play:(UIButton *)sender { [self.player play];}- (IBAction)pause:(UIButton *)sender { [self.player pause];}- (IBAction)stop:(UIButton *)sender { [self.player stop]; self.player=nil;}- (IBAction)valueChange:(UISlider *)sender { self.player.volume=sender.value;}@end