使用MPMoviePlayerController和MP MovieScalingMode AspectFill和Constraints

时间:2013-07-15 22:09:09

标签: ipad constraints mpmovieplayercontroller scaling

我正在尝试缩放YouTube视频,使其不会失真,但也会填充没有黑条的视图。我很好用带有裁剪边缘的视频来实现这一点,所以看起来MPMovieScalingModeAspectFill是我的最佳选择。此外,此视图包含在另一个视图中。

但是,我正在做的项目要求使用约束代码来配置所有UI。我不能使用nib文件或故事板。我发现很难让边缘实际裁剪(它显示视频被炸毁以适应视图的顶部和底部,水平部分溢出我的视图),我认为这可能与我的使用有关约束与使用CGRect定义实际框架。

我的具体问题包括:

  • 是否有可能让MPMovieScalingModeAspectFill裁剪我的视频定义其受限制的用户界面?
  • 如果可能的话,我在代码中做错了什么?

下面是我的.m文件示例,其中包含创建视频播放器视图的部分方法:

- (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];
}

2 个答案:

答案 0 :(得分:3)

感谢您提问和回答。但以下代码实际上对我有用:

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

.
. /* set constraints */
.

[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];

答案 1 :(得分:2)

答案很简单,我需要将我的视图“clipToBounds”设置为true,同时还将setScalingMode:MPMovieScalingModeAspectFill与我的电影播放器​​合并:

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