以横向模式将影片加载到iPhone中

时间:2011-11-02 08:34:37

标签: iphone objective-c

我是Objective-C编码的新手,所以请原谅这个愚蠢的问题。我必须做一些非常简单的事情:我加载一个本地视频,当它停止播放时,它会从头开始。

所以,首先,我想知道我使用的代码是否正确(即:如果它可以生成内存泄漏,或者根据我的基本经验有例如,我应该考虑,但我没有。)

另一件事是如何让我的视频以横向模式覆盖整个屏幕。如果我在模拟器中选择UIInterfaceOrientationLandscapeLeft(或Right),我会在左下角的矩形中看到我的视频(然后我会看到两个区域,一个灰色区域和一个黑色区域)。

我删除了评论,因为它们不是英文。

- (void)viewDidLoad
{
    [self startPlayingVideo];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) startPlayingVideo{
    //
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *urlAsString = [mainBundle pathForResource:@"space" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:urlAsString];
    //
    MPMoviePlayerController *newMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    //
    self.moviePlayer = newMoviePlayer; 
    //
    [newMoviePlayer release]; 
    //
    [self.view addSubview:self.moviePlayer.view];
    //
    [self.moviePlayer.view setFrame: self.view.bounds];
    //
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoHasFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
    //
    self.moviePlayer.controlStyle = MPMovieControlStyleNone;
    //
    // play
    [self.moviePlayer play];
    //
    // fullscreen
    [self.moviePlayer setFullscreen:YES animated:YES];
}

-(void)videoHasFinishedPlaying:(NSNotification*)paramNotification{
    //
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
    //
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoHasFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
    self.moviePlayer.initialPlaybackTime = -1.0;
    [self.moviePlayer play];
    //
}

非常感谢您的任何建议。

[编辑] 我发现我可以对此行进行硬编码[self.moviePlayer.view setFrame: self.view.bounds]并将其更改为[self.moviePlayer.view setFrame: CGRectMake(0, 0, 640,360)],以匹配电影尺寸和iPhone屏幕尺寸。

看起来self.view.bounds不会返回640x360,而是460x320:这些是XIB维度(似乎无法通过XCode Inspector更改)。与UIScreen相同的问题。通过向return UIInterfaceOrientationIsLandscape(interfaceOrientation);方法添加shouldAutoRotateToInterfaceOrientation:interfaceOrientation:,我可以按预期工作,但仍然是硬编码维度的问题

与此同时,我很容易摆脱NSNotificationCenter(我发现了repeatMode类的属性MPMoviePlayerController

[编辑2] 最后,通过以这种方式反转W和H,任何事情似乎都能正常工作:[self.moviePlayer.view setFrame: CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width)]并添加newMoviePlayer.scalingMode = MPMovieScalingModeAspectFill。我不知道这是否是最好的方法,但是......

0 个答案:

没有答案