Search code examples
iosobjective-cxcode6custom-controlsmpmovieplayercontroller

How to create a button as a subview of MPMoviePlayerController?


I am trying to add a custom play button but can't put it in front of the video. here is my code.

- (IBAction)playMovie:(UIButton *)sender {


    NSURL *url = [[NSURL alloc] initWithString:urlStr.text];
    self.movie = [[MPMoviePlayerController alloc]initWithContentURL:url];
    self.movie.controlStyle = MPMovieControlStyleNone;


    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
    button.backgroundColor=[UIColor yellowColor];
    [button setTitle:@"Normal State Title" forState:UIControlStateNormal];

    [movie.view addSubview:button];

    self.movie.shouldAutoplay = YES;
    [self.view addSubview:self.movie.view];
    [self.movie setFullscreen:YES animated:NO];
}

Solution

  • I think this is what you are looking for. Here is simple example.

    - (void)viewDidLoad 
    {
       [super viewDidLoad];
       // Do any additional setup after loading the view, typically from a nib.
    
       url=[NSURL URLWithString:@"https://scontent.cdninstagram.com/hphotos-xaf1/t50.2886-16/11179443_819874424728492_389701720_n.mp4"];
    
       movPlayer=[[MPMoviePlayerController alloc] init];
       [movPlayer setContentURL:url];
       [movPlayer setMovieSourceType:MPMovieSourceTypeFile];
       [movPlayer.view setFrame:CGRectMake(_imgVw.frame.origin.x, _imgVw.frame.origin.y, _imgVw.frame.size.width, _imgVw.frame.size.height)];
       [movPlayer prepareToPlay];
       movPlayer.controlStyle = MPMovieControlStyleNone;
       movPlayer.fullscreen = NO;
       movPlayer.shouldAutoplay=NO;
       [movPlayer setScalingMode:MPMovieScalingModeAspectFill];
       [self.view addSubview:movPlayer.view];
    
    
       UIButton *pauseButton=[[UIButton alloc]init];
       [pauseButton setFrame:CGRectMake(100,220, 50, 50)];
       [pauseButton setBackgroundColor:[UIColor yellowColor]];
       [pauseButton setTag:123456789];
       [pauseButton setAccessibilityValue:@"445"];
       [pauseButton setAccessibilityLabel:@"445"];
       [pauseButton addTarget:self action:@selector(pauseBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
       [self.view addSubview:pauseButton];
    
    
    }
    
    -(void)pauseBtnClicked:(UIButton*)btn
    {
        NSLog(@"clicked on pause button");
    
        UIButton *pause = (UIButton*)[self.view viewWithTag:123456789];
    
        NSLog(@"clicked on pause button");
    
       if ([pause.accessibilityValue intValue] == 455)
       {
           NSLog(@"Stop");
           [movPlayer stop];
           [pause setBackgroundColor:[UIColor yellowColor]];
           [pause setAccessibilityValue:@"445"];
    
       }
       else if ([pause.accessibilityValue intValue] == 445)
       {
           NSLog(@"Play");
    
           [movPlayer play];
    
           [pause setBackgroundColor:[UIColor brownColor]];
           [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlaybackComplete:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:movPlayer];
           [pause setAccessibilityValue:@"455"];
    
        }
    }
    
    
    - (void)moviePlaybackComplete:(NSNotification *)notification
    {
    
        NSLog(@"video playing is completed");
        MPMoviePlayerController *moviePlayerController = (MPMoviePlayerController*)[notification object];
    
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayerController];
    
        [moviePlayerController stop];
    
        int pauseViewTag = 123456789;
        UIButton *pause = (UIButton*)[self.view viewWithTag:pauseViewTag];
    
        [pause setBackgroundColor:[UIColor yellowColor]];
    
    
    
    }