控制视图内的视频位置(MPMoviePlayerController)

时间:2014-03-28 00:59:55

标签: ios iphone objective-c ios7

我正在尝试定位视图(带有内嵌视频),但我有点卡住了。

我让视频正常工作并且播放得很好(在视图中)。我正在使用故事板。

我已经将缩放模式设置为MPMovieScalingModeAspectFill,这为我提供了全屏切换视图。只是为了澄清 - 我想要裁剪,这不是一个错误,但我希望它保持对齐而不是居中。

我的代码是:

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

这是我想要的模型与上面的代码产生的模型。

任何建议都将不胜感激,我是一个Objective C newb! :)

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

编辑:更新了代码以演示我现在如何使用更大的父视图和CGRectOffset

实现此目的

0 个答案:

没有答案