iOS开发eiYoiOS图像、音频开发

iOS开发-视频播放

2015-08-23  本文已影响19270人  superWX

视频播放

区别

1.AVPlayer(远程⾳音乐/播放视频)—>添加layer

2.MPMoviePlayerController—>给.view设置frame,将这个view 添加到某⼀一个View

3.MPMoviePlayerViewController(modal出来,⾃自动播放,⼀一定全 屏播放)

1:AVPlayer

#pragma mark - 懒加载代码
- (AVPlayer *)player
{
   if (_player == nil) {
       // 1.获取URL(远程/本地)
       // NSURL *url = [[NSBundle mainBundle] URLForResource:@"01-知识回顾.mp4" withExtension:nil];
       NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/a45016f4-08d6-4277-abe6-bcfd5244c201/L.mp4"];
       
       // 2.创建AVPlayerItem
       AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
       
       // 3.创建AVPlayer
       _player = [AVPlayer playerWithPlayerItem:item];
       
       // 4.添加AVPlayerLayer
       AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
       layer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
       [self.view.layer addSublayer:layer];
   }
   return _player;
}

2:MPMoviePlayerController

#import <MediaPlayer/MediaPlayer.h>

#pragma mark - 懒加载代码
- (MPMoviePlayerController *)playerController
{
   if (_playerController == nil) {
       // 1.获取视频的URL
       NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"];
       
       // 2.创建控制器
       _playerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
       
       // 3.设置控制器的View的位置
       _playerController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
       
       // 4.将View添加到控制器上
       [self.view addSubview:_playerController.view];
       
       // 5.设置属性
       _playerController.controlStyle = MPMovieControlStyleNone;
   }
   return _playerController;
}

3:MPMoviePlayerViewController

 #import <MediaPlayer/MediaPlayer.h>
 
 #pragma mark - 懒加载
- (MPMoviePlayerViewController *)playerVc
{
    if (_playerVc == nil) {
        NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/a45016f4-08d6-4277-abe6-bcfd5244c201/L.mp4"];
        
        _playerVc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];;
    }
    return _playerVc;
}

- (IBAction)play {
    [self presentMoviePlayerViewControllerAnimated:self.playerVc];
}


上一篇下一篇

猜你喜欢

热点阅读