当前位置:   article > 正文

iOS 利用AVPlayer创建视频播放器_xcode 基于avplayer开发视频浏览器

xcode 基于avplayer开发视频浏览器


目录

  1. 导入框架
    导入

  1. NSString *str = @"http://vmovier.qiniudn.com/559b918dbf717.mp4";
  2. NSString *urlStr =[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//链接接口中的汉字会导致请求失败
  3. NSURL *url=[NSURL URLWithString:urlStr];
  4. AVPlayerItem *playerItem=[AVPlayerItem playerItemWithURL:url];//主要创建方法,创建媒体资源管理对象
  5. AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];//根据媒体资源管理对象创建AVPlayer对象
  6. AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];//AVPlayer要显示视频,必须创建一个播放器层AVPlayerLayer用于展示
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

(2)视频的播放、暂停功能。
这也是最基本的功能,AVPlayer对应着两个方法play、pause来实现。
[player play];//播放
[player pause];//暂停
通常我们会需要得知当前的播放状态来进行主动地改变播放状态,问题的关键是如何判断当前视频是否在播放。很不幸的是AVPlayer没有这样的状态属性,但是很幸运的是我们可以通过判断播放器的播放速度来获得播放状态。AVPlayer类包含一个极其重要的属性:

**@property (nonatomic) float rate;**
  
  
  • 1
  • 1

其代码注释为:/* indicates the current rate of playback; 0.0 means “stopped”, 1.0 means “play at the natural rate of the current item” */
解释为:”表示当前的播放速度;0表示“停止”,1表示“在当前的PlayerItem下以正常速率播放”。
通过监控这个属性,可以间接获取当前播放状态,获取这个属性值对于视频播放器的功能完善具有不可代替的作用。

(3)播放进度及缓存进度
MPMoviePlayerController拥有自己的进度条,AVPlayer想要要展示播放进度就没有那么简单了。视频播放器通常是使用通知来获得播放器的当前状态,媒体加载状态等,但是无论是AVPlayer还是AVPlayerItem(AVPlayer有一个属性currentItem是AVPlayerItem类型,表示当前播放的视频对象)都无法获得这些信息。当然AVPlayerItem是有通知的,但是对于获得播放状态和加载状态有用的通知只有一个:播放完成通知AVPlayerItemDidPlayToEndTimeNotification。
在播放视频时,特别是播放网络视频往往需要知道视频加载情况、缓冲情况、播放情况,这些信息可以通过KVO监控AVPlayerItem的status、loadedTimeRanges属性来获得。当AVPlayerItem的status属性为AVPlayerStatusReadyToPlay是说明正在播放,只有处于这个状态时才能获得视频时长等信息;当loadedTimeRanges的改变时(每缓冲一部分数据就会更新此属性)可以获得本次缓冲加载的视频范围(包含起始时间、本次加载时长),这样一来就可以实时获得缓冲情况。然后就是依靠AVPlayer的

- (id)addPeriodicTimeObserverForInterval:(CMTime)interval queue:(dispatch_queue_t)queue usingBlock:(void (^)(CMTime time))block
  
  
  • 1
  • 1

方法获得播放进度,这个方法会在设定的时间间隔内定时更新播放进度,通过time参数通知客户端。有了这些视频信息,播放进度就不成问题了,事实上通过这些信息就算是平时看到的其他播放器的缓冲进度显示以及拖动播放的功能也可以顺利的实现。

  1. 具体实现
    具体实现的效果图:
    exp_1
    exp_2

具体代码:
(注:自知自己水平有限,代码优化处理方面做的也不是太好,但是幸好实现效果勉强可以,请各位读者勿喷就好)
(1)ViewController里的调用情况:

ViewController.h文件

  1. #import <UIKit/UIKit.h>
  2. #import <AVFoundation/AVFoundation.h>
  3. #import "AVPlayer_S.h"
  4. @interface ViewController : UIViewController
  5. **//AVPlayer_S 为我自己创建的类,继承于UIView,用于承载AVPlayer**
  6. @property (nonatomic, retain) AVPlayer_S *myVideoPlayer;
  7. @end
  8. ViewController.m文件
  9. #import "ViewController.h"
  10. @interface ViewController ()
  11. @end
  12. @implementation ViewController
  13. - (void)viewDidLoad {
  14. [super viewDidLoad];
  15. self.view.backgroundColor = [UIColor whiteColor];
  16. //AVPlayer_S 为我自己创建的类,继承于UIView,用于承载AVPlayer,此处用了固定的frame,自己在使用时可以根据需要设定具体的frame
  17. self.myVideoPlayer = [[AVPlayer_S alloc] initWithFrame:CGRectMake(20, 60, 335, 280) videoURL:@"http://vmovier.qiniudn.com/559b918dbf717.mp4"];
  18. [self.view addSubview:self.myVideoPlayer];
  19. }
  20. - (void)didReceiveMemoryWarning {
  21. [super didReceiveMemoryWarning];
  22. // Dispose of any resources that can be recreated.
  23. }
  24. @end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

AVPlayer_S.h文件

  1. #import <UIKit/UIKit.h>
  2. #import <AVFoundation/AVFoundation.h>
  3. #import "MBProgressHUD.h"//为视频添加缓冲时候的菊花旋转,此第三方可以再Github上下载,直接搜索MBProgressHUD即可下载使用,假如你不想用可以把与之相关的全部删除
  4. //附加MBProgressHUD的下载地址:https://github.com/jdg/MBProgressHUD
  5. @interface AVPlayer_S : UIView<MBProgressHUDDelegate>
  6. @property (nonatomic, retain) AVPlayer *player;//AVPlayer对象
  7. @property (nonatomic, retain) AVPlayerLayer *playerLayer;//视频播放器的layer层视图,若不明白为何创建,请看目录2.AVPlayer类介绍
  8. @property (nonatomic, retain) NSString *video_url;//接收视频连接
  9. @property (nonatomic, assign) CMTime totalTime;//记录视频总时长
  10. @property (nonatomic, assign) BOOL hidenBar;//视频UI控件是否显示的标识
  11. @property (nonatomic, assign) BOOL didPlay;//缓冲结束立即播放的标识
  12. @property (nonatomic, assign) BOOL isFullScreen;//全屏状态标识
  13. @property (nonatomic, assign) CGRect originalFrame;//记录播放器原始frame
  14. @property (nonatomic, retain) UIImageView *backIamge;//背景图
  15. @property (nonatomic, retain) UILabel *didTimeLabel;//显示已完成的播放时长
  16. @property (nonatomic, retain) UILabel *totalTimeLabel;//显示视频的总播放时长
  17. @property (nonatomic, retain) UIButton *playButton;//播放暂停按钮
  18. @property (nonatomic, retain) UIProgressView *playProgress;//缓冲条
  19. @property (nonatomic, retain) UISlider *playSlider;//播放进度条
  20. @property (nonatomic, retain) UIButton *fullScreenButton;
  21. @property (nonatomic, retain) UIView *barView;//承载上述控件的UIView
  22. @property (nonatomic, retain) UIView *superView;//记录下全屏前的父视图,便于退出全屏后视频处在正确的位置
  23. - (instancetype)initWithFrame:(CGRect)frame videoURL:(NSString *)url;//自定义初始化方法,只需要给定frame和视频连接即可返回一个具有该frame的承载视频播放器的UIView
  24. - (void)removeNotification;//移除通知
  25. - (void)removeObserverFromPlayerItem:(AVPlayerItem *)playerItem;//移除playerItem的观察者
  26. @end
  27. AVPlayer_S.m文件
  28. #import "AVPlayer_S.h"
  29. @implementation AVPlayer_S
  30. //自定义初始化方法
  31. - (instancetype)initWithFrame:(CGRect)frame videoURL:(NSString *)url
  32. {
  33. self = [super initWithFrame:frame];
  34. if (self) {
  35. self.didPlay = NO;
  36. self.originalFrame = frame;
  37. self.hidenBar = NO;
  38. self.isFullScreen = NO;
  39. self.video_url = url;
  40. self.backgroundColor = [UIColor blackColor];
  41. [self creatView:frame];//创建UI视图
  42. [self createPlayer:self.originalFrame];//创建layer层,用于播放视频
  43. [MBProgressHUD showHUDAddedTo:self animated:YES];//添加小菊花,若不想用就删除与之相关的东西
  44. }
  45. return self;
  46. }
  47. - (void)createPlayer:(CGRect)frame {
  48. NSString *urlStr =[self.video_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  49. NSURL *url=[NSURL URLWithString:urlStr];
  50. //开辟线程执行AVPlayerItem的创建,若不开辟线程则会占用主线程,造成视图加载完成后暂时无法对页面进行操作的卡顿现象
  51. dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
  52. dispatch_async(globalQueue, ^{
  53. //创建AVPlayerItem
  54. AVPlayerItem *playerItem=[AVPlayerItem playerItemWithURL:url];
  55. dispatch_async(dispatch_get_main_queue(), ^{
  56. if (playerItem) {
  57. if (!_player) {
  58. _player = [AVPlayer playerWithPlayerItem:playerItem];
  59. if (_player) {
  60. //保证AVPlayer对象存在,然后再为其添加进度观察,以便获取当前播放进度
  61. [self addProgressObserver];
  62. //给AVPlayerItem添加监控(最重要的监控)
  63. [self addObserverToPlayerItem:playerItem];
  64. }
  65. }
  66. } else {
  67. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无法连接到视频资源" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  68. [alert show];
  69. [self removeFromSuperview];
  70. }
  71. });
  72. });
  73. //创建播放层(layer层)
  74. self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
  75. self.playerLayer.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
  76. [self.layer addSublayer:self.playerLayer];
  77. }
  78. #pragma mark - 通知
  79. //添加通知
  80. - (void)addNotification {
  81. //添加 播放结束 通知
  82. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
  83. }
  84. //移除所有通知
  85. - (void)removeNotification {
  86. [[NSNotificationCenter defaultCenter] removeObserver:self];
  87. }
  88. // 播放完成通知执行的方法:移除播放视图及小菊花假如想执行此操作可以解除注释
  89. - (void)playbackFinished:(NSNotification *)notification {
  90. /*
  91. [MBProgressHUD hideHUDForView:self animated:YES];
  92. [self removeFromSuperview];
  93. */
  94. }
  95. #pragma mark - 监控
  96. // 给播放器添加进度更新
  97. - (void)addProgressObserver {
  98. UISlider *slider = self.playSlider;//获取当前滑动条
  99. __block AVPlayer_S *av_s = self;
  100. //这里设置每秒执行一次
  101. [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 10.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
  102. float current = CMTimeGetSeconds(time);//获取当前的播放时间
  103. if (current) {
  104. [slider setValue:current animated:YES];//实时设置滑动条当前值
  105. int currentTime = (int)current;
  106. int m = currentTime / 60;
  107. int s = currentTime % 60;
  108. NSString *strM = nil;
  109. NSString *strS = nil;
  110. if (m < 10) {
  111. strM = [NSString stringWithFormat:@"0%d", m];
  112. } else {
  113. strM = [NSString stringWithFormat:@"%d", m];
  114. }
  115. if (s < 10) {
  116. strS = [NSString stringWithFormat:@"0%d", s];
  117. } else {
  118. strS = [NSString stringWithFormat:@"%d", s];
  119. }
  120. //设置UILabel视图显示当前播放时长
  121. av_s.didTimeLabel.text = [NSString stringWithFormat:@"%@:%@", strM, strS];
  122. }
  123. }];
  124. }
  125. // 给AVPlayerItem添加监控
  126. - (void)addObserverToPlayerItem:(AVPlayerItem *)playerItem {
  127. //监控状态属性,注意AVPlayer也有一个status属性,通过监控它的status也可以获得播放状态
  128. [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
  129. //监控网络加载情况属性
  130. [playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
  131. }
  132. //通知移除方法
  133. - (void)removeObserverFromPlayerItem:(AVPlayerItem *)playerItem {
  134. [playerItem removeObserver:self forKeyPath:@"status"];
  135. [playerItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
  136. }
  137. /*
  138. * 通过KVO监控播放器状态
  139. * @param keyPath 监控属性
  140. * @param object 监视器
  141. * @param change 状态改变
  142. * @param context 上下文
  143. */
  144. //通知所执行的方法
  145. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  146. AVPlayerItem *playerItem = object;//获取监控对象
  147. if ([keyPath isEqualToString:@"status"]) {
  148. //获取status值
  149. AVPlayerStatus status = [[change objectForKey:@"new"] intValue];
  150. //暂停状态
  151. if (status == AVPlayerStatusReadyToPlay) {
  152. self.totalTime = playerItem.duration;//获取视频总时长
  153. [self customVideoSlider:playerItem.duration];//设置播放进度最大值
  154. int totalTime = (int)CMTimeGetSeconds(playerItem.duration);
  155. int m = totalTime / 60;
  156. int s = totalTime % 60;
  157. NSString *strM = nil;
  158. NSString *strS = nil;
  159. if (m < 10) {
  160. strM = [NSString stringWithFormat:@"0%d", m];
  161. } else {
  162. strM = [NSString stringWithFormat:@"%d", m];
  163. }
  164. if (s < 10) {
  165. strS = [NSString stringWithFormat:@"0%d", s];
  166. } else {
  167. strS = [NSString stringWithFormat:@"%d", s];
  168. }
  169. //设置视频时长的UILabel视图显示值
  170. self.totalTimeLabel.text = [NSString stringWithFormat:@"%@:%@", strM, strS];
  171. }
  172. } else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
  173. //播放状态
  174. [self addNotification];//添加播放完成通知
  175. NSArray *array = playerItem.loadedTimeRanges;
  176. CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];//本次缓冲时间范围
  177. float startSeconds = CMTimeGetSeconds(timeRange.start);
  178. float durationSeconds = CMTimeGetSeconds(timeRange.duration);
  179. NSTimeInterval totalBuffer = startSeconds + durationSeconds;//缓冲总长度
  180. //设置缓存条
  181. [self.playProgress setProgress:totalBuffer/CMTimeGetSeconds(self.totalTime) animated:YES];
  182. float currentPlayTime = CMTimeGetSeconds([self.player currentTime]);//获取当前播放时长
  183. //设定当缓冲总时长超过播放时长3秒时开始播放视频
  184. if (totalBuffer - currentPlayTime > 3.0) {
  185. if (!self.didPlay) {
  186. [self createPlayer:self.frame];
  187. [self.player play];
  188. [MBProgressHUD hideHUDForView:self animated:YES];
  189. [self bringSubviewToFront:self.barView];
  190. [self.playButton setImage:[UIImage imageNamed:@"pause_64"] forState:UIControlStateNormal];
  191. self.didPlay = YES;
  192. }
  193. } else {
  194. [self.player pause];
  195. [MBProgressHUD showHUDAddedTo:self animated:YES];
  196. self.didPlay = NO;
  197. [self.playButton setImage:[UIImage imageNamed:@"play_64"] forState:UIControlStateNormal];
  198. }
  199. }
  200. }
  201. //播放视图的创建
  202. - (void)creatView:(CGRect)frame {
  203. self.backIamge = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
  204. self.backIamge.image = [UIImage imageNamed:@"back_Image.png"];
  205. self.barView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height-40, frame.size.width, 40)];
  206. self.barView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
  207. self.playProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(50, 15, self.barView.frame.size.width-100, 10)];
  208. [self.playProgress setProgressTintColor:[UIColor orangeColor]];
  209. self.playSlider = [[UISlider alloc] initWithFrame:CGRectMake(47, 11.2, self.playProgress.frame.size.width+10, 10)];
  210. [self.playSlider setThumbImage:[UIImage imageNamed:@"ball_16"] forState:UIControlStateNormal];
  211. self.didTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 25, 50, 10)];
  212. self.didTimeLabel.font = [UIFont systemFontOfSize:14];
  213. self.totalTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.barView.frame.size.width-100, 25, 50, 10)];
  214. self.totalTimeLabel.font = [UIFont systemFontOfSize:14];
  215. //=================
  216. UIGraphicsBeginImageContextWithOptions((CGSize){ 2, 2 }, NO, 0.0f);
  217. UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext();
  218. UIGraphicsEndImageContext();
  219. [self.playSlider setMinimumTrackTintColor:[UIColor blueColor]];
  220. [self.playSlider setMaximumTrackImage:transparentImage forState:UIControlStateNormal];
  221. //=================
  222. [self.playSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
  223. self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
  224. self.playButton.frame = CGRectMake(5, 5, 30, 30);
  225. [self.playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  226. [self.playButton setBackgroundColor:[UIColor clearColor]];
  227. [self.playButton setImage:[UIImage imageNamed:@"play_64.png"] forState:UIControlStateNormal];
  228. self.fullScreenButton = [UIButton buttonWithType:UIButtonTypeCustom];
  229. self.fullScreenButton.frame = CGRectMake(self.barView.frame.size.width - 40, 5, 30, 30);
  230. [self.fullScreenButton addTarget:self action:@selector(fullScreenButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  231. [self.fullScreenButton setBackgroundColor:[UIColor clearColor]];
  232. [self.fullScreenButton setImage:[UIImage imageNamed:@"fullScreen_64.png"] forState:UIControlStateNormal];
  233. [self.barView addSubview:self.playButton];
  234. [self.barView addSubview:self.playProgress];
  235. [self.barView addSubview:self.playSlider];
  236. [self.barView addSubview:self.didTimeLabel];
  237. [self.barView addSubview:self.totalTimeLabel];
  238. [self.barView addSubview:self.fullScreenButton];
  239. [self addSubview:self.barView];
  240. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
  241. [self addGestureRecognizer:tap];
  242. }
  243. - (void)tapAction:(UITapGestureRecognizer *)tap {
  244. if (self.hidenBar) {
  245. self.hidenBar = NO;
  246. [self bringSubviewToFront:self.barView];
  247. } else {
  248. self.hidenBar = YES;
  249. }
  250. self.barView.hidden = self.hidenBar;
  251. }
  252. - (void)playButtonAction:(UIButton *)button
  253. {
  254. NSLog(@"%f", self.player.rate);
  255. if(self.player.rate == 0){ //说明是暂停
  256. [button setImage:[UIImage imageNamed:@"pause_64"] forState:UIControlStateNormal];
  257. self.backIamge.hidden = YES;
  258. if (self.didPlay) {
  259. [self.player play];
  260. }
  261. }else if(self.player.rate == 1){//正在播放
  262. [self.player pause];
  263. [button setImage:[UIImage imageNamed:@"play_64"] forState:UIControlStateNormal];
  264. }
  265. }
  266. //全屏设置
  267. - (void)fullScreenButtonAction:(UIButton *)button {
  268. if (self.isFullScreen) {
  269. self.isFullScreen = NO;
  270. [self.fullScreenButton setImage:[UIImage imageNamed:@"fullScreen_64.png"] forState:UIControlStateNormal];
  271. [UIView animateWithDuration:0.2 animations:^{
  272. self.transform = CGAffineTransformRotate(self.transform, -M_PI_2);
  273. self.frame = self.originalFrame;
  274. [self.playerLayer removeFromSuperlayer];
  275. [self createPlayer:self.originalFrame];
  276. self.barView.frame = CGRectMake(0, self.originalFrame.size.height-40, self.originalFrame.size.width, 40);
  277. [self bringSubviewToFront:self.barView];
  278. self.center = CGPointMake(self.originalFrame.origin.x + self.originalFrame.size.width/2.0, self.originalFrame.origin.y + self.originalFrame.size.height/2.0);
  279. self.backIamge.frame = CGRectMake(0, 0, self.originalFrame.size.width, self.originalFrame.size.height);
  280. self.playProgress.frame = CGRectMake(50, 15, self.barView.frame.size.width-100, 10);
  281. self.playSlider.frame = CGRectMake(45, 11, self.playProgress.frame.size.width+10, 10);
  282. self.playButton.frame = CGRectMake(5, 5, 30, 30);
  283. self.fullScreenButton.frame = CGRectMake(self.barView.frame.size.width - 40, 5, 30, 30);
  284. [self.superView addSubview:self];
  285. }];
  286. } else {
  287. self.isFullScreen = YES;
  288. self.superView = self.superview;
  289. [self.fullScreenButton setImage:[UIImage imageNamed:@"exitFullScreen_64.png"] forState:UIControlStateNormal];
  290. [self.window addSubview:self];
  291. [self.window bringSubviewToFront:self];
  292. [UIView animateWithDuration:0.2 animations:^{
  293. self.frame = CGRectMake(20, 0, [UIScreen mainScreen].bounds.size.height-20, [UIScreen mainScreen].bounds.size.width);
  294. [self.playerLayer removeFromSuperlayer];
  295. [self createPlayer:self.frame];
  296. self.barView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.width-40, [UIScreen mainScreen].bounds.size.height-20, 40);
  297. [self bringSubviewToFront:self.barView];
  298. self.backIamge.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height-20, [UIScreen mainScreen].bounds.size.width);
  299. self.transform = CGAffineTransformRotate(self.transform, M_PI_2);
  300. self.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2.0, ([UIScreen mainScreen].bounds.size.height+20)/2.0);
  301. self.playProgress.frame = CGRectMake(50, 15, self.barView.frame.size.width-100, 10);
  302. self.playSlider.frame = CGRectMake(45, 11, self.playProgress.frame.size.width+10, 10);
  303. self.playButton.frame = CGRectMake(5, 5, 30, 30);
  304. self.fullScreenButton.frame = CGRectMake(self.barView.frame.size.width - 40, 5, 30, 30);
  305. self.totalTimeLabel.frame = CGRectMake(self.barView.frame.size.width-100, 25, 50, 10);
  306. }];
  307. }
  308. }
  309. //UISlider基本设置
  310. - (void)customVideoSlider:(CMTime)duration {
  311. self.playSlider.maximumValue = CMTimeGetSeconds(duration);
  312. self.playSlider.minimumValue = 0.0;
  313. }
  314. //拖动进度条到任何位置播放
  315. - (void)sliderAction:(UISlider *)slider {
  316. if (self.player.rate == 0) {
  317. [self.player seekToTime:CMTimeMake((int)slider.value*10, 10.0)];
  318. [self.player play];
  319. [self.playButton setImage:[UIImage imageNamed:@"pause_64.png"] forState:UIControlStateNormal];
  320. } else if(self.player.rate == 1) {
  321. [self.player pause];
  322. [self.player seekToTime:CMTimeMake((int)slider.value*10, 10.0)];
  323. [self.player play];
  324. [self.playButton setImage:[UIImage imageNamed:@"pause_64.png"] forState:UIControlStateNormal];
  325. }
  326. }
  327. @end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392

Demo :http://download.csdn.net/detail/yangxinca/8949003

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号