Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Asset keys */
- NSString * const kTracksKeyT = @"tracks";
- NSString * const kPlayableKeyT = @"playable";
- /* PlayerItem keys */
- NSString * const kStatusKeyT = @"status";
- /* AVPlayer keys */
- NSString * const kRateKeyT = @"rate";
- NSString * const kCurrentItemKeyT = @"currentItem";
- /* Custom keys for displaying ads on Madness View*/
- NSString * const kReadyForAdDisplayKeyT = @"readyForDisplay";
- static void *AVPlayerDemoPlaybackViewControllerRateObservationContext = &AVPlayerDemoPlaybackViewControllerRateObservationContext;
- static void *AVPlayerDemoPlaybackViewControllerStatusObservationContext = &AVPlayerDemoPlaybackViewControllerStatusObservationContext;
- static void *AVPlayerDemoPlaybackViewControllerCurrentItemObservationContext = &AVPlayerDemoPlaybackViewControllerCurrentItemObservationContext;
- static void *AVPlayerDemoPlaybackViewControllerCurrentItemObservationContextForDisplay = &AVPlayerDemoPlaybackViewControllerCurrentItemObservationContextForDisplay;
- #define kMaxIdleTimeSeconds 5.0
- @interface LFVideoPlayerViewController (){
- AVPlayer *_player;
- AVPlayerItem *_playerItem;
- NSURL *_URL;
- id mTimeObserver;
- float mRestoreAfterScrubbingRate;
- BOOL seekToZeroBeforePlay;
- BOOL controlsHidden;
- UITapGestureRecognizer *_tapGestureRecognizer;
- NSTimer *idleTimer;
- }
- @end
- @implementation LFVideoPlayerViewController
- - (void)initialize
- {
- }
- - (id)init
- {
- self = [super init];
- if (self) {
- // Initialize self
- [self initialize];
- }
- return self;
- }
- - (id)initWithCoder:(NSCoder *)aDecoder
- {
- self = [super initWithCoder:aDecoder];
- if (self) {
- // Initialize self
- [self initialize];
- }
- return self;
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Initialize self
- [self initialize];
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [self.progressSlider addTarget:self action:@selector(progressSliderChanged:) forControlEvents:UIControlEventValueChanged];
- [self setPlayer:nil];
- [self.madnessView setVideoId:self.videoId];
- UIImage *maxImage = [[UIImage imageNamed:@"slider_empty.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 8, 0, 8)];
- UIImage *minImage = [[UIImage imageNamed:@"slider_filled.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 8, 0, 8)];
- [self.progressSlider setMaximumTrackImage:maxImage forState:UIControlStateNormal];
- [self.progressSlider setMinimumTrackImage:minImage forState:UIControlStateNormal];
- [self.progressSlider setThumbImage:[UIImage imageNamed:@"slider.png"] forState:UIControlStateNormal];
- [self setupViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
- _tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(controlsViewClicked:)];
- [self.controlsView addGestureRecognizer:_tapGestureRecognizer];
- [super viewDidLoad];
- }
- -(void) viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- if(_player.rate == 1.0){
- [_player pause];
- }
- [idleTimer invalidate];
- if(mTimeObserver){
- [_player removeTimeObserver:mTimeObserver];
- mTimeObserver = nil;
- }
- [_playerItem removeObserver:self forKeyPath:kStatusKeyT];
- [[NSNotificationCenter defaultCenter] removeObserver:self
- name:AVPlayerItemDidPlayToEndTimeNotification
- object:_playerItem];
- _player = nil;
- _playerItem = nil;
- idleTimer = nil;
- _tapGestureRecognizer = nil;
- }
- -(void) dealloc
- {
- NSLog(@"DEALLOCING");
- }
- -(void) hideControlsAnimated:(BOOL) animated
- {
- if (self.navigationController.navigationBar.hidden == NO)
- {
- // hide the Navigation Bar
- [self.navigationController setNavigationBarHidden:YES animated:YES];
- [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
- [UIView animateWithDuration:0.2f animations:
- ^{
- [self._controlsBar setTransform:CGAffineTransformMakeTranslation(0.f, CGRectGetHeight([self._controlsBar bounds]))];
- } completion:
- ^(BOOL finished)
- {
- [self._controlsBar setHidden:YES];
- }
- ];
- }
- }
- -(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
- [self setupViewsForOrientation:toInterfaceOrientation];
- }
- -(void) setupViewsForOrientation:(UIInterfaceOrientation) toInterfaceOrientation
- {
- if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
- UIButton *temp = [[UIButton alloc] initWithFrame:CGRectMake(0,0, 28, 28)];
- [temp setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
- [temp addTarget:self action:@selector(doneDidTouch:) forControlEvents:UIControlEventTouchUpInside];
- UIBarButtonItem *leftButtonTemp = [[UIBarButtonItem alloc] initWithCustomView:temp];
- self.navigationItem.leftBarButtonItem = leftButtonTemp;
- }
- else{
- UIButton *temp = [[UIButton alloc] initWithFrame:CGRectMake(0,0, 30, 30)];
- [temp setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
- [temp addTarget:self action:@selector(doneDidTouch:) forControlEvents:UIControlEventTouchUpInside];
- UIBarButtonItem *leftButtonTemp = [[UIBarButtonItem alloc] initWithCustomView:temp];
- self.navigationItem.leftBarButtonItem = leftButtonTemp;
- }
- }
- #pragma mark - Actions
- -(void) controlsViewClicked:(UIGestureRecognizer *)gestureRecognizer
- {
- CGPoint loc = [gestureRecognizer locationInView:self.controlsView];
- if([self.madnessView adClicked:loc]){
- }
- else{
- if (self.navigationController.navigationBar.hidden == YES)
- {
- // hide the Navigation Bar
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
- [self._controlsBar setHidden:NO];
- [UIView animateWithDuration:0.2f animations:
- ^{
- [self._controlsBar setTransform:CGAffineTransformMakeTranslation(0.f, CGRectGetHeight([self._controlsBar bounds])-50)];
- } completion:
- ^(BOOL finished)
- {
- }
- ];
- }
- else if (self.navigationController.navigationBar.hidden == NO)
- {
- // hide the Navigation Bar
- [self.navigationController setNavigationBarHidden:YES animated:YES];
- [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
- [UIView animateWithDuration:0.2f animations:
- ^{
- [self._controlsBar setTransform:CGAffineTransformMakeTranslation(0.f, CGRectGetHeight([self._controlsBar bounds]))];
- } completion:
- ^(BOOL finished)
- {
- [self._controlsBar setHidden:YES];
- }
- ];
- }
- }
- }
- - (void)doneDidTouch:(id)sender
- {
- [NSObject cancelPreviousPerformRequestsWithTarget:self];
- [_player pause];
- [[self navigationController] popToRootViewControllerAnimated:YES];
- }
- - (IBAction)playButtonPressed:(id)sender {
- /* If we are at the end of the movie, we must seek to the beginning first
- before starting playback. */
- if (YES == seekToZeroBeforePlay)
- {
- seekToZeroBeforePlay = NO;
- [_player seekToTime:kCMTimeZero];
- }
- [_player play];
- [self showStopButton];
- }
- - (IBAction)pauseButtonPressed:(id)sender {
- [_player pause];
- [self showPlayButton];
- }
- - (IBAction)zoomButtonPressed:(id)sender {
- if([[self.playbackView getVideoFillMode] isEqualToString:@"AVLayerVideoGravityResizeAspect"]){
- [self.playbackView setVideoFillMode:@"AVLayerVideoGravityResizeAspectFill"];
- [self.madnessView setVideoGravity:@"AVLayerVideoGravityResizeAspectFill"];
- [self.zoomButton setImage:[UIImage imageNamed:@"btn_zoomOut.png"] forState:UIControlStateNormal];
- }
- else{
- [self.playbackView setVideoFillMode:@"AVLayerVideoGravityResizeAspect"];
- [self.madnessView setVideoGravity:@"AVLayerVideoGravityResizeAspect"];
- [self.zoomButton setImage:[UIImage imageNamed:@"btn_zoomIn.png"] forState:UIControlStateNormal];
- }
- }
- - (void)progressSliderChanged:(id)sender
- {
- seekToZeroBeforePlay = NO;
- [self resetIdleTimer];
- float duration = CMTimeGetSeconds([_playerItem duration]);
- CMTime seekTo = CMTimeMakeWithSeconds(duration * _progressSlider.value, NSEC_PER_SEC);
- [_player seekToTime:seekTo];
- }
- - (void)forwardDidTouch:(id)sender
- {
- float current_time = CMTimeGetSeconds([_player currentTime]);
- CMTime seekTo = CMTimeMakeWithSeconds(current_time + 10, NSEC_PER_SEC);
- [_player seekToTime:seekTo];
- }
- - (void)rewindDidTouch:(id)sender
- {
- float current_time = CMTimeGetSeconds([_player currentTime]);
- CMTime seekTo = CMTimeMakeWithSeconds(current_time - 10, NSEC_PER_SEC);
- [_player seekToTime:seekTo];
- }
- #pragma mark Asset URL
- -(void) setURL:(NSURL*) URL
- {
- if (_URL != URL)
- {
- _URL = [URL copy];
- /*
- Create an asset for inspection of a resource referenced by a given URL.
- Load the values for the asset keys "tracks", "playable".
- */
- AVURLAsset *asset = [AVURLAsset URLAssetWithURL:_URL options:nil];
- NSArray *requestedKeys = [NSArray arrayWithObjects:kTracksKeyT, kPlayableKeyT, nil];
- /* Tells the asset to load the values of any of the specified keys that are not already loaded. */
- [asset loadValuesAsynchronouslyForKeys:requestedKeys completionHandler:
- ^{
- dispatch_async( dispatch_get_main_queue(),
- ^{
- /* IMPORTANT: Must dispatch to main queue in order to operate on the AVPlayer and AVPlayerItem. */
- [self prepareToPlayAsset:asset withKeys:requestedKeys];
- });
- }];
- }
- }
- - (NSURL*)URL
- {
- return _URL;
- }
- #pragma mark - ASSET PREPARE TO PLAY
- -(void) setPlayer:(AVPlayer*) player
- {
- _player = player;
- }
- /*
- Invoked at the completion of the loading of the values for all keys on the asset that we require.
- Checks whether loading was successfull and whether the asset is playable.
- If so, sets up an AVPlayerItem and an AVPlayer to play the asset.
- */
- - (void)prepareToPlayAsset:(AVURLAsset *)asset withKeys:(NSArray *)requestedKeys
- {
- /* Make sure that the value of each key has loaded successfully. */
- for (NSString *thisKey in requestedKeys)
- {
- NSError *error = nil;
- AVKeyValueStatus keyStatus = [asset statusOfValueForKey:thisKey error:&error];
- if (keyStatus == AVKeyValueStatusFailed)
- {
- [self assetFailedToPrepareForPlayback:error];
- return;
- }
- /* If you are also implementing -[AVAsset cancelLoading], add your code here to bail out properly in the case of cancellation. */
- }
- /* Use the AVAsset playable property to detect whether the asset can be played. */
- if (!asset.playable)
- {
- /* Generate an error describing the failure. */
- NSString *localizedDescription = NSLocalizedString(@"Item cannot be played", @"Item cannot be played description");
- NSString *localizedFailureReason = NSLocalizedString(@"The assets tracks were loaded, but could not be made playable.", @"Item cannot be played failure reason");
- NSDictionary *errorDict = [NSDictionary dictionaryWithObjectsAndKeys:
- localizedDescription, NSLocalizedDescriptionKey,
- localizedFailureReason, NSLocalizedFailureReasonErrorKey,
- nil];
- NSError *assetCannotBePlayedError = [NSError errorWithDomain:@"StitchedStreamPlayer" code:0 userInfo:errorDict];
- /* Display the error to the user. */
- [self assetFailedToPrepareForPlayback:assetCannotBePlayedError];
- return;
- }
- /* At this point we're ready to set up for playback of the asset. */
- /* Stop observing our prior AVPlayerItem, if we have one. */
- if (_playerItem)
- {
- /* Remove existing player item key value observers and notifications. */
- [_playerItem removeObserver:self forKeyPath:kStatusKeyT];
- [[NSNotificationCenter defaultCenter] removeObserver:self
- name:AVPlayerItemDidPlayToEndTimeNotification
- object:_playerItem];
- }
- /* Create a new instance of AVPlayerItem from the now successfully loaded AVAsset. */
- _playerItem = [AVPlayerItem playerItemWithAsset:asset];
- /* Observe the player item "status" key to determine when it is ready to play. */
- [_playerItem addObserver:self
- forKeyPath:kStatusKeyT
- options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
- context:AVPlayerDemoPlaybackViewControllerStatusObservationContext];
- /* When the player item has played to its end time we'll toggle
- the movie controller Pause button to be the Play button */
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(playerItemDidReachEnd:)
- name:AVPlayerItemDidPlayToEndTimeNotification
- object:_playerItem];
- //TODO
- seekToZeroBeforePlay = NO;
- /* Create new player, if we don't already have one. */
- if (!_player)
- {
- /* Get a new AVPlayer initialized to play the specified player item. */
- [self setPlayer:[AVPlayer playerWithPlayerItem:_playerItem]];
- /* Observe the AVPlayer "currentItem" property to find out when any
- AVPlayer replaceCurrentItemWithPlayerItem: replacement will/did
- occur.*/
- [_player addObserver:self
- forKeyPath:kCurrentItemKeyT
- options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
- context:AVPlayerDemoPlaybackViewControllerCurrentItemObservationContext];
- /* Observe the AVPlayer "rate" property to update the scrubber control. */
- [_player addObserver:self
- forKeyPath:kRateKeyT
- options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
- context:AVPlayerDemoPlaybackViewControllerRateObservationContext];
- }
- // AVPlayerLayer *playerLayer = [[AVPlayerLayer alloc] init];
- // [playerLayer setPlayer:self.player];
- // [playerLayer setFrame:self.mPlaybackView.layer.bounds];
- // [self.mPlaybackView.layer addSublayer:playerLayer];
- [self.playbackView.layer addObserver:self forKeyPath:kReadyForAdDisplayKeyT
- options:NSKeyValueObservingOptionInitial context:AVPlayerDemoPlaybackViewControllerCurrentItemObservationContextForDisplay];
- /* Make our new AVPlayerItem the AVPlayer's current item. */
- if (_player.currentItem != _playerItem)
- {
- /* Replace the player item with a new player item. The item replacement occurs
- asynchronously; observe the currentItem property to find out when the
- replacement will/did occur*/
- [_player replaceCurrentItemWithPlayerItem:_playerItem];
- [self syncPlayPauseButtons];
- }
- [self.progressSlider setValue:0.0];
- }
- #pragma mark -
- #pragma mark Loading the Asset Keys Asynchronously
- #pragma mark -
- #pragma mark Error Handling - Preparing Assets for Playback Failed
- /* --------------------------------------------------------------
- ** Called when an asset fails to prepare for playback for any of
- ** the following reasons:
- **
- ** 1) values of asset keys did not load successfully,
- ** 2) the asset keys did load successfully, but the asset is not
- ** playable
- ** 3) the item did not become ready to play.
- ** ----------------------------------------------------------- */
- -(void)assetFailedToPrepareForPlayback:(NSError *)error
- {
- [self removePlayerTimeObserver];
- [self syncScrubber];
- [self disableScrubber];
- [self disablePlayerButtons];
- /* Display the error. */
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[error localizedDescription]
- message:[error localizedFailureReason]
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alertView show];
- }
- #pragma mark Player Item
- - (BOOL)isPlaying
- {
- return mRestoreAfterScrubbingRate != 0.f || [_player rate] != 0.f;
- }
- /* Called when the player item has played to its end time. */
- - (void)playerItemDidReachEnd:(NSNotification *)notification
- {
- /* After the movie has played to its end time, seek back to time zero
- to play it again. */
- seekToZeroBeforePlay = YES;
- }
- /* ---------------------------------------------------------
- ** Get the duration for a AVPlayerItem.
- ** ------------------------------------------------------- */
- - (CMTime)playerItemDuration
- {
- AVPlayerItem *playerItem = [_player currentItem];
- if (playerItem.status == AVPlayerItemStatusReadyToPlay)
- {
- /*
- NOTE:
- Because of the dynamic nature of HTTP Live Streaming Media, the best practice
- for obtaining the duration of an AVPlayerItem object has changed in iOS 4.3.
- Prior to iOS 4.3, you would obtain the duration of a player item by fetching
- the value of the duration property of its associated AVAsset object. However,
- note that for HTTP Live Streaming Media the duration of a player item during
- any particular playback session may differ from the duration of its asset. For
- this reason a new key-value observable duration property has been defined on
- AVPlayerItem.
- See the AV Foundation Release Notes for iOS 4.3 for more information.
- */
- return([playerItem duration]);
- }
- return(kCMTimeInvalid);
- }
- /* Cancels the previously registered time observer. */
- -(void)removePlayerTimeObserver
- {
- if (mTimeObserver)
- {
- [_player removeTimeObserver:mTimeObserver];
- mTimeObserver = nil;
- }
- }
- #pragma mark - Movie scrubber control
- /* ---------------------------------------------------------
- ** Methods to handle manipulation of the movie scrubber control
- ** ------------------------------------------------------- */
- /* Requests invocation of a given block during media playback to update the movie scrubber control. */
- -(void)initScrubberTimer
- {
- double interval = .1f;
- NSLog(@"Seconds: %f", CMTimeGetSeconds([self playerItemDuration]));
- CMTime playerDuration = [self playerItemDuration];
- if (CMTIME_IS_INVALID(playerDuration))
- {
- return;
- }
- double duration = CMTimeGetSeconds(playerDuration);
- if (isfinite(duration))
- {
- CGFloat width = CGRectGetWidth([self.progressSlider bounds]);
- interval = 0.5f * duration / width;
- }
- __weak typeof(self) weakSelf = self;
- /* Update the scrubber during normal playback. */
- mTimeObserver = [_player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(interval, NSEC_PER_SEC)
- queue:NULL /* If you pass NULL, the main queue is used. */
- usingBlock:^(CMTime time)
- {
- __strong typeof(self) strongSelf = weakSelf;
- [strongSelf syncScrubber];
- }];
- }
- /* Set the scrubber based on the player current time. */
- - (void)syncScrubber
- {
- CMTime playerDuration = [self playerItemDuration];
- if (CMTIME_IS_INVALID(playerDuration))
- {
- _progressSlider.minimumValue = 0.0;
- return;
- }
- double duration = CMTimeGetSeconds(playerDuration);
- if (isfinite(duration))
- {
- float minValue = [self.progressSlider minimumValue];
- float maxValue = [self.progressSlider maximumValue];
- double time = CMTimeGetSeconds([_player currentTime]);
- [self.progressSlider setValue:(maxValue - minValue) * time / duration + minValue];
- }
- }
- -(void)enableScrubber
- {
- self.progressSlider.enabled = YES;
- }
- -(void)disableScrubber
- {
- self.progressSlider.enabled = NO;
- }
- #pragma mark -
- #pragma mark Asset Key Value Observing
- #pragma mark
- #pragma mark Key Value Observer for player rate, currentItem, player item status
- /* ---------------------------------------------------------
- ** Called when the value at the specified key path relative
- ** to the given object has changed.
- ** Adjust the movie play and pause button controls when the
- ** player item "status" value changes. Update the movie
- ** scrubber control when the player item is ready to play.
- ** Adjust the movie scrubber control when the player item
- ** "rate" value changes. For updates of the player
- ** "currentItem" property, set the AVPlayer for which the
- ** player layer displays visual output.
- ** NOTE: this method is invoked on the main queue.
- ** ------------------------------------------------------- */
- - (void)observeValueForKeyPath:(NSString*) path
- ofObject:(id)object
- change:(NSDictionary*)change
- context:(void*)context
- {
- /* AVPlayerItem "status" property value observer. */
- if (context == AVPlayerDemoPlaybackViewControllerStatusObservationContext)
- {
- [self syncPlayPauseButtons];
- AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
- switch (status)
- {
- /* Indicates that the status of the player is not yet known because
- it has not tried to load new media resources for playback */
- case AVPlayerStatusUnknown:
- {
- [self removePlayerTimeObserver];
- [self syncScrubber];
- [self disableScrubber];
- [self disablePlayerButtons];
- }
- break;
- case AVPlayerStatusReadyToPlay:
- {
- /* Once the AVPlayerItem becomes ready to play, i.e.
- [playerItem status] == AVPlayerItemStatusReadyToPlay,
- its duration can be fetched from the item. */
- [self initScrubberTimer];
- [self enableScrubber];
- [self enablePlayerButtons];
- }
- break;
- case AVPlayerStatusFailed:
- {
- AVPlayerItem *playerItem = (AVPlayerItem *)object;
- [self assetFailedToPrepareForPlayback:playerItem.error];
- }
- break;
- }
- }
- /* AVPlayer "rate" property value observer. */
- else if (context == AVPlayerDemoPlaybackViewControllerRateObservationContext)
- {
- [self syncPlayPauseButtons];
- }
- /* AVPlayer "currentItem" property observer.
- Called when the AVPlayer replaceCurrentItemWithPlayerItem:
- replacement will/did occur. */
- else if (context == AVPlayerDemoPlaybackViewControllerCurrentItemObservationContext)
- {
- AVPlayerItem *newPlayerItem = [change objectForKey:NSKeyValueChangeNewKey];
- /* Is the new player item null? */
- if (newPlayerItem == (id)[NSNull null])
- {
- [self disablePlayerButtons];
- [self disableScrubber];
- }
- else /* Replacement of player currentItem has occurred */
- {
- /* Set the AVPlayer for which the player layer displays visual output. */
- [self.playbackView setPlayer:_player];
- /* Specifies that the player should preserve the video’s aspect ratio and
- fit the video within the layer’s bounds. */
- [self.playbackView setVideoFillMode:AVLayerVideoGravityResizeAspect];
- [self syncPlayPauseButtons];
- }
- }
- else if( context == AVPlayerDemoPlaybackViewControllerCurrentItemObservationContextForDisplay)
- {
- AVPlayerLayer *layer = (AVPlayerLayer*) object;
- if (layer.readyForDisplay){
- [layer removeObserver:self forKeyPath:kReadyForAdDisplayKeyT];
- AVPlayerItem *item = _player.currentItem;
- AVSynchronizedLayer *syncedLayer = [AVSynchronizedLayer synchronizedLayerWithPlayerItem:item];
- syncedLayer.frame = CGRectMake(0, 0, 568, 320);
- syncedLayer.backgroundColor = [[UIColor clearColor] CGColor];
- [self.madnessView addSyncedLayer:syncedLayer player:[self.playbackView player] withGravity:layer.videoGravity];
- }
- }
- else
- {
- [super observeValueForKeyPath:path ofObject:object change:change context:context];
- }
- }
- #pragma mark -
- #pragma mark Play, Stop buttons
- /* Show the stop button in the movie player controller. */
- -(void)showStopButton
- {
- self.playButton.hidden = YES;
- self.pauseButton.hidden = NO;
- }
- /* Show the play button in the movie player controller. */
- -(void)showPlayButton
- {
- self.pauseButton.hidden = YES;
- self.playButton.hidden = NO;
- }
- /* If the media is playing, show the stop button; otherwise, show the play button. */
- - (void)syncPlayPauseButtons
- {
- if ([self isPlaying])
- {
- [self showStopButton];
- }
- else
- {
- [self showPlayButton];
- }
- }
- -(void)enablePlayerButtons
- {
- _playButton.enabled = YES;
- _pauseButton.enabled = YES;
- }
- -(void)disablePlayerButtons
- {
- _playButton.enabled = NO;
- _pauseButton.enabled = NO;
- }
- #pragma mark -
- #pragma mark Handling idle timeout
- - (void)resetIdleTimer {
- if (!idleTimer) {
- idleTimer = [NSTimer scheduledTimerWithTimeInterval:kMaxIdleTimeSeconds
- target:self
- selector:@selector(idleTimerExceeded)
- userInfo:nil
- repeats:NO];
- }
- else {
- if (fabs([idleTimer.fireDate timeIntervalSinceNow]) < kMaxIdleTimeSeconds-1.0) {
- [idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:kMaxIdleTimeSeconds]];
- }
- }
- }
- - (void)idleTimerExceeded {
- idleTimer = nil;
- [self hideControlsAnimated:YES];
- [self resetIdleTimer];
- }
- - (UIResponder *)nextResponder {
- [self resetIdleTimer];
- return [super nextResponder];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement