视频直播的简单实现

最近要做路况大数据,播放摄像头的视频直播,学了两个简单的demo

import

1.AVPlayer

NSURL*liveURL = [NSURL URLWithString:@"http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8"]; //这个地址是抓的战旗的直播地址,假如不能用的话可以自己抓一下战旗的videoID="18620_aVSpe",随便找一个在直播的房间抓就可以.
    
    AVAsset *liveAsset = [AVURLAsset URLAssetWithURL:liveURL options:nil];
    
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:liveAsset];
    
    AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
    
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    
    playerLayer.frame=self.view.layer.bounds;
    
    playerLayer.videoGravity=AVLayerVideoGravityResizeAspect;
    
    [self.view.layer addSublayer:playerLayer];
    
    [player play];
  1. AVPlayerViewController这个可以实现全屏,有控制按钮
 NSURL*url = [NSURL URLWithString:@"http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8"];
    
    AVPlayerViewController* play = [[AVPlayerViewController alloc]init];
    
    play.player= [[AVPlayer alloc]initWithURL:url];
    
    play.allowsPictureInPicturePlayback=YES;//这个是允许画中画的
    
    [play.player play]; //这里我设置直接播放,页面弹出后会直接播放,要不然还需要点击一下播放按钮
  
    [self presentViewController:play animated:YES completion:nil];

你可能感兴趣的:(视频直播的简单实现)