如何在AVPlayer中默认以横向模式播放视频?

时间:2018-05-29 13:41:52

标签: ios objective-c iphone avplayer

- (void)playMethod
{
    NSURL *url = [[NSURL alloc] initWithString:@"https://www.rmp- 
    streaming.com/media///bbb-360p.mp4"];
    player = [AVPlayer playerWithURL:url];
    controller = [[AVPlayerViewController alloc] init];
    [self addChildViewController:controller];
    [self.view addSubview:controller.view];

    controller.view.frame = CGRectMake(15,50,345,300);
    controller.player = player;
    controller.showsPlaybackControls = YES;
    player.closedCaptionDisplayEnabled = NO;

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 addTarget:self
                action:@selector(backMethod)
      forControlEvents:UIControlEventTouchUpInside];
    [button1 setTitle:@"Back" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
     button1.frame = CGRectMake(50.0, CGRectGetMinY(controller.view.frame)+10, 160.0, 40.0);
    button1.tag = 1001;
    [self.view addSubview:button1];
    [controller.view  bringSubviewToFront:button1];
    [player pause];
    [player play];
    }

我想以横向模式播放视频,它以纵向模式播放。我想在调用playMethod时它应该只以横向模式打开视频。

1 个答案:

答案 0 :(得分:0)

创建自定义类,该类继承自AVPlayerViewController并使用它而不是基础AVPlayerViewController

import AVKit

class CustomAVPlayerViewController: AVPlayerViewController {

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeLeft
    }
}