iOS之視頻

1.AVPlayer

工具欄須要自定義訂製。

#import <AVFoundation/AVFoundation.h>

@property (nonatomic, strong)AVPlayer *player;

-(AVPlayer *)player
{
    if (_player == nil) {
        //1.獲取URL(遠程/本地)
     NSURL *url = [[NSBundle mainBundle] URLForResource:@"周杰倫.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:_player];
        layer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width * 9/16);
        layer.backgroundColor = [UIColor redColor].CGColor;
        [self.view.layer addSublayer:layer];

    }
    return _player;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.player play];
}


2.MPMoviePlayerController

自帶工具欄,有很是多通知方法。

#import <MediaPlayer/MediaPlayer.h>
@property (nonatomic ,strong)MPMoviePlayerController *playController;

-(MPMoviePlayerController *)playController
{
    if (_playController == nil) {
        //1.獲取視頻的URL
        NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/3e35cbb0-c8e5-4827-9614-b5a355259010/L.mp4"];
        
        //2.建立控制器
        _playController = [[MPMoviePlayerController alloc] initWithContentURL:url];
        
        //3.設置控制器的View的位置
        _playController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width * 9/16);
        
        //4.將view添加到控制器上
        [self.view addSubview:_playController.view];
        
        //5.設置屬性,隱藏工具欄
       // _playController.controlStyle = MPMovieControlStyleNone;
    }
    return _playController;
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.playController play];
}


3.MPMoviePlayerViewController

全屏播放

#import <MediaPlayer/MediaPlayer.h>

@property (nonatomic ,strong)MPMoviePlayerViewController *playViewContrller;

-(MPMoviePlayerViewController *)playViewContrller
{
    if (_playViewContrller == nil) {
        NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/3e35cbb0-c8e5-4827-9614-b5a355259010/L.mp4"];
        
        _playViewContrller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    
    }
    return _playViewContrller;
}

-(void)play
{
    [self presentMoviePlayerViewControllerAnimated:self.playViewContrller];
}


4.區別

1.AVAudioPlayer(本地音樂)框架

2.AVPlayer(遠程音樂/播放視頻)-> 添加到layer工具

3.MPMoviePlayerController -> 給view設置frame,將這個view添加到某個view上atom

4.MPMoviePlayerViewController(modal出來,自動播放,必定會全屏播放)url


表明框架:kxmovie(最簡易) VLC   FFMpegspa

相關文章
相關標籤/搜索