iOS視頻播放詳解1-基本使用

在iOS中播放視頻有三種方式:

  • MPMoviePlayerController
  • MPMoviePlayerViewController
  • AVPlayer

下面我們就來看看這三種方法怎么使用以及有什么不同。

1: MPMoviePlayerController

MPMoviePlayerController(iOS9后已經(jīng)被拋棄了,系統(tǒng)建議使用"Use AVPlayerViewController in AVKit.")屬于MediaPlayer.framework,它支持本地視頻和網(wǎng)絡(luò)視頻播放。這個類實現(xiàn)了MPMediaPlayback協(xié)議,因此具備一般的播放器控制功能,例如播放、暫停、停止等。但是MPMediaPlayerController自身并不是一個完整的視圖控制器,如果要在UI中展示視頻需要將view屬性添加到界面中

使用例子(這里是主要代碼):
NSString * urlStr = @"http://zyvideo1.oss-cn-qingdao.aliyuncs.com/zyvd/7c/de/04ec95f4fd42d9d01f63b9683ad0";
    NSURL * url = [NSURL URLWithString:urlStr];
    //MPMoviePlayerController iOS9后系統(tǒng)建議用 "Use AVPlayerViewController in AVKit."
    MPMoviePlayerController * player = [[MPMoviePlayerController alloc]initWithContentURL:url];
    _player = player;
    player.view.frame = CGRectMake(0, 100, 375, 300);
    [self.view addSubview:player.view];
    
    [player play];
    
    //添加通知 監(jiān)聽播放狀態(tài)
    NSNotificationCenter *notificationCenter=[NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.player];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];

//播放狀態(tài)改變,注意播放完成時的狀態(tài)是暫停
-(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{
    switch (self.player.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"正在播放...");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暫停播放.");
            break;
        case MPMoviePlaybackStateStopped:
            NSLog(@"停止播放.");
            break;
        default:
            NSLog(@"播放狀態(tài):%li",self.player.playbackState);
            break;
    }
}

//播放完成
-(void)mediaPlayerPlaybackFinished:(NSNotification *)notification{
    NSLog(@"播放完成.%li",self.player.playbackState);
}

2: MPMoviePlayerViewController

MPMoviePlayerViewController(iOS9以后也被拋棄,系統(tǒng)建議使用"Use AVPlayerViewController in AVKit.")是對MPMoviePlayerController的再次封裝,它內(nèi)部有一個MPMoviePlayerController的視圖控制器就是MPMoviePlayerViewController,它繼承于UIViewController。MPMoviePlayerViewController內(nèi)部多了一個moviePlayer屬性和一個帶有url的初始化方法,同時它內(nèi)部實現(xiàn)了一些作為模態(tài)視圖展示所特有的功能,例如默認是全屏模式展示、彈出后自動播放、作為模態(tài)窗口展示時如果點擊“Done”按鈕會自動退出模態(tài)窗口等。在下面的示例中就不直接將播放器放到主視圖控制器,而是放到一個模態(tài)視圖控制器中。MPMoviePlayerViewController初始化方法做了大量工作(例如設(shè)置URL、自動播放、添加點擊Done完成的監(jiān)控等)。

使用例子:
    - (void)clickTestButton:(UIButton *) sender{
    
    [self presentViewController:self.moviePlayerViewController animated:YES completion:nil];
}

- (MPMoviePlayerViewController *)moviePlayerViewController{
    if (!_moviePlayerViewController) {
        NSString * urlStr = @"http://zyvideo1.oss-cn-qingdao.aliyuncs.com/zyvd/7c/de/04ec95f4fd42d9d01f63b9683ad0";
        NSURL * url = [NSURL URLWithString:urlStr]; //"Use AVPlayerViewController in AVKit."
        _moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    }
    return _moviePlayerViewController;
}

3. AVPlayer

以上兩個工具非常強大,使用非常簡單,幾行代碼就可以搞定,可以滿足項目中常見的大部分視頻播放功能。但是由于他的高度封裝性,當我們需要自定義播放器界面的時候就比較困難了,尤其是實現(xiàn)邊下邊播,隨時需要知道播放器各種狀態(tài)以及播放完后將緩存數(shù)據(jù)保存到本地的時候就比較難以實現(xiàn)了,這個時候AVPlayer就閃亮登場了,關(guān)于AVPlayer邊下邊播我們下篇博客詳細講,這里只講它的基本使用。

AVPlayer

)

AVPlayer本身并不能顯示視頻,而且它也不像MPMoviePlayerController有一個view屬性。如果AVPlayer要顯示必須創(chuàng)建一個播放器層AVPlayerLayer用于展示,播放器層繼承于CALayer,有了AVPlayerLayer之添加到控制器視圖的layer中即可。要使用AVPlayer首先了解一下幾個常用的類:

AVAsset:主要用于獲取多媒體信息,是一個抽象類,不能直接使用。

AVURLAsset:AVAsset的子類,可以根據(jù)一個URL路徑創(chuàng)建一個包含媒體信息的AVURLAsset對象。

AVPlayerItem:一個媒體資源管理對象,管理者視頻的一些基本信息和狀態(tài),一個AVPlayerItem對應(yīng)著一個視頻資源。

使用例子(簡單使用):

@property (nonatomic, strong) AVPlayer * player;
@property (nonatomic, strong) AVPlayerItem * playerItem;
@property (nonatomic, strong) AVPlayerLayer * playerLayer;


//根據(jù)媒體資源地址創(chuàng)建AVPlayerItem
 _playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://zyvideo1.oss-cn-qingdao.aliyuncs.com/zyvd/7c/de/04ec95f4fd42d9d01f63b9683ad0"]];
 //根據(jù)AVPlayerItem創(chuàng)建媒體播放器
    _player = [[AVPlayer alloc] initWithPlayerItem:_playerItem];
    //創(chuàng)建AVPlayerLayer,用于呈現(xiàn)視頻
    _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    _playerLayer.frame = CGRectMake(0, 100, 375, 300);
    [self.view.layer addSublayer:_playerLayer];
    
    [_player play];

補充說明

蘋果在iOS9后廢棄了第一種方法和第二種方法,引入了AVKit框架下的AVPlayerViewController
AVPlayerViewController提供了默認的可視化控制界面,要使用AVPlayerViewController需導入AVKit.h。AVPlayerViewController整合了一個完整的播放器,可以作為控制器進行操作顯示。

常用屬性

player:設(shè)置播放器

showsPlaybackControls:設(shè)置是否顯示媒體播放組件,默認YES

videoGravity:設(shè)置視頻拉伸模式

allowsPictureInPicturePlayback:設(shè)置是否允許畫中畫回放,默認YES

delegate:設(shè)置代理

AVPlayerViewControllerDelegate

// 1、即將開始畫中畫
- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController;

// 2、開始畫中畫
- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController;

// 3、畫中畫失敗
- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error;

// 4、即將結(jié)束畫中畫
- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController;

// 5、結(jié)束畫中畫
- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController;
使用例子(簡單使用):
@interface ViewController ()<AVPlayerViewControllerDelegate>

@property (nonatomic, strong) AVPlayerViewController * playerView;

@end
// 配置媒體播放控制器
    _playerView = [[AVPlayerViewController alloc]  init];
    // 設(shè)置媒體源數(shù)據(jù)
    _playerView.player = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://zyvideo1.oss-cn-qingdao.aliyuncs.com/zyvd/7c/de/04ec95f4fd42d9d01f63b9683ad0"]];
    // 設(shè)置拉伸模式
    _playerView.videoGravity = AVLayerVideoGravityResizeAspect;
    // 設(shè)置是否顯示媒體播放組件
    _playerView.showsPlaybackControls = YES;
    // 設(shè)置大力
    _playerView.delegate = self;
    // 播放視頻
    [_playerView.player play];
    // 設(shè)置媒體播放器視圖大小
    _playerView.view.bounds = CGRectMake(0, 0, 350, 300);
    _playerView.view.center = CGPointMake(CGRectGetMidX(self.view.bounds), 64 + CGRectGetMidY(_playerView.view.bounds) + 30);
    // 4、推送播放
    // 推送至媒體播放器進行播放
    // [self presentViewController:_playerViewController animated:YES completion:nil];
    // 直接在本視圖控制器播放
    [self addChildViewController:_playerView];
    [self.view addSubview:_playerView.view];
    
    
    
    #pragma mark *** AVPlayerViewControllerDelegate ***
- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

以上是iOS中播放視頻幾種方式最簡單最基本的使用,寫一篇我們來學習一個基于AVPlayer的自定義的較為復(fù)雜的播放器。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容