[IOS]对视频、音频播放器添加缓冲进度条

来源:互联网 发布:系统网管软件 编辑:程序博客网 时间:2024/05/17 08:40

转载:http://blog.csdn.net/wsyx768/article/details/46697683


[IOS]对视频、音频播放器添加缓冲进度条


Demo地址:http://download.csdn.net/detail/u012881779/8854967

思路说起来很简单:

先拖一个Progress View控件,设置它的Progress Tint和Track Tint;

再拖一个Horizontal Slider覆盖在ProgressView上面,设置它的Min Track Tint 同时设置Max Track Tint为Clear Color(为了透过控件看见背后progressView的背景);

最后分别设置一下各进度的值就OK了。

Min Track Tint(播放进度)、Progress Tint(缓冲进度)、Track Tint(进度条背景)。

[objc] view plaincopyprint?
  1. @interface AVVideoViewController: UIViewController  
  2. @property (weak, nonatomic) IBOutlet UISlider       *mScrubber;     //播放进度  
  3. @property (weak, nonatomic) IBOutlet UIProgressView *cacheProgressV;//缓冲进度  
  4. @end  
  5.   
  6. @implementation AVVideoViewController  
  7.   
  8. - (void)syncScrubber  
  9. {  
  10.     /*播放进度*/  
  11.     CMTime playerDuration = [self playerItemDuration];  
  12.     if (CMTIME_IS_INVALID(playerDuration))   
  13.     {  
  14.         mScrubber.minimumValue = 0.0;  
  15.         [self showStarLabletimeToEndLableTime:0.0 end:0.0];  
  16.         return;  
  17.     }   
  18.       
  19.     double duration = CMTimeGetSeconds(playerDuration);  
  20.     if (isfinite(duration))  
  21.     {  
  22.         float minValue = [self.mScrubber minimumValue];  
  23.         float maxValue = [self.mScrubber maximumValue];  
  24.         double time = CMTimeGetSeconds([self.mPlayer currentTime]);  
  25.         [self showStarLabletimeToEndLableTime:time end:duration];  
  26.         [self.mScrubber setValue:(maxValue - minValue) * time / duration + minValue];  
  27.     }  
  28.   
  29.     /*缓冲进度*/  
  30.     NSTimeInterval timeInterval = [self availableDuration];  
  31.     NSLog(@"Time I nterval:%f",timeInterval);  
  32.     CMTime duration11 = self.mPlayerItem.duration;  
  33.     CGFloat totalDuration = CMTimeGetSeconds(duration11);  
  34.     [_cacheProgressV setProgress:timeInterval / totalDuration animated:YES];  
  35. }  
  36.   
  37. // 计算缓冲进度  
  38. - (NSTimeInterval)availableDuration {  
  39.     NSArray *loadedTimeRanges = [[self.mPlayer currentItem] loadedTimeRanges];  
  40.     CMTimeRange timeRange = [loadedTimeRanges.firstObject CMTimeRangeValue];// 获取缓冲区域  
  41.     float startSeconds = CMTimeGetSeconds(timeRange.start);  
  42.     float durationSeconds = CMTimeGetSeconds(timeRange.duration);  
  43.       
  44.     NSTimeInterval result = startSeconds + durationSeconds;// 计算缓冲总进度  
  45.       
  46.     return result;  
  47. }  
  48.   
  49. @end  
  50.    


示意图:




0 0
原创粉丝点击