1. 程式人生 > >iOS 視訊播放實現:AVPlayerViewController

iOS 視訊播放實現:AVPlayerViewController

直接上程式碼

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view addSubview:self.moviePlayer.view];

    [self.moviePlayer.player play];

}

-(AVPlayerViewController *)moviePlayer{

    if (!_moviePlayer) {

        NSURL * url = [NSURL URLWithString:@"http://v.cctv.com/flash/mp4video6/TMS/2011/01/05/cf752b1c12ce452b3040cab2f90bc265_h264818000nero_aac32-1.mp4"];

        _moviePlayer=[[AVPlayerViewController alloc]init];

        _moviePlayer.player = [AVPlayer playerWithURL:url];

        _moviePlayer.view.frame=self.view.bounds;

        _moviePlayer.view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

        _moviePlayer.showsPlaybackControls = YES;

        [self.view addSubview:_moviePlayer.view];

    }

    return _moviePlayer;

}

iOS9.0後要求請求都要使用https,demo中的視訊是http的,需要再info.plist檔案中加入:App Transport Security Settings(字典),在裡面加入Allow Arbitrary Loads(設定為YES)

重新執行,可以播放視訊了.