視頻播放-AVKit - (Obj-C)

1.AVKit (AVPlayerViewController) 是iOS 8.0 開始使用的視頻播放API

2.AVKit還是以AVFoundation的形式來使用-->自定義播放視圖
MediaPlayer是基于AVFoundation的基礎(chǔ)上進(jìn)行的封裝了
提供了帶視圖和不帶視圖的兩種方式
現(xiàn)實開發(fā)中,系統(tǒng)默認(rèn)封裝好的MPMoviePlayerViewController(帶有視圖)很少被使用
所以AVKit回歸自定義播放視圖的方式來使用

3.AVPlayerViewController在實例化對象時
系統(tǒng)并沒有為我們提供特殊的類方法,而是自身包含一個AVPlayer類型的屬性,實例化控制器后,通過設(shè)置player屬性,來確認(rèn)播放的視頻資源,所以同樣需要使用AVFoundation類庫

4.AVPlayerViewController 繼承自 UIViewController
所以也可以進(jìn)行modal展示,相當(dāng)于AVKit能夠?qū)崿F(xiàn)MediaPlayer和AVFoundation的樣式

使用步驟:

1.導(dǎo)入頭文件

#import <AVFoundation/AVFoundation.h> // 基于AVFoundation,通過實例化的控制器來設(shè)置player屬性
#import <AVKit/AVKit.h>   // 1. 導(dǎo)入頭文件   iOS 9 新增

2. 創(chuàng)建視頻播放控制器

AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];

3. 設(shè)置視頻播放器 (這里為了簡便,使用了URL方式,同樣支持playerWithPlayerItem:的方式)

playerViewController.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:filePath]];

4. modal展示

[self presentViewController:playerViewController animated:YES completion:nil];

視圖中的媒體按鈕系統(tǒng)都已經(jīng)幫我們實現(xiàn)了功能

視頻播放_1.png

這樣雖然能夠modal顯示視圖了,但是并不會自動播放,需要我們手動開啟

5. 開始播放 : 默認(rèn)不會自動播放

[playerViewController.player play];

modal展示方式代碼:

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h> // 基于AVFoundation,通過實例化的控制器來設(shè)置player屬性
#import <AVKit/AVKit.h>   // 1. 導(dǎo)入頭文件   iOS 9 新增

/*
     MediaPlayer是基于AVFoundation的基礎(chǔ)上進(jìn)行的封裝了,提供了帶視圖和不帶視圖的兩種方式
     現(xiàn)實開發(fā)中,系統(tǒng)默認(rèn)封裝好的MPMoviePlayerViewController(帶有視圖)很少被使用,所以還是以AVFoundation的形式來使用-->自定義播放視圖
 */

@interface ViewController ()

@end

@implementation ViewController


// 開始播放按鈕
- (IBAction)clickStartButtton:(UIButton *)sender{
    
    // 本地資源文件
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"minion_01.mp4" ofType:nil];
    
    // 2. 創(chuàng)建視頻播放控制器
    AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
    
    // 3. 設(shè)置視頻播放器 (這里為了簡便,使用了URL方式,同樣支持playerWithPlayerItem:的方式)
    playerViewController.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:filePath]];
    
    // 4. modal展示
    [self presentViewController:playerViewController animated:YES completion:nil];
//    [self presentViewController:playerViewController animated:YES completion:^{
//        [playerViewController.player play];
//    }];
    
    // 5. 開始播放 : 默認(rèn)不會自動播放
    [playerViewController.player play];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

這樣通過AVKit就以最簡單的方式實現(xiàn)了視頻播放,使用了系統(tǒng)提供的視圖完成
如果使用自定義視圖的方式,也很簡單:

playerViewController.view.frame = CGRectMake(40, 100, 300, 300);
[self.view addSubview:playerViewController.view];
視頻播放_2.png

但是默認(rèn)除了進(jìn)度條外,其他的媒體控制都是未實現(xiàn)的,需要我們手動去實現(xiàn)

完整示例代碼:

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h> // 基于AVFoundation,通過實例化的控制器來設(shè)置player屬性
#import <AVKit/AVKit.h>   // 1. 導(dǎo)入頭文件   iOS 9 新增

/*
     MediaPlayer是基于AVFoundation的基礎(chǔ)上進(jìn)行的封裝了,提供了帶視圖和不帶視圖的兩種方式
     現(xiàn)實開發(fā)中,系統(tǒng)默認(rèn)封裝好的MPMoviePlayerViewController(帶有視圖)很少被使用,所以還是以AVFoundation的形式來使用-->自定義播放視圖
 */


@interface ViewController ()
@end

@implementation ViewController


// 開始播放按鈕
- (IBAction)clickStartButtton:(UIButton *)sender{
    
    // 本地資源文件
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"minion_01.mp4" ofType:nil];
    
    // 2. 創(chuàng)建視頻播放控制器
    AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
    
    // 3. 設(shè)置視頻播放器 (這里為了簡便,使用了URL方式,同樣支持playerWithPlayerItem:的方式)
    playerViewController.player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:filePath]];
    
    
    // 4. modal展示
//    [self presentViewController:playerViewController animated:YES completion:nil];
//    [self presentViewController:playerViewController animated:YES completion:^{
//        [playerViewController.player play];
//    }];
    
    // 4. 自定義視圖 (媒體控制默認(rèn)是)
    playerViewController.view.frame = CGRectMake(40, 100, 300, 300);
    [self.view addSubview:playerViewController.view];
    
    
    // 5. 開始播放 : 默認(rèn)不會自動播放
    [playerViewController.player play];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}


@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容