@import AVKit;

@import AVFoundation;

NSString *address = [[NSBundle mainBundle]pathForResource:@"show" ofType:@"mp4"];

NSURL *urlPath = [NSURL fileURLWithPath:address];

self.playerController = [[AVPlayerViewController alloc]init];

self.playerController.player = [AVPlayer playerWithURL:urlPath];

self.playerController.showsPlaybackControls = NO;     //不显示播放器控件

self.playerController.videoGravity = AVLayerVideoGravityResizeAspectFill;  //设置拉伸模式

self.playerController.view.frame = CGRectMake(0, 0, KWidth, KHeight);

[self.navigationController.view addSubview:self.playerController.view];

[self.playerController.player play];

@import AVFoundation;

@import MediaPlayer;

self.mPlayer = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL  URLWithString:@"http://192.168.42.1/DCIM/100MEDIA/FILE0001.mp4"]];

self.mPlayer.view.frame = CGRectMake(0, 150, self.view.frame.size.width, 220);

[self.view addSubview:self.mPlayer.view];

self.mPlayer.scalingMode = MPMovieScalingModeAspectFill;

[self.mPlayer play];

[self.mPlayer requestThumbnailImagesAtTimes:@[@1.2,@2.1] timeOption:MPMovieTimeOptionNearestKeyFrame];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(noti:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];

- (void)noti:(NSNotification*)noti{

static int a = 1;

if (a == 1) {

_img1.image = noti.userInfo[MPMoviePlayerThumbnailImageKey];

}else{

_img2.image = noti.userInfo[MPMoviePlayerThumbnailImageKey];

}

a ++;

}

NSURL *videoUrl = [NSURL URLWithString:@"http://192.168.42.1/DCIM/100MEDIA/FILE0140.mp4"];

self.playerItem = [AVPlayerItem playerItemWithURL:videoUrl];

[self.playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];// 监听status属性

[self.playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];// 监听loadedTimeRanges属性

self.player = [AVPlayer playerWithPlayerItem:self.playerItem];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.playerItem];

self.layer = [AVPlayerLayer playerLayerWithPlayer:self.player];

_layer.frame = self.view.layer.frame;

[self.view.layer addSublayer:_layer];

[self.player setVolume:0.8];

[self.player play];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

AVPlayerItem *playerItem = (AVPlayerItem *)object;

if ([keyPath isEqualToString:@"status"]) {

if ([playerItem status] == AVPlayerStatusReadyToPlay) {

NSLog(@"AVPlayerStatusReadyToPlay");

//  self.stateButton.enabled = YES;

CMTime duration = self.playerItem.duration;// 获取视频总长度

CGFloat totalSecond = playerItem.duration.value / playerItem.duration.timescale;// 转换成秒

// _totalTime = [self convertTime:totalSecond];// 转换成播放时间

//[self customVideoSlider:duration];// 自定义UISlider外观

NSLog(@"movie total duration:%f",CMTimeGetSeconds(duration));

[self monitoringPlayback:self.playerItem];// 监听播放状态

} else if ([playerItem status] == AVPlayerStatusFailed) {

NSLog(@"AVPlayerStatusFailed");

}

} else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {

NSTimeInterval timeInterval = [self availableDuration];// 计算缓冲进度

NSLog(@"Time Interval:%f",timeInterval);

CMTime duration = self.playerItem.duration;

CGFloat totalDuration = CMTimeGetSeconds(duration);

[self.gress setProgress:timeInterval/totalDuration animated:YES];

NSLog(@"%f",timeInterval / totalDuration);

}

}

- (NSTimeInterval)availableDuration {

NSArray *loadedTimeRanges = [[self.player currentItem] loadedTimeRanges];

CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];// 获取缓冲区域

float startSeconds = CMTimeGetSeconds(timeRange.start);

float durationSeconds = CMTimeGetSeconds(timeRange.duration);

NSTimeInterval result = startSeconds + durationSeconds;// 计算缓冲总进度

return result;

}

- (NSString *)convertTime:(CGFloat)second{

NSDate *d = [NSDate dateWithTimeIntervalSince1970:second];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

if (second/3600 >= 1) {

[formatter setDateFormat:@"HH:mm:ss"];

} else {

[formatter setDateFormat:@"mm:ss"];

}

NSString *showtimeNew = [formatter stringFromDate:d];

return showtimeNew;

}

- (void)monitoringPlayback:(AVPlayerItem *)playerItem {

[self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:NULL usingBlock:^(CMTime time) {

CGFloat currentSecond = playerItem.currentTime.value/playerItem.currentTime.timescale;// 计算当前在第几秒

[self updateVideoSlider:currentSecond];

NSString *timeString = [self convertTime:currentSecond];

//self.timeLabel.text = [NSString stringWithFormat:@"%@/%@",timeString,_totalTime];

NSLog(@"%f,%@",currentSecond,timeString);

}];

}

-(void)updateVideoSlider:(CGFloat)f{

CMTime duration = self.playerItem.duration;// 获取视频总长度

CGFloat totalSecond = self.playerItem.duration.value / self.playerItem.duration.timescale;

self.playerSlider.value = f / totalSecond;

}

-(void)moviePlayDidEnd:(NSNotification*)noti{

}

AVKit & MediaPlayer简写的更多相关文章

  1. cocoaPods 创建自己的依赖库

    1.先在github上创建一个仓库 和一般创建一样,就是证书一定要选,我选的是MIT,不要问我因为啥, 我也不知道, 哈哈 2.check到本地或者本地创建,反正最后都要上传到这个仓库,以个人习惯吧 ...

  2. Mediaplayer error (-19,0)

    Android MediaPlayer 发生 error (-19,0) 错误解决方法. 引起原因:由于多次实例化MediaPlayer.start() 进行播放操作引起的.由于没有及时释放内存资源导 ...

  3. 关于从Activity A跳转到Activity B ,其中Activity A中有一个VideoView,Activity B中有一个MediaPlayer。

    两个不同的视频的跳转, 前面我是在onStop()方法中销毁VideoView(因为MediaPlayer是全局共用的,而VideoView内包含MediaPlayer),但是每次进入Activity ...

  4. [LeetCode] Generalized Abbreviation 通用简写

    Write a function to generate the generalized abbreviations of a word. Example: Given word = "wo ...

  5. Android四大组件--MediaPlayer详解(转)

    一. MediaPlayer 状态机 介绍 Android MediaPlayer 状态即图例 : 1. Idle (闲置) 状态 和 End (结束) 状态 MediaPlayer 对象声明周期 : ...

  6. Android音视频之MediaPlayer音视频播放

    前言: 昨天总结了视频录制,今天来学习一下视频的播放,Android的视频播放主要采用MediaPlayer类. MediaPlayer介绍 MediaPlayer类可用于控制音频/视频文件或流的播放 ...

  7. Android音频开发之MediaRecorder/MediaPlayer

    前言: 上次我们介绍了实时采集音频数据以及播放,今天我们来认识一下直接录制文件的方式. 直接上代码:录制管理类 public class MediaRecorderManager { public s ...

  8. WPF语音播放MediaPlayer

    private MediaPlayer player; public MainWindow() { InitializeComponent(); player = new MediaPlayer(); ...

  9. CSS属性简写

    盒模型简写: 如果top.right.bottom.left的值相同,如下面代码:margin:10px 10px 10px 10px; 缩写为:margin:10px; 如果top和bottom值相 ...

随机推荐

  1. 【行业交流】2016 TiD质量竞争力大会——移动互联网测试到质量的转变之路

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5691233.html TiD质量大会在北京召开,有幸去参加 ...

  2. 如何真正抓住微信小程序的红利? 阿禅知乎live总结

    微信App定义 为满足用户某种开发需求.完全基于微信的消息或网页应用,入口是公众号,用户无需离开微信即可完成所有操作,所有需求都在公众号里被满足 微信App的优势 1. 顾客在哪里,就让顾客在哪里看到 ...

  3. 优化SQL Sever查询语句的几个要点

    1.不要在Where子句中的“=”左边进行函数.算术运算或其他表达式运算,否则系统将可能无法正确使用索引. 2.尽量不要在Where条件中使用函数,否则将不能利用索引. 3.在Where条件中使用明确 ...

  4. TCP_NODELAY

    启用TCP_NODELAY的情况下: 客户端程序C连接到服务器程序S: C仅接受数据,S仅发送数据 S循环调用send发送长度很小的数据包比如:10字节; 在C上用任务管理器查看到C的上行流量大约是下 ...

  5. XML中文本节点存储任意字符的方法

    XML xml是一种可扩展标签语言, 为众多浏览器支持解析, ajax更是利用xml来完成服务器和客户端之前的通信. xml基本元素为 <label>xxx</label>, ...

  6. leetcode 223

    题目描述: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is def ...

  7. grep 常用参数详解

    grep常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 刚刚和同事打完球,虽然自己输了~不过也蛮好玩的,好久没有玩过乒乓球啦,话说你喜欢玩乒乓球吗?好啦,话不多说说,让 ...

  8. C++之路进阶——codevs1319(玩具装箱)

    1319 玩具装箱  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description P教授要去看奥运,但是他舍不下他的玩具,于是 ...

  9. windows系统调用 semaphore信号量

    #include "iostream" #include "windows.h" #include "cstring" using name ...

  10. 转: Oracle Form 中commit 与do_key('commit_form')区别

    1.commit_form针对form上面的数据变动进行commit,对于代码中的类似update,insert语句也进行提交:如果form上面的数据变动和代码中的数据变动有冲突,最后以界面上的为准. ...