command + s 截模拟器的屏幕
MP.4只是一个容器 H.264 H.263是真正的格式 H.265正在测试中
avi H.264 H.263
视频解码:将H.263 H.264转换为视频和音频的格式
视频编码:
解码:硬解码:GPU做的解码 CPU做除了图片以外的,其他的处理 GPU(解码的效率特别快)显卡上带的 和图片有关
软解码:CPU做的解码(前提是GPU不能做相应的解码时)
硬解码比软解码强一点
一 、AVPlayer
播放视频 可以播放流媒体 导入两个库 CoreMedia AVFoundation
单独使用AVPlayer类是无法显示视频的,要将视频层添加值AVPlayerLayer中,这样才能将视频显示出来
NSURL * url = [NSURL fileURLWithPath:path];不是规范的path(NSString)时,使用该方法初始化NSURL
CMTimeGetSeconds(currentTime)转为秒
//AVAudioPlayer->url
//AVPlayer-?AVPlayerItem->AVURLAsset->NSURL
NSString * path = [[NSBundle mainBundle]pathForResource:@"敢死队3" ofType:@"mp4"];
NSURL * url = [NSURL fileURLWithPath:path];
AVURLAsset *asset = [AVURLAsset assetWithURL:url];
//1、异步 检测
[asset loadValuesAsynchronouslyForKeys:@[@"tracks"] completionHandler:^{
//AVPlayerItem
AVPlayerItem * item = [AVPlayerItem playerItemWithAsset:asset];
_player = [[AVPlayer alloc]initWithPlayerItem:item];
//关联屏幕和播放器
[_playerView setPlayer:_player];
//刷新进度
//这个方法就是加一个监听 每隔这么长时间会再者个线程里面调用这个方法 CMTimeMake(<#int64_t value#>, <#int32_t timescale#>) 帧 帧/每秒
__weak AVPlayer * player = _player;
__weak UISlider * slider = _slider;
[ _player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
//当前时间 警告的原因是相互强引用
CMTime currentTime = player.currentItem.currentTime;
//总时间
CMTime duration = player.currentItem.duration;
//CMTimeGetSeconds 转为秒
float pro = CMTimeGetSeconds(currentTime)/CMTimeGetSeconds(duration);
[slider setValue:pro animated:YES];
}];
}];
}
//设置进度
-(void)setProgress
{
CMTime duration = _player.currentItem.duration;//总时间
CMTime currentTime = CMTimeMultiplyByFloat64(duration, _slider.value);
//从当前时间播放
[_player seekToTime:currentTime];
}
-(void)play
{
[_player play];
}
-(void)pause
{
[_player pause];
}
二、MPMoviePlayerController 视频播放器 不能播放流媒体
需要导入MediaPlayer库
MPMoviePlayerViewController * _playerController;//不能播放流媒体 流媒体可以想一想直播
- (IBAction)buttonClick:(UIButton *)sender {
NSString * path = [[NSBundle mainBundle]pathForResource:@"敢死队3" ofType:@"mp4"];
NSURL * url = [NSURL fileURLWithPath:path];
_playerController = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:_playerController];
}
三、AVAudioPlayer 音频播放器 引入AVFoundition
- (void)viewDidLoad {
[super viewDidLoad];
NSString* path = [[NSBundle mainBundle] pathForResource:@"Beat It" ofType:@"mp3"];
NSURL* url = [NSURL fileURLWithPath:path];
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
//准备播放
[_player prepareToPlay];
//变速
[_player setEnableRate:YES];
//峰值和平均值监测
[_player setMeteringEnabled:YES];
}
//刷新
- (void)refresh{
//进度 = 当前时间 / 总时间
[_proSlider setValue:_player.currentTime/_player.duration animated:YES];
//刷新峰值平均值
[_player updateMeters];
//平均值
float avg = [_player averagePowerForChannel:0];
//峰值
float peak = [_player peakPowerForChannel:0];
//NSLog(@"%f, %f", avg, peak);
[_pv1 setProgress: (avg + 30) / 30 animated:YES];
[_pv2 setProgress: (peak + 30) / 30 animated:YES];
}
//播放
- (void)play:(id)sender{
[_player play];
if (_timer == nil) {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(refresh) userInfo:nil repeats:YES];
}
}
//暂停
- (void)pause:(id)sender{
[_player pause];
if (_timer) {
[_timer invalidate];
_timer = nil;
}
}
//设置进度
- (IBAction)setProgress:(id)sender{
//当前时间 = 总时间 * 百分比
UISlider* slider = sender;
_player.currentTime = _player.duration * slider.value;
}
//设置音量
- (IBAction)setVolume:(id)sender{
UISlider* slider = sender;
_player.volume = slider.value;
}
//设置声道
- (IBAction)setPan:(id)sender{
UISlider* slider = sender;
_player.pan = slider.value;
}
//设置速度
- (IBAction)setRate:(id)sender{
UISlider* slider = sender;
_player.rate = slider.value;
}
四、AVAUdioRecorder 录音 引入AVFoundition系统库
//录音设置
NSMutableDictionary * recordSetting = [[NSMutableDictionary alloc]init];
//设置录音格式 AVFormatIDKey == kAudioFormatLinearPCM
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//设置录音采用率(Hz)如:AVSampleRateKey == 8000/44100/96000(影响音频质量)
[recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
//录音通道数 1 或 2
[recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
//线性采样位数 8.16。24.32
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//录音质量
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
_recorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:nil];
[_recorder prepareToRecord];
-(void)startRecord
{
[_button setTitle:@"录音中" forState:UIControlStateNormal];
[_recorder record];
}
-(void)stopRecord
{
[_button setTitle:@"录音" forState:UIControlStateNormal];
[_recorder stop];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Document/a.aac"];
NSURL * url = [NSURL fileURLWithPath:path];
NSLog(@"录音路径%@",path);
_player = [[AVAudioPlayer alloc]initWithContentsOfURL:url
error:nil];
[_player prepareToPlay];
[_player play];
}