Control position of video (MPMoviePlayerController) inside a view

孤人 提交于 2020-01-16 00:43:21

问题


I'm trying to position a view (with an inline video) but I'm a little stuck.

I've got the video working and playing well (inside the a view). I'm using storyboards.

I've already set the scaling mode to MPMovieScalingModeAspectFill which gives me my great full screen cropped view. Just to clarify - I WANT it to crop, it's not a mistake, but I want it left aligned and not centred.

My code is:

VideoViewController.h

@interface VideoViewController : UIViewController

@property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
@property (weak, nonatomic) IBOutlet UIView *videoView;

@end

VideoViewController.m

@implementation VideoViewController

@synthesize moviePlayer;
@synthesize videoView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Set parent view to be larger (let's us offset video later on)
    CGRect newSize = CGRectMake(0,0, 960, 568);
    [videoView setBounds:newSize];

    NSURL *theurl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"]];

    moviePlayer =  [[MPMoviePlayerController alloc] initWithContentURL:theurl];
    moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
    //Set to parent bounds
    [moviePlayer.view setFrame: videoView.bounds];
    [videoView addSubview:moviePlayer.view];

    //Offset video inside view (as required)
    moviePlayer.view.frame = CGRectOffset( moviePlayer.view.frame, -320, 0 );

    //Play video
    [moviePlayer prepareToPlay];
    [moviePlayer play];
}

@end

And this is a mockup of what I want versus what the code above is producing.

Any advice would be appreciated, I'm am an Objective C newb! :)

EDIT: Updated code to demonstrate how I have achieved this now using a larger parent view and CGRectOffset

来源:https://stackoverflow.com/questions/22702287/control-position-of-video-mpmovieplayercontroller-inside-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!