iOS之AVPlayer、MPMoviePlayerController、MPMoviePlayerViewController、AVPlayerViewController播放音乐和视频

AVPlayer播放音乐

#import 
/** 播放器 */
@property (nonatomic, strong) AVPlayer *player;
#pragma mark - 懒加载代码

- (AVPlayer *)player
{
    if (_player == nil) {

        // 1.创建音乐资源
        NSURL *url = [NSURL URLWithString:@"http://cc.stream.qqmusic.qq.com/C100003j8IiV1X8Oaw.m4a?fromtag=52"];

        AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];

        // 2.创建播放器
       // _player = [AVPlayer playerWithURL:url];
        _player = [AVPlayer playerWithPlayerItem:playerItem];
    }
    return _player;
}

AVPlayer播放视频

- (AVPlayer *)player
{
    if (_player == nil) {

        // 1.加载视频资源
      //  NSURL *url = [[NSBundle mainBundle] URLForResource:@"xiaohuangren.mp4" withExtension:nil];

        NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"];

        // 2.创建播放器
        AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
        _player = [AVPlayer playerWithPlayerItem:playerItem];

        // 3.创建AVPlayerLayer
        AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
        playerLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
        [self.view.layer addSublayer:playerLayer];

    }
    return _player;
}

MPMoviePlayerController播放视频

#import 
/** 播放器 */
@property (nonatomic, strong) MPMoviePlayerController *playerController;
- (MPMoviePlayerController *)playerController
{
    if (_playerController == nil) {

        // 1.创建视频的资源
        NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"];

        // 2.创建播放器
        _playerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
        _playerController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
        [self.view addSubview:_playerController.view];

        // 3.取消工具栏
        _playerController.controlStyle = MPMovieControlStyleNone;

    }
    return _playerController;
}

MPMoviePlayerViewController播放视频

#import 
/** 播放器 */
@property (nonatomic, strong) MPMoviePlayerViewController *playerVC;
- (IBAction)btnClick {

    // 弹出播放器,并播放视频
    [self presentViewController:self.playerVC animated:YES completion:nil];
}

- (MPMoviePlayerViewController *)playerVC
{
    if (_playerVC == nil) {

        // 1.创建视频的资源
        NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"];

        // 2.创建播放器
        _playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

    }
    return _playerVC;
}

AVPlayerViewController播放远程视频

#import 
#import 
/** 播放器 */
@property (nonatomic, strong) AVPlayerViewController *playerVC;
- (IBAction)play {

    [self presentViewController:self.playerVC animated:YES completion:nil];
}

- (AVPlayerViewController *)playerVC
{
    if (_playerVC == nil) {

        NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"];
        AVPlayerItem *playItem = [AVPlayerItem playerItemWithURL:url];
        AVPlayer *player = [AVPlayer playerWithPlayerItem:playItem];

        _playerVC = [[AVPlayerViewController alloc] init];
        _playerVC.player = player;
    }
    return _playerVC;
}

你可能感兴趣的:(IOS专栏)