AVKit & MediaPlayer简写
@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简写的更多相关文章
- cocoaPods 创建自己的依赖库
1.先在github上创建一个仓库 和一般创建一样,就是证书一定要选,我选的是MIT,不要问我因为啥, 我也不知道, 哈哈 2.check到本地或者本地创建,反正最后都要上传到这个仓库,以个人习惯吧 ...
- Mediaplayer error (-19,0)
Android MediaPlayer 发生 error (-19,0) 错误解决方法. 引起原因:由于多次实例化MediaPlayer.start() 进行播放操作引起的.由于没有及时释放内存资源导 ...
- 关于从Activity A跳转到Activity B ,其中Activity A中有一个VideoView,Activity B中有一个MediaPlayer。
两个不同的视频的跳转, 前面我是在onStop()方法中销毁VideoView(因为MediaPlayer是全局共用的,而VideoView内包含MediaPlayer),但是每次进入Activity ...
- [LeetCode] Generalized Abbreviation 通用简写
Write a function to generate the generalized abbreviations of a word. Example: Given word = "wo ...
- Android四大组件--MediaPlayer详解(转)
一. MediaPlayer 状态机 介绍 Android MediaPlayer 状态即图例 : 1. Idle (闲置) 状态 和 End (结束) 状态 MediaPlayer 对象声明周期 : ...
- Android音视频之MediaPlayer音视频播放
前言: 昨天总结了视频录制,今天来学习一下视频的播放,Android的视频播放主要采用MediaPlayer类. MediaPlayer介绍 MediaPlayer类可用于控制音频/视频文件或流的播放 ...
- Android音频开发之MediaRecorder/MediaPlayer
前言: 上次我们介绍了实时采集音频数据以及播放,今天我们来认识一下直接录制文件的方式. 直接上代码:录制管理类 public class MediaRecorderManager { public s ...
- WPF语音播放MediaPlayer
private MediaPlayer player; public MainWindow() { InitializeComponent(); player = new MediaPlayer(); ...
- CSS属性简写
盒模型简写: 如果top.right.bottom.left的值相同,如下面代码:margin:10px 10px 10px 10px; 缩写为:margin:10px; 如果top和bottom值相 ...
随机推荐
- EntityFramework SQLite
安装完sqlite的nuget包后,还要设置App.config文件才能正常使用 1. 在<providers>节点添加一条提供器配置 <provider invariantNam ...
- WordPress 添加面包屑导航
所谓面包屑,就是类似这种:首页 > 公司简介 > 发展历史 展示网站树型结构,并让网站访问者随时知道自己所处的位置,方便返回上几级. 将下面的代码添加到主题的 functions.php ...
- Maintaining Your Signing Identities and Certificates 维护你的签名标识和证书
Code signing your app lets users trust that your app has been created by a source known to Apple and ...
- Java学习-046-日志抓取合并后排序问题解决方案之 --- log4j 二次定制,实现日志输出添加延时10ms
自3月25至今,已经好久没有写学习日志了,今天在写日志抓取合并的小方法,发现抓取后的日志并米有依据系统执行的日志顺序排序.日志抓取排列逻辑如下: 通过日志标识,从各个日志文件(例如 use.log,e ...
- csrf 跨站请求伪造
转自 http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html
- ios开发xcode中设置代码块
在开发中有很多重复的代码,很多开发者把常用的代码做成代码块提高开发效率. 在xcode里选中代码块的时候总是很不容易,点击选中的代码(文字),不要移动和松开鼠标左键,当竖线变成像拉长了的x(我也不知道 ...
- asp.net如何在前台利用jquery Ajax调用后台方法
一 :最近因为帮同事开发项目使用到了asp.net,而我又想实现Ajax异步请求....从网上查询了一下资料之后,原来在asp.net中利用Ajax调用后台方法同样很简单,为了便于自己以后查看,特将此 ...
- windows 文件权限导致的 git 问题
windows 文件权限导致的 git 问题 在 windows 上使用 git 时,会遇到明明什么都没有改动,但是 git status 显示一堆文件被修改.这时,通过 git diff 可看到什么 ...
- Java 并发性和多线程
一.介绍 在过去单 CPU 时代,单任务在一个时间点只能执行单一程序.之后发展到多任务阶段,计算机能在同一时间点并行执行多任务或多进程.虽然并不是真正意义上的“同一时间点”,而是多个任务或进程共享一个 ...
- ThinkPHP讲解(十)——第三方类的引入:以分页为主
第三方类的引入,以分页类为例: 1.在控制器里新建一个分页的操作方法FenYe() 注意:第三方类Page.class.php放在Think或Home文件夹下,并新近一个文件夹,放在里面,并在其类里加 ...