在 iOS 7 横向模式下播放视频

Posted

技术标签:

【中文标题】在 iOS 7 横向模式下播放视频【英文标题】:Play the video in Landscape mode iOS 7 【发布时间】:2014-06-11 07:13:54 【问题描述】:

在我的应用程序中,我正在尝试使用来自服务器的 URL 播放视频。我正在使用UITableView 显示视频列表,并通过点击列表中的单元格,视频将在子视图中播放。现在我想以横向模式播放视频。

这是当前的视频代码。

    _movieplayer = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:[self urlencode:self.strPlayUrl]]];
    [[_movieplayer view] setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [self.view addSubview: [_movieplayer view]];
    [_movieplayer setShouldAutoplay:YES];
    [_movieplayer prepareToPlay];

    [self.movieplayer play];

在上面的代码中,我如何让它在横向模式下运行。请指导我。纠结这个很久了。

【问题讨论】:

好问题。也有同样的问题。你得到你的解决方案了吗? 【参考方案1】:

使用电影播放器​​将其添加到您的视图控制器

@property (nonatomic, strong) MPMoviePlayerController* mpc;

- (void)setUpMPC

    NSURL* m = [[NSBundle mainBundle] URLForResource:@"YourVideo" withExtension:@"mp4"];
    MPMoviePlayerController* mp = [[MPMoviePlayerController alloc] initWithContentURL:m];
    self.mpc = mp; // retain policy
    self.mpc.shouldAutoplay = NO;
    [self.mpc prepareToPlay];
    self.mpc.view.frame = CGRectMake(50, 50, self.view.bounds.size.width, self.view.bounds.size.height);


-(NSUInteger)supportedInterfaceOrientations 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) 
        return UIInterfaceOrientationMaskAll;
    
    return UIInterfaceOrientationMaskLandscape;

【讨论】:

您的应用是否允许横向显示? 在常规->部署信息->勾选横向 是的,它将允许检查横向方向 请告诉我现在必须做的事情 我更改了解决方案,我认为这与您将电影添加为子视图有关。新解决方案向您展示了如何添加电影播放器​​。【参考方案2】:

也许你应该在你的 viewController 中实现 shouldAutorotateToInterfaceOrientationsupportedInterfaceOrientations 方法

@property (nonatomic, strong) MPMoviePlayerController*  moviePlayerController;

#pragma mark # - Init -

- (void) createAndPlayMovieForURL: (NSURL*) movieURL

    self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
    [self.moviePlayerController.view setFrame: self.view.bounds];

    [self.view addSubview: self.moviePlayerController.view];
    [self.view bringSubviewToFront: self.overlayView];


#pragma mark - Rotation -

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
 
    return YES;


- (NSUInteger) supportedInterfaceOrientations

    return UIInterfaceOrientationMaskAllButUpsideDown;

【讨论】:

您检查过shouldAutorotateToInterfaceOrientationsupportedInterfaceOrientations 是否曾经调用过? 告诉我怎么称呼它 linksupportedInterfaceOrientations. When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. Is only called if the view controller’s shouldAutorotate method returns YES.

以上是关于在 iOS 7 横向模式下播放视频的主要内容,如果未能解决你的问题,请参考以下文章

iphone - 强制 MPMoviePlayerController 以横向模式播放视频

iOS:在 MPMoviePlayerController 中将设备方向更改为横向时如何全屏播放视频?

我的 iPhone 视频仅在纵向模式下播放

在 ios6 和 ios7 的 MPMoviePlayerViewController 中将横向旋转为纵向

Android TV 中的 ExoPlayer 以纵向模式而不是横向模式播放视频

横向和纵向播放视频