iOS AVPlayer

1.使用AVPlayer播放在线视频,可自己定制播放器

//1申明url

 NSURL * playUrl = [NSURL URLWithString:@"http://video.zzyzsw.com/4c4312dab5170b78/6b7c7150a0689152.flv.m3u8"];

//2.创建playitem

AVPlayerItem * playItem = [AVPlayerItem playerItemWithURL:playUrl];

//申明播放器

AVPlayer * player = [AVPlayer playerWithPlayerItem:playerItem];

//申明播放的视图位置

UIView * videoView= [[UIView alloc] initWithFrame:self.view.bounds];

//创建AVPlayerLayer

AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

//创建playerLayer的大小

playerLayer.frame = self.videoView.bounds;

//播放视图添加layer

[videoView.layer addSublayer:playerLayer];

[self.view addSubview:videoView];

//播放

[player play];


2使用系统AVPlayerViewController播放在线播放器,其实还是用到AVPlayerLayer,但是不能定制播放界面,需要导入#import 

//1设置url

NSURL * playUrl = [NSURL URLWithString:@"http://video.zzyzsw.com/4c4312dab5170b78/6b7c7150a0689152.flv.m3u8"];

//2设置playerItem

AVPlayerItem * playerItem= [AVPlayerItem playerItemWithURL:playUrl];

    //如果要切换视频需要调AVPlayer的replaceCurrentItemWithPlayerItem:方法

   AVPlayer * player = [AVPlayer playerWithPlayerItem:self.playerItem];

    AVPlayerViewController * playerViewController = [[AVPlayerViewController alloc] init];

    playerViewController.player=self.player;

    playerViewController.videoGravity = AVLayerVideoGravityResizeAspect;

    playerViewController.view.frame=self.view.bounds;

    playerViewController.showsPlaybackControls=YES;

    [self addChildViewController:playerViewController];

    [self.view addSubview:playerViewController.view];

    [playerViewController.player play];

你可能感兴趣的:(iOS AVPlayer)