ViewController.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // ViewController.m
  3. // VIMediaCacheDemo
  4. //
  5. // Created by Vito on 5/17/16.
  6. // Copyright © 2016 Vito. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import "VIMediaCache.h"
  10. #import "PlayerView.h"
  11. @interface ViewController ()
  12. @property (nonatomic, strong) VIResourceLoaderManager *resourceLoaderManager;
  13. @property (nonatomic, strong) AVPlayer *player;
  14. @property (nonatomic, strong) AVPlayerItem *playerItem;
  15. @property (nonatomic, strong) id timeObserver;
  16. @property (nonatomic) CMTime duration;
  17. @property (weak, nonatomic) IBOutlet PlayerView *playerView;
  18. @property (weak, nonatomic) IBOutlet UISlider *slider;
  19. @property (weak, nonatomic) IBOutlet UILabel *totalTimeLabel;
  20. @property (weak, nonatomic) IBOutlet UILabel *currentTimeLabel;
  21. @property (nonatomic, strong) VIMediaDownloader *downloader;
  22. @end
  23. @implementation ViewController
  24. - (void)dealloc {
  25. [[NSNotificationCenter defaultCenter] removeObserver:self];
  26. [self.player removeTimeObserver:self.timeObserver];
  27. self.timeObserver = nil;
  28. [self.playerItem removeObserver:self forKeyPath:@"status"];
  29. [self.player removeObserver:self forKeyPath:@"timeControlStatus"];
  30. }
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. [self cleanCache];
  34. // NSURL *url = [NSURL URLWithString:@"https://mvvideo5.meitudata.com/56ea0e90d6cb2653.mp4"];
  35. // VIMediaDownloader *downloader = [[VIMediaDownloader alloc] initWithURL:url];
  36. // [downloader downloadFromStartToEnd];
  37. // self.downloader = downloader;
  38. [self setupPlayer];
  39. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mediaCacheDidChanged:) name:VICacheManagerDidUpdateCacheNotification object:nil];
  40. }
  41. - (void)viewDidAppear:(BOOL)animated {
  42. [super viewDidAppear:animated];
  43. [self.player play];
  44. }
  45. - (void)cleanCache {
  46. unsigned long long fileSize = [VICacheManager calculateCachedSizeWithError:nil];
  47. NSLog(@"file cache size: %@", @(fileSize));
  48. NSError *error;
  49. [VICacheManager cleanAllCacheWithError:&error];
  50. if (error) {
  51. NSLog(@"clean cache failure: %@", error);
  52. }
  53. [VICacheManager cleanAllCacheWithError:&error];
  54. }
  55. - (IBAction)touchSliderAction:(UISlider *)sender {
  56. sender.tag = -1;
  57. }
  58. - (IBAction)sliderAction:(UISlider *)sender {
  59. CMTime duration = self.player.currentItem.asset.duration;
  60. CMTime seekTo = CMTimeMake((NSInteger)(duration.value * sender.value), duration.timescale);
  61. NSLog(@"seetTo %ld", (long)(duration.value * sender.value) / duration.timescale);
  62. __weak typeof(self)weakSelf = self;
  63. [self.player pause];
  64. [self.player seekToTime:seekTo completionHandler:^(BOOL finished) {
  65. sender.tag = 0;
  66. [weakSelf.player play];
  67. }];
  68. }
  69. - (IBAction)toggleAction:(id)sender {
  70. [self cleanCache];
  71. [self.playerItem removeObserver:self forKeyPath:@"status"];
  72. [self.player removeObserver:self forKeyPath:@"timeControlStatus"];
  73. [self.resourceLoaderManager cancelLoaders];
  74. NSURL *url = [NSURL URLWithString:@"https://mvvideo5.meitudata.com/56ea0e90d6cb2653.mp4"];
  75. AVPlayerItem *playerItem = [self.resourceLoaderManager playerItemWithURL:url];
  76. self.playerItem = playerItem;
  77. [self.playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
  78. [self.player addObserver:self forKeyPath:@"timeControlStatus" options:NSKeyValueObservingOptionNew context:nil];
  79. [self.player replaceCurrentItemWithPlayerItem:playerItem];
  80. }
  81. #pragma mark - Setup
  82. - (void)setupPlayer {
  83. // NSURL *url = [NSURL URLWithString:@"http://gedftnj8mkvfefuaefm.exp.bcevod.com/mda-hc2s2difdjz6c5y9/hd/mda-hc2s2difdjz6c5y9.mp4?playlist%3D%5B%22hd%22%5D&auth_key=1500559192-0-0-dcb501bf19beb0bd4e0f7ad30c380763&bcevod_channel=searchbox_feed&srchid=3ed366b1b0bf70e0&channel_id=2&d_t=2&b_v=9.1.0.0"];
  84. // NSURL *url = [NSURL URLWithString:@"https://mvvideo5.meitudata.com/56a9e1389b9706520.mp4"];
  85. NSURL *url = [NSURL URLWithString:@"https://mvvideo5.meitudata.com/56ea0e90d6cb2653.mp4"];
  86. VIResourceLoaderManager *resourceLoaderManager = [VIResourceLoaderManager new];
  87. self.resourceLoaderManager = resourceLoaderManager;
  88. AVPlayerItem *playerItem = [resourceLoaderManager playerItemWithURL:url];
  89. self.playerItem = playerItem;
  90. VICacheConfiguration *configuration = [VICacheManager cacheConfigurationForURL:url];
  91. if (configuration.progress >= 1.0) {
  92. NSLog(@"cache completed");
  93. }
  94. AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
  95. // AVPlayer *player = [AVPlayer playerWithURL:url];
  96. player.automaticallyWaitsToMinimizeStalling = NO;
  97. self.player = player;
  98. [self.playerView setPlayer:player];
  99. __weak typeof(self)weakSelf = self;
  100. self.timeObserver =
  101. [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 10)
  102. queue:dispatch_queue_create("player.time.queue", NULL)
  103. usingBlock:^(CMTime time) {
  104. dispatch_async(dispatch_get_main_queue(), ^(void) {
  105. if (weakSelf.slider.tag == 0) {
  106. CGFloat duration = CMTimeGetSeconds(weakSelf.player.currentItem.duration);
  107. weakSelf.totalTimeLabel.text = [NSString stringWithFormat:@"%.f", duration];
  108. CGFloat currentDuration = CMTimeGetSeconds(time);
  109. weakSelf.currentTimeLabel.text = [NSString stringWithFormat:@"%.f", currentDuration];
  110. weakSelf.slider.value = currentDuration / duration;
  111. }
  112. });
  113. }];
  114. [self.playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
  115. [self.player addObserver:self forKeyPath:@"timeControlStatus" options:NSKeyValueObservingOptionNew context:nil];
  116. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPlayerViewAction:)];
  117. [self.playerView addGestureRecognizer:tap];
  118. }
  119. - (void)tapPlayerViewAction:(UITapGestureRecognizer *)gesture {
  120. if (gesture.state == UIGestureRecognizerStateEnded) {
  121. if (self.player.rate > 0.0) {
  122. [self.player pause];
  123. } else {
  124. [self.player play];
  125. }
  126. }
  127. }
  128. #pragma mark - KVO
  129. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
  130. change:(NSDictionary *)change context:(void *)context {
  131. if (object == self.playerItem && [keyPath isEqualToString:@"status"]) {
  132. NSLog(@"player status %@, rate %@, error: %@", @(self.playerItem.status), @(self.player.rate), self.playerItem.error);
  133. if (self.playerItem.status == AVPlayerItemStatusReadyToPlay) {
  134. dispatch_async(dispatch_get_main_queue(), ^(void) {
  135. CGFloat duration = CMTimeGetSeconds(self.playerItem.duration);
  136. self.totalTimeLabel.text = [NSString stringWithFormat:@"%.f", duration];
  137. });
  138. } else if (self.playerItem.status == AVPlayerItemStatusFailed) {
  139. // something went wrong. player.error should contain some information
  140. NSLog(@"player error %@", self.playerItem.error);
  141. }
  142. } else if (object == self.player && [keyPath isEqualToString:@"timeControlStatus"]) {
  143. NSLog(@"timeControlStatus: %@, reason: %@, rate: %@", @(self.player.timeControlStatus), self.player.reasonForWaitingToPlay, @(self.player.rate));
  144. }
  145. }
  146. #pragma mark - notification
  147. - (void)mediaCacheDidChanged:(NSNotification *)notification {
  148. NSDictionary *userInfo = notification.userInfo;
  149. VICacheConfiguration *configuration = userInfo[VICacheConfigurationKey];
  150. NSArray<NSValue *> *cachedFragments = configuration.cacheFragments;
  151. long long contentLength = configuration.contentInfo.contentLength;
  152. NSInteger number = 100;
  153. NSMutableString *progressStr = [NSMutableString string];
  154. [cachedFragments enumerateObjectsUsingBlock:^(NSValue * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  155. NSRange range = obj.rangeValue;
  156. NSInteger location = roundf((range.location / (double)contentLength) * number);
  157. NSInteger progressCount = progressStr.length;
  158. [self string:progressStr appendString:@"0" muti:location - progressCount];
  159. NSInteger length = roundf((range.length / (double)contentLength) * number);
  160. [self string:progressStr appendString:@"1" muti:length];
  161. if (idx == cachedFragments.count - 1 && (location + length) <= number + 1) {
  162. [self string:progressStr appendString:@"0" muti:number - (length + location)];
  163. }
  164. }];
  165. NSLog(@"%@", progressStr);
  166. }
  167. - (void)string:(NSMutableString *)string appendString:(NSString *)appendString muti:(NSInteger)muti {
  168. for (NSInteger i = 0; i < muti; i++) {
  169. [string appendString:appendString];
  170. }
  171. }
  172. @end