PeterWong
PeterWong

Reputation: 16011

Problem on overlay on top of a MPMoviePlauerViewController

I have a movie player view controller which i want to add a overlay view on top of it.

I used the following code:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  if (self) {
      // Custom initialization
      [[moviePlayer view] addSubview:[self overlayControlsView]];

      UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
      [[self view] addGestureRecognizer:tap];
  }
  return self;
}

overlayControlsView is currently only a UILabel.

- (void)handleTap:(UITapGestureRecognizer *)sender {
    NSLog(@"Tap handled");
    if ([sender state] == UIGestureRecognizerStateEnded) {
        if ([[self overlayControlsView] isHidden]) {
            [[self overlayControlsView] setHidden:NO];
        } else {
            [[self overlayControlsView] setHidden:YES];
        }
    }
}

When the movieplayer is just displayed, tapping on it causes the overlayControlsView shows and hides successfully.

However, right after the moviewplayer load state changed and starts to play (I load the video from the Internet, and so there's some time gap here), the handleTap method was not called anymore.

Does anyone has any idea on it? Is there a better way to add overlay controls?

Upvotes: 1

Views: 671

Answers (1)

illuminatus
illuminatus

Reputation: 2086

I dont know whats wrong with your code; here is an alternative of accomplishing overlay control using custom button

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *urlString = [[NSBundle mainBundle] pathForResource:@"sample-video" ofType:@"mov"];
    player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:urlString]];
    UIButton *overlayView = [UIButton buttonWithType:UIButtonTypeCustom];
    [overlayView setTitle:@"" forState:UIControlStateNormal];
    [overlayView setTitle:@"Play" forState:UIControlStateSelected];
    [overlayView addTarget:self action:@selector(handleTap:) forControlEvents:UIControlEventTouchUpInside];
    player.view.frame = CGRectMake(50, 50, 200, 250);
    overlayView.frame = player.view.frame;
    [player.view addSubview:overlayView];
    player.controlStyle = MPMovieControlStyleNone;
    [self.view addSubview:player.view];
     }


- (void)handleTap:(id *)sender {
    UIButton *btn = (UIButton *)sender;
    if (btn.selected) {
        [player play];
        [btn setSelected:NO];
    }
    else {
        [player pause];
        [btn setSelected:YES];
    } }

Upvotes: 1

Related Questions