0

我正在尝试缩放 youtube 视频,使其不失真,但也没有黑条填充视图。我可以通过裁剪边缘的视频来完成此操作,因此 MPMovieScalingModeAspectFill 似乎是我的最佳选择。此外,此视图包含在另一个视图中。

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

我的具体问题包括:

  • 是否可以让 MPMovieScalingModeAspectFill 裁剪我的视频,同时使用约束定义其 UI?
  • 如果可能的话,我在代码中做错了什么?

下面是我的 .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];
}
4

2 回答 2

3

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

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

.
. /* set constraints */
.

[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
于 2013-11-08T21:10:00.073 回答
2

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

videoPlayerView.clipsToBounds = YES;
[moviePlayerController setScalingMode:MPMovieScalingModeAspectFill];
于 2013-07-16T14:15:42.690 回答