1

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! :)

Illustration to demo what I want to achieve vs. what my app is doing now

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

Pete
  • 895
  • 1
  • 6
  • 13
  • 2
    I think all you can do is make the video view extra wide. For example, change the width of `videoView` to 960 in storyboard, but leave the `origin.x` value at 0. – user3386109 Mar 28 '14 at 01:15
  • Thanks for the tip, trying it out now. Assuming though that I still wanted to control th offset somewhat... E.G. instead of starting it at 0,0 like the picture shows, I wanted to offset it by 100 points. Would the best way be to use `CGRectOffset`? – Pete Mar 28 '14 at 03:54
  • That depends on whether you have autolayout enabled. If autolayout is enabled, you would have to change the leading space constraint. Otherwise, yes, CGRectOffset is a good choice to change the frame of the videoView (be sure to use a negative dx :)) – user3386109 Mar 28 '14 at 04:19
  • Great thanks. +1 for pointing me in the right direction with setting `videoView` to 960. GRectOffset works well (with negative values). Also I updated my question to include the answer and how I put it together in the end. – Pete Mar 28 '14 at 04:39
  • Would be nice to let @user3386109 actually write an answer so you can accept it. – matt Mar 28 '14 at 04:46
  • @matt I didn't prevent him from posting an answer. I voted the comment, and will definitely accept it if it's posted as an answer – Pete Apr 02 '14 at 00:23
  • 1
    @PeterVahaviolos What I mean was, we should _ask_ him to write an answer. I thought his comment was an excellent solution. :) – matt Apr 02 '14 at 00:34

0 Answers0