Advertisement
Guest User

Untitled

a guest
Jun 7th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.73 KB | None | 0 0
  1.  
  2. #import <AVKit/AVKit.h>
  3. #import "C8VideoPlaybackController.h"
  4. #import "C8PlayerView.h"
  5. #import "C8AppDelegate.h"
  6.  
  7. static void *C8VideoPlaybackControllerRateObservationContext = &C8VideoPlaybackControllerRateObservationContext;
  8. static void *C8VideoPlaybackControllerStatusObservationContext = &C8VideoPlaybackControllerStatusObservationContext;
  9. static void *C8VideoPlaybackControllerCurrentItemObservationContext = &C8VideoPlaybackControllerCurrentItemObservationContext;
  10.  
  11. static NSTimeInterval const kHideControlsAnimationDuration = 1.0;
  12. static NSTimeInterval const kAutoHideControlsInterval = 2.0;
  13.  
  14.  
  15. @interface C8VideoPlaybackController ()<AVPlayerViewControllerDelegate>
  16.  
  17. @property (nonatomic, weak) IBOutlet UIButton *closeButton;
  18.  
  19.  
  20. @property (nonatomic, weak) IBOutlet C8PlayerView *playerView;
  21. @property (weak, nonatomic) IBOutlet UIView *controlsView;
  22.  
  23. @property (nonatomic) AVPlayer *player;
  24. @property (nonatomic,strong) C8AppDelegate *appDelegate;
  25.  
  26. @property (weak, nonatomic) IBOutlet UISlider *progressControl;
  27.  
  28. @property (strong, nonatomic) IBOutlet UIButton *playButton;
  29. @property (strong, nonatomic) IBOutlet UIButton *pauseButton;
  30. @property (weak, nonatomic) IBOutlet UIView *playPausePlaceholder;
  31.  
  32. @property (nonatomic) id playerTimeObserver;
  33. @property (nonatomic) float previousRate;
  34. @property (nonatomic) BOOL isSeeking;
  35.  
  36. @property (nonatomic, nullable) NSTimer *autoHideControlsViewTimer;
  37.  
  38. @end
  39.  
  40. @implementation C8VideoPlaybackController
  41.  
  42. + (instancetype)instantiate {
  43. UIStoryboard *story = [UIStoryboard storyboardWithName:@"C8VideoPlayback" bundle:[CSDKSetup sdkBundle]];
  44. C8VideoPlaybackController *controller = (C8VideoPlaybackController *)[story instantiateViewControllerWithIdentifier:@"C8VideoPlaybackController"];
  45.  
  46. [AVAudioSession.sharedInstance setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:NULL];
  47. [AVAudioSession.sharedInstance setActive:YES error:NULL];
  48.  
  49. return controller;
  50. }
  51.  
  52. -(void)viewDidLayoutSubviews{
  53. self.playerView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
  54. }
  55.  
  56. - (void)setPlayer:(AVPlayer *)player {
  57. NSLog(@"assett percentageinY 7 c8prefetching 2 dataListItem 2: %@ - %@",[self.appDelegate.globalAvPlayers objectForKey:_videoURL],_videoURL);
  58. // NSMutableDictionary * myDictionary = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dictionaryForAvPlayers"] mutableCopy];
  59. // NSLog(@"c8prefetching 1 dataListItem: %@ - %@ - %lu",[myDictionary objectForKey:_videoURL],_videoURL,(unsigned long)myDictionary.count);
  60. // if (nil == _videoURL) {
  61. // return;
  62. // }
  63. //
  64. // if(_videoURL){
  65. // self.appDelegate = (C8AppDelegate *)[UIApplication sharedApplication].delegate;
  66. //
  67. // NSLog(@"c8prefetching 2 dataListItem 0: %@ - %@",[self.appDelegate.globalAvPlayers objectForKey:_videoURL],_videoURL);
  68. // player=[self.appDelegate.globalAvPlayers objectForKey:_videoURL];
  69. // player = [myDictionary objectForKey:_videoURL];
  70. self.playerView.player = player;
  71. //(AVPlayer*)[self.appDelegate.globalAvPlayers objectForKey:_videoURL];
  72. // }
  73. }
  74.  
  75. - (AVPlayer *)player {
  76. NSLog(@"assett percentageinY 6 c8prefetching 2 dataListItem 2: %@ - %@",[self.appDelegate.globalAvPlayers objectForKey:_videoURL],_videoURL);
  77. // if(_videoURL){
  78. // self.appDelegate = (C8AppDelegate *)[UIApplication sharedApplication].delegate;
  79. // self.playerView.player = [self.appDelegate.globalAvPlayers objectForKey:_videoURL];
  80. return self.playerView.player;
  81. // }
  82. }
  83.  
  84. - (void)setVideoURL:(NSURL *)videoURL {
  85. NSLog(@"assett return before before %@ - %@",_videoURL,videoURL);
  86. if (_videoURL != videoURL){
  87.  
  88. [self invalidateObservingForCurrentPlayer];
  89. NSLog(@"assett return before %@",_videoURL);
  90. _videoURL = [videoURL copy];
  91. if (nil == _videoURL) {
  92. NSLog(@"assett return %@",_videoURL);
  93. return;
  94. }
  95.  
  96. self.appDelegate = (C8AppDelegate *)[UIApplication sharedApplication].delegate;
  97. // AVPlayerItem *tempItem = (AVPlayerItem*)[[self.appDelegate.globalAvPlayers objectForKey:_videoURL] currentItem];
  98. // [AVURLAsset URLAssetWithURL:_videoURL options:nil];
  99. // AVURLAsset *asset = (AVURLAsset *)tempItem.asset;
  100. AVURLAsset *asset=[AVURLAsset URLAssetWithURL:_videoURL options:nil];
  101. NSArray *requestedKeys = @[@"playable"];
  102.  
  103. NSLog(@"assett percentageinY 8 c8prefetching 2 dataListItem 1: %@ - _videoURL:%@ - videoURL:%@",[self.appDelegate.globalAvPlayers objectForKey:_videoURL],_videoURL,videoURL);
  104. /* Tells the asset to load the values of any of the specified keys that are not already loaded. */
  105. [asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler:
  106. ^{
  107. dispatch_async( dispatch_get_main_queue(), ^{
  108. NSLog(@"assett return prepareToPlayAsset %@ - %@",self.videoURL,[self.appDelegate.globalAvPlayers objectForKey:self.videoURL]);
  109. /* IMPORTANT: Must dispatch to main queue in order to operate on the AVPlayer and AVPlayerItem. */
  110. // if (self.player == nil) {
  111. [self prepareToPlayAsset:asset withKeys:requestedKeys];
  112. // }
  113. });
  114. }];
  115. }
  116. }
  117.  
  118. - (UIStatusBarStyle)preferredStatusBarStyle
  119. {
  120. return UIStatusBarStyleLightContent;
  121. }
  122.  
  123. - (AVLayerVideoGravity)videoGravity {
  124. return self.playerView.playerLayer.videoGravity;
  125. }
  126.  
  127. - (void)setVideoGravity:(AVLayerVideoGravity)videoGravity {
  128. self.playerView.playerLayer.videoGravity = videoGravity;
  129. }
  130.  
  131. - (BOOL)playbackControlHidden {
  132. return self.playPausePlaceholder.isHidden;
  133. }
  134.  
  135. - (void)setPlaybackControlHidden:(BOOL)playbackControlHidden {
  136. self.playPausePlaceholder.hidden = playbackControlHidden;
  137. }
  138.  
  139. - (BOOL)progressControlHidden {
  140. return self.progressControl.isHidden;
  141. }
  142.  
  143. - (void)setProgressControlHidden:(BOOL)progressControlHidden {
  144. self.progressControl.hidden = progressControlHidden;
  145. if (!progressControlHidden && nil == self.playerTimeObserver) {
  146. [self setupPlayerTimeObserver];
  147. }
  148. }
  149.  
  150. - (void)setProgressControlEnabled:(BOOL)progressControlEnabled {
  151. _progressControlEnabled = progressControlEnabled;
  152. if (_progressControlEnabled) {
  153. if (self.player.currentItem.status == AVPlayerItemStatusReadyToPlay)
  154. self.progressControl.enabled = YES;
  155. } else {
  156. self.progressControl.enabled = NO;
  157. }
  158. }
  159.  
  160. - (BOOL)closeButtonHidden {
  161. (void)self.view;
  162. return self.closeButton.hidden;
  163. }
  164.  
  165. - (void)setCloseButtonHidden:(BOOL)closeButtonHidden {
  166. (void)self.view;
  167. self.closeButton.hidden = closeButtonHidden;
  168. }
  169.  
  170. - (BOOL)skipButtonHidden {
  171. (void)self.view;
  172. return self.skipButton.hidden;
  173. }
  174.  
  175. - (void)setSkipButtonHidden:(BOOL)skipButtonHidden {
  176. (void)self.view;
  177. self.skipButton.hidden = skipButtonHidden;
  178. }
  179.  
  180. - (void)viewDidLoad {
  181.  
  182. [super viewDidLoad];
  183. self.playPausePlaceholder.exclusiveTouch = YES;
  184. self.progressControl.exclusiveTouch = YES;
  185. }
  186.  
  187. - (void)videoDidPlayToEnd:(NSNotification *)notification {
  188. if (notification.object != self.player.currentItem) {
  189. return;
  190. }
  191.  
  192. dispatch_async(dispatch_get_main_queue(), ^{
  193. [self.delegate playbackControllerVideoDidPlayToEnd:self];
  194. });
  195. }
  196.  
  197. - (void)viewDidAppear:(BOOL)animated {
  198. [super viewDidAppear:animated];
  199. }
  200.  
  201. - (void)viewDidDisappear:(BOOL)animated {
  202. [super viewDidDisappear:animated];
  203.  
  204. [self.player pause];
  205. }
  206.  
  207. - (BOOL)playing {
  208. return self.player.rate > 0.0f && self.player.error == nil;
  209. }
  210.  
  211. #pragma mark - actions
  212.  
  213. - (IBAction)handleTapOnPlayerView:(id)sender {
  214.  
  215. [self invalidateAutoHideControlsTimer];
  216. [self presentControls:YES];
  217. [self scheduleAutoHideControls];
  218. }
  219.  
  220. - (IBAction)handleClose:(id)sender {
  221. [self.player pause];
  222.  
  223. if ([self.delegate playbackControllerShouldClose:self]) {
  224. [self dismissViewControllerAnimated:YES completion:nil];
  225. }
  226. }
  227.  
  228. - (IBAction)handleSkip:(id)sender {
  229. [self.player replaceCurrentItemWithPlayerItem:nil];
  230. if (nil!=self.delegate) {
  231. [self.delegate playbackControllerVideoSkippedVideo:self];
  232. }
  233. }
  234.  
  235.  
  236. - (IBAction)play:(id)sender {
  237. [self.player play];
  238. }
  239. - (IBAction)pause:(id)sender {
  240. [self.player pause];
  241. }
  242.  
  243. #pragma mark - controls
  244.  
  245. - (CMTime)playerItemDuration
  246. {
  247. AVPlayerItem *thePlayerItem = self.player.currentItem;
  248. if (thePlayerItem.status == AVPlayerItemStatusReadyToPlay)
  249. {
  250.  
  251. return thePlayerItem.duration;
  252. }
  253.  
  254. return kCMTimeInvalid;
  255. }
  256.  
  257. #pragma mark - hide/present controls
  258.  
  259. - (void)scheduleAutoHideControls {
  260. self.autoHideControlsViewTimer = [NSTimer scheduledTimerWithTimeInterval:kAutoHideControlsInterval target:self selector:@selector(autoHideControlsTimerFired:) userInfo:nil repeats:NO];
  261. }
  262.  
  263. - (void)invalidateAutoHideControlsTimer {
  264. [self.autoHideControlsViewTimer invalidate];
  265. self.autoHideControlsViewTimer = nil;
  266. }
  267.  
  268. - (void)autoHideControlsTimerFired:(NSTimer *)timer {
  269. NSAssert(timer == self.autoHideControlsViewTimer, @"");
  270. [self hideControls:YES];
  271. }
  272.  
  273. - (void)presentControls:(BOOL)animated {
  274.  
  275. self.controlsView.hidden = NO;
  276. if (animated) {
  277. self.controlsView.alpha = 0.0;
  278. [UIView animateWithDuration:kHideControlsAnimationDuration animations:
  279. ^{
  280. self.controlsView.alpha = 1.0;
  281. }];
  282. }
  283. }
  284.  
  285. - (void)hideControls:(BOOL)animated {
  286.  
  287. [self invalidateAutoHideControlsTimer];
  288.  
  289. if (animated) {
  290. [UIView animateWithDuration:kHideControlsAnimationDuration animations:^{
  291. self.controlsView.alpha = 0.0;
  292. } completion:^(BOOL finished) {
  293. self.controlsView.hidden = YES;
  294. self.controlsView.alpha = 1.0;
  295. }];
  296.  
  297. } else {
  298. self.controlsView.hidden = YES;
  299. }
  300. }
  301.  
  302. #pragma mark - movie progress control
  303.  
  304. -(void)setupPlayerTimeObserver
  305. {
  306. double interval = .1f;
  307.  
  308. CMTime playerDuration = [self playerItemDuration];
  309. if (CMTIME_IS_INVALID(playerDuration))
  310. {
  311. return;
  312. }
  313. double duration = CMTimeGetSeconds(playerDuration);
  314. if (isfinite(duration))
  315. {
  316. interval = duration / 100.f;
  317. }
  318.  
  319. @weakify(self)
  320. self.playerTimeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(interval, NSEC_PER_SEC) queue:NULL usingBlock:^(CMTime time) {
  321. @strongify(self)
  322. [self syncProgressControl];
  323. }];
  324. }
  325.  
  326. - (void)syncProgressControl
  327. {
  328. CMTime playerDuration = [self playerItemDuration];
  329. if (CMTIME_IS_INVALID(playerDuration))
  330. {
  331. self.progressControl.minimumValue = 0.0;
  332. return;
  333. }
  334.  
  335. double duration = CMTimeGetSeconds(playerDuration);
  336. if (isfinite(duration))
  337. {
  338. float minValue = [self.progressControl minimumValue];
  339. float maxValue = [self.progressControl maximumValue];
  340. double time = CMTimeGetSeconds([self.player currentTime]);
  341.  
  342. [self.progressControl setValue:(maxValue - minValue) * time / duration + minValue animated:YES];
  343. }
  344. }
  345.  
  346. // The user is dragging the movie controller thumb to scrub through the movie.
  347. - (IBAction)beginScrubbing:(id)sender
  348. {
  349. self.previousRate = [self.player rate];
  350. [self.player setRate:0.f];
  351.  
  352. /* Remove previous timer. */
  353. [self removePlayerTimeObserver];
  354. }
  355.  
  356. // Set the player current time to match the scrubber position.
  357. - (IBAction)scrub:(id)sender
  358. {
  359. if ([sender isKindOfClass:[UISlider class]] && !self.isSeeking)
  360. {
  361. self.isSeeking = YES;
  362. UISlider* slider = sender;
  363.  
  364. CMTime playerDuration = [self playerItemDuration];
  365. if (CMTIME_IS_INVALID(playerDuration)) {
  366. return;
  367. }
  368.  
  369. double duration = CMTimeGetSeconds(playerDuration);
  370. if (isfinite(duration))
  371. {
  372. float minValue = [slider minimumValue];
  373. float maxValue = [slider maximumValue];
  374. float value = [slider value];
  375.  
  376. double time = duration * (value - minValue) / (maxValue - minValue);
  377.  
  378. [self.player seekToTime:CMTimeMakeWithSeconds(time, NSEC_PER_SEC) completionHandler:^(BOOL finished) {
  379. dispatch_async(dispatch_get_main_queue(), ^{
  380. self.isSeeking = NO;
  381. });
  382. }];
  383. }
  384. }
  385. }
  386.  
  387. // The user has released the movie thumb control to stop scrubbing through the movie.
  388. - (IBAction)endScrubbing:(id)sender
  389. {
  390. if (nil == self.playerTimeObserver)
  391. {
  392. [self setupPlayerTimeObserver];
  393. }
  394.  
  395. [self.player setRate:self.previousRate];
  396. self.previousRate = 0.f;
  397. }
  398.  
  399. - (BOOL)isScrubbing
  400. {
  401. return self.previousRate > 0.f;
  402. }
  403.  
  404. #pragma mark -
  405. #pragma mark Play, Stop buttons
  406.  
  407. // Show the stop button in the movie player controller.
  408. - (void)showStopButton
  409. {
  410. UIView *presentingControl = [self.playPausePlaceholder subviews].firstObject;
  411. [presentingControl removeFromSuperview];
  412. [self.playPausePlaceholder addSubview:self.pauseButton];
  413. [self.playPausePlaceholder mas_makeConstraints:^(MASConstraintMaker *make) {
  414. make.edges.equalTo(self.pauseButton).with.insets(UIEdgeInsetsZero);
  415. }];
  416. }
  417.  
  418. // Show the play button in the movie player controller.
  419. - (void)showPlayButton
  420. {
  421. UIView *presentingControl = [self.playPausePlaceholder subviews].firstObject;
  422. [presentingControl removeFromSuperview];
  423. [self.playPausePlaceholder addSubview:self.playButton];
  424. [self.playPausePlaceholder mas_makeConstraints:^(MASConstraintMaker *make) {
  425. make.edges.equalTo(self.playButton).with.insets(UIEdgeInsetsZero);
  426. }];
  427. }
  428.  
  429. // If the media is playing, show the stop button; otherwise, show the play button.
  430. - (void)syncPlayPauseButtons
  431. {
  432. if (self.playing)
  433. {
  434. [self showStopButton];
  435. }
  436. else
  437. {
  438. [self showPlayButton];
  439. }
  440. }
  441.  
  442. - (void)enablePlayerButtons
  443. {
  444. self.playButton.enabled = YES;
  445. self.pauseButton.enabled = YES;
  446. }
  447.  
  448. - (void)disablePlayerButtons
  449. {
  450. self.playButton.enabled = NO;
  451. self.pauseButton.enabled = NO;
  452. }
  453. #pragma mark -
  454.  
  455. -(void)removePlayerTimeObserver
  456. {
  457. if (self.playerTimeObserver)
  458. {
  459. [self.player removeTimeObserver:self.playerTimeObserver];
  460. self.playerTimeObserver = nil;
  461. }
  462. }
  463.  
  464. - (void)invalidateObservingForCurrentPlayer {
  465. self.appDelegate = (C8AppDelegate *)[UIApplication sharedApplication].delegate;
  466.  
  467. @try{
  468. // if ([self observationInfo]){
  469. NSLog(@"assett invalidate");
  470. [self invalidateAutoHideControlsTimer];
  471. [self removePlayerTimeObserver];
  472. [self.player removeObserver:self forKeyPath:@"rate"];
  473. [self.player.currentItem removeObserver:self forKeyPath:@"status"];
  474. [self.player pause];
  475. // self.playerView =nil;
  476. // self.controller
  477. self.player = nil;
  478. // }
  479. }
  480. @catch (NSException *e){
  481. NSLog(@"exception e: %@",e.description);
  482. }
  483.  
  484.  
  485. }
  486.  
  487. #pragma mark -
  488.  
  489. - (void)observeValueForKeyPath:(NSString*) path
  490. ofObject:(id)object
  491. change:(NSDictionary*)change
  492. context:(void*)context
  493. {
  494. /* AVPlayerItem "status" property value observer. */
  495. if (context == C8VideoPlaybackControllerStatusObservationContext)
  496. {
  497.  
  498. [self syncPlayPauseButtons];
  499.  
  500. AVPlayerItemStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
  501. switch (status)
  502. {
  503. /* Indicates that the status of the player is not yet known because
  504. it has not tried to load new media resources for playback */
  505. case AVPlayerItemStatusUnknown:
  506. {
  507. [self removePlayerTimeObserver];
  508. [self syncProgressControl];
  509.  
  510. self.progressControl.enabled = NO;
  511. [self disablePlayerButtons];
  512. }
  513. break;
  514.  
  515. case AVPlayerItemStatusReadyToPlay:
  516. {
  517. // NSArray<AVAssetTrack*>* tracks = self.player.currentItem.asset.tracks;
  518. // NSLog(@"tracks: %lu", tracks.count);
  519.  
  520. /* Once the AVPlayerItem becomes ready to play, i.e.
  521. [playerItem status] == AVPlayerItemStatusReadyToPlay,
  522. its duration can be fetched from the item. */
  523. if ([self.delegate respondsToSelector:@selector(playbackControllerShouldAutoplay:)]) {
  524. if ([self.delegate playbackControllerShouldAutoplay:self]) {
  525. NSString *isEightFeedVisible = [[NSUserDefaults standardUserDefaults] stringForKey:@"isEightFeedVisible"];
  526. if ([isEightFeedVisible isEqualToString:@"YES"] )
  527. {
  528. [self.player play];
  529. }
  530. }
  531. }
  532.  
  533. if ([self.delegate respondsToSelector:@selector(playbackControllerShouldStartMuted:)]) {
  534. self.player.muted = [self.delegate playbackControllerShouldStartMuted:self];
  535. }
  536.  
  537. if (!self.progressControlHidden) {
  538. [self setupPlayerTimeObserver];
  539. }
  540.  
  541. self.progressControl.enabled = self.progressControlEnabled;
  542. [self enablePlayerButtons];
  543. }
  544. break;
  545.  
  546. case AVPlayerItemStatusFailed:
  547. {
  548. AVPlayerItem *playerItem = (AVPlayerItem *)object;
  549. [self assetFailedToPrepareForPlayback:playerItem.error];
  550. NSLog(@"failed to play: %@ - %@ - %@",playerItem.error.description,playerItem.error.localizedDescription,playerItem.error.debugDescription);
  551. }
  552. break;
  553. }
  554. }
  555. /* AVPlayer "rate" property value observer. */
  556. else if (context == C8VideoPlaybackControllerRateObservationContext)
  557. {
  558. if (self.progressControlHidden) { return; }
  559. NSAssert([NSThread currentThread].isMainThread, @"");
  560. [self syncPlayPauseButtons];
  561. if (!self.playing) {
  562. [self invalidateAutoHideControlsTimer];
  563. [self presentControls:YES];
  564. } else {
  565. [self hideControls:YES];
  566. }
  567. }
  568. else
  569. {
  570. [super observeValueForKeyPath:path ofObject:object change:change context:context];
  571. }
  572. }
  573.  
  574. #pragma mark - error handling
  575. -(void)assetFailedToPrepareForPlayback:(NSError *)error
  576. {
  577. [self removePlayerTimeObserver];
  578. [self syncProgressControl];
  579. self.progressControl.enabled = NO;
  580. [self disablePlayerButtons];
  581.  
  582. UIAlertController *alert = [UIAlertController alertControllerWithTitle:error.localizedDescription message:error.localizedFailureReason preferredStyle:UIAlertControllerStyleAlert];
  583. UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:NULL];
  584. [alert addAction:action];
  585.  
  586. //[self presentViewController:alert animated:YES completion:NULL];
  587. }
  588.  
  589. #pragma mark -
  590.  
  591. - (void)prepareToPlayAsset:(AVURLAsset *)asset withKeys:(NSArray *)requestedKeys
  592. {
  593. /* Make sure that the value of each key has loaded successfully. */
  594. for (NSString *thisKey in requestedKeys)
  595. {
  596. NSError *error = nil;
  597. AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];
  598. if (keyStatus == AVKeyValueStatusFailed)
  599. {NSLog(@"assett not playable 1");
  600. [self assetFailedToPrepareForPlayback:error];
  601. return;
  602. }
  603. }
  604.  
  605. /* Use the AVAsset playable property to detect whether the asset can be played. */
  606. if (!asset.playable)
  607. {NSLog(@"assett not playable");
  608. /* Generate an error describing the failure. */
  609. NSString *localizedDescription = NSLocalizedString(@"Item cannot be played", nil);
  610. NSString *localizedFailureReason = NSLocalizedString(@"The assets tracks were loaded, but can not be palyed", nil);
  611. NSDictionary *errorDict = @{
  612. NSLocalizedDescriptionKey: localizedDescription,
  613. NSLocalizedFailureReasonErrorKey: localizedFailureReason
  614. };
  615.  
  616. NSError *assetCannotBePlayedError = [NSError errorWithDomain:@"StitchedStreamPlayer" code:0 userInfo:errorDict];
  617.  
  618. [self assetFailedToPrepareForPlayback:assetCannotBePlayedError];
  619.  
  620. return;
  621. }else{
  622. NSLog(@"assett return playable %@",_videoURL);
  623. }
  624.  
  625. // Create a new instance of AVPlayerItem from the now successfully loaded AVAsset.
  626. self.appDelegate = (C8AppDelegate *)[UIApplication sharedApplication].delegate;
  627.  
  628. AVPlayerItem *temp_playerItem=[AVPlayerItem playerItemWithAsset:asset];
  629. // AVPlayerItem *temp_playerItem= (AVPlayerItem*)[[self.appDelegate.globalAvPlayers objectForKey:_videoURL] currentItem];
  630.  
  631. //AVPlayerItem *playerItem =temp_playerItem;// [AVPlayerItem playerItemWithAsset:asset];
  632.  
  633. [temp_playerItem addObserver:self
  634. forKeyPath:@"status"
  635. options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
  636. context:C8VideoPlaybackControllerStatusObservationContext];
  637.  
  638. // When the player item has played to its end time we'll toggle
  639. // the movie controller Pause button to be the Play button
  640. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoDidPlayToEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:temp_playerItem];
  641.  
  642. AVPlayer *temp_player=(AVPlayer*)[self.appDelegate.globalAvPlayers objectForKey:_videoURL];
  643. self.player = [AVPlayer playerWithPlayerItem:temp_playerItem];
  644. //self.player = temp_player;
  645. NSLog(@"assett return c8prefetching 2 dataListItem 3: %@ - %@ - self.player.currentItem:%@ - temp_playerItem: %@",[self.appDelegate.globalAvPlayers objectForKey:_videoURL],_videoURL,self.player.currentItem,temp_playerItem);
  646. //s Observe the AVPlayer "rate" property to update the scrubber control
  647. [self.player addObserver:self
  648. forKeyPath:@"rate"
  649. options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
  650. context:C8VideoPlaybackControllerRateObservationContext];
  651. //self.playerView.player = self.player;
  652.  
  653. [self syncPlayPauseButtons];
  654. [self.progressControl setValue:0.0];
  655. }
  656.  
  657. #pragma mark -
  658. - (void)dealloc {
  659.  
  660. [self invalidateObservingForCurrentPlayer];
  661. }
  662.  
  663. @end
  664.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement