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];
}