bmjohns
bmjohns

Reputation: 6470

Using MPMoviePlayerController with MPMovieScalingModeAspectFill and Contraints

I am attempting to scale a youtube video so that it is not distorted, but also fills the view with no black bars. I am ok with the video having cropped edges to accomplish this, so it would seem MPMovieScalingModeAspectFill is my best option. Also, this view is contained in another view.

However, the project I am doing requires all UI to be configured using constraints in-code. I can not use nib files or storyboard. I am finding it hard to get the edges to actually crop (it shows the video blown up to fit the top and bottom of the view with the horizontal part spilling out of my view) , and I assume this might have to do with my use of constraints versus using CGRect to define an actual frame.

My specific questions include:

Below is an example of my .m file with parts of the method that creates the video player view:

- (void) createMediaPlayer: (NSURL *)videoURL {

    YouTubeVideo *selectedVideo = [youTubeCollection.items objectAtIndex:youTubeCollection.currentItem];

    moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:selectedVideo.contentsURL]];

    [moviePlayerController setContentURL:videoURL];
    [moviePlayerController setShouldAutoplay:YES];
    [moviePlayerController setRepeatMode:MPMovieRepeatModeNone];
    [moviePlayerController setFullscreen:NO];
    [moviePlayerController setControlStyle:MPMovieControlStyleNone];
    [moviePlayerController setScalingMode:MPMovieScalingModeAspectFill];

    playingVideo = YES;

    moviePlayerController.view.translatesAutoresizingMaskIntoConstraints = NO;
    [videoPlayerView addSubview:moviePlayerController.view];
    [videoPlayerView sendSubviewToBack:videoPlayerView];


    // Configure Constraints
    [videoPlayerView addConstraint:[NSLayoutConstraint constraintWithItem:moviePlayerController.view
                                                            attribute:NSLayoutAttributeTop
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:videoPlayerView
                                                            attribute:NSLayoutAttributeTop
                                                           multiplier:1.0
                                                             constant:0.0]];
    [videoPlayerView addConstraint:[NSLayoutConstraint constraintWithItem:moviePlayerController.view
                                                            attribute:NSLayoutAttributeBottom
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:videoPlayerView
                                                            attribute:NSLayoutAttributeBottom
                                                           multiplier:1.0
                                                             constant:0.0]];
    [videoPlayerView addConstraint:[NSLayoutConstraint constraintWithItem:moviePlayerController.view
                                                            attribute:NSLayoutAttributeLeft
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:videoPlayerView
                                                            attribute:NSLayoutAttributeLeft
                                                           multiplier:1.0
                                                             constant:0.0]];
    [videoPlayerView addConstraint:[NSLayoutConstraint constraintWithItem:moviePlayerController.view
                                                            attribute:NSLayoutAttributeRight
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:videoPlayerView
                                                            attribute:NSLayoutAttributeRight
                                                           multiplier:1.0
                                                             constant:0.0]];

    [moviePlayerController prepareToPlay];

    [moviePlayerController play];
}

Upvotes: 0

Views: 1771

Answers (2)

Roozbeh Zabihollahi
Roozbeh Zabihollahi

Reputation: 7347

Thanks for both question and answer. But the following code actually worked for me:

moviePlayer = [[MPMoviePlayerController alloc] init];
[moviePlayer.view setFrame: _viewPlayer.bounds];
[moviePlayer.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[_viewPlayer addSubview:moviePlayer.view];

.
. /* set constraints */
.

[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];

Upvotes: 3

bmjohns
bmjohns

Reputation: 6470

The answer was rather simple, I needed to set my views "clipToBounds" to true while also incorporating setScalingMode:MPMovieScalingModeAspectFill with my movie player :

videoPlayerView.clipsToBounds = YES;
[moviePlayerController setScalingMode:MPMovieScalingModeAspectFill];

Upvotes: 2

Related Questions