天天看點

iOS 音視訊之網絡視訊播放AVPlayerViewController(iOS8.0+使用)

1.視訊播放新功能

iOS8.0之後新增了AVPlayerViewController,內建了AVPlayer

iOS9.0之後增加了快進和後退功能,播放視訊特别友善,再也不用複雜地封裝AVPlayer功能了,目前iOS8.0,iOS7.0等低版本系統的占有率不超過5%了,可以慢慢考慮不用适配iOS8.0一下的版本了了

API_AVAILABLE(ios())
@interface AVPlayerViewController : UIViewController

@property (nonatomic, strong, nullable) AVPlayer *player;
@property (nonatomic) BOOL showsPlaybackControls;
@property (nonatomic, copy) NSString *videoGravity;
@property (nonatomic, readonly, getter = isReadyForDisplay) BOOL readyForDisplay;
@property (nonatomic, readonly) CGRect videoBounds;
@property (nonatomic, readonly, nullable) UIView *contentOverlayView;

@property (nonatomic) BOOL allowsPictureInPicturePlayback API_AVAILABLE(ios());

@property (nonatomic) BOOL updatesNowPlayingInfoCenter API_AVAILABLE(ios());

@property (nonatomic) BOOL entersFullScreenWhenPlaybackBegins API_AVAILABLE(ios());
@property (nonatomic) BOOL exitsFullScreenWhenPlaybackEnds API_AVAILABLE(ios());
@property (nonatomic, weak, nullable) id <AVPlayerViewControllerDelegate> delegate API_AVAILABLE(ios());

@end

//iOS9.0之後才能使用代理
@protocol AVPlayerViewControllerDelegate <NSObject>
@optional
- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController;

- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController;

- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error;

- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController;

- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController;

- (BOOL)playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:(AVPlayerViewController *)playerViewController;

- (void)playerViewController:(AVPlayerViewController *)playerViewController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler;

@end
           

2.代碼實作

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>


@interface ViewController ()

@property (nonatomic, strong) AVPlayerViewController *playerVC;

@end

@implementation ViewController

//ios8.0之後才能使用
- (AVPlayerViewController *)playerVC{
    if (!_playerVC) {
        _playerVC = [[AVPlayerViewController alloc] init];
        // 根據URL建立AVPlayer
        NSURL *remoteURL = [NSURL URLWithString:@"http://bos.nj.bpc.baidu.com/tieba-smallvideo/11772_3c435014fb2dd9a5fd56a57cc369f6a0.mp4"];
        AVPlayer *player = [AVPlayer playerWithURL:remoteURL];

        _playerVC.player = player;
        //iOS9.0新增
        _playerVC.allowsPictureInPicturePlayback = YES;
    }
    return _playerVC;
}



- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"網絡視訊播放";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(playVideo)];

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
    btn.center = self.view.center;
    [btn setTitle:@"播放" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(playVideo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)playVideo{
    if(self.presentedViewController) return;
    // 直接彈出;
    [self presentViewController:self.playerVC animated:YES completion:nil];
    // 開始播放
    [self.playerVC.player play];
}


@end
           

iOS8.1的使用效果

iOS 音視訊之網絡視訊播放AVPlayerViewController(iOS8.0+使用)
iOS 音視訊之網絡視訊播放AVPlayerViewController(iOS8.0+使用)
iOS 音視訊之網絡視訊播放AVPlayerViewController(iOS8.0+使用)

iOS9.0效果

iOS 音視訊之網絡視訊播放AVPlayerViewController(iOS8.0+使用)

iOS10.0效果

iOS 音視訊之網絡視訊播放AVPlayerViewController(iOS8.0+使用)

iOS11.2效果

iOS 音視訊之網絡視訊播放AVPlayerViewController(iOS8.0+使用)
iOS 音視訊之網絡視訊播放AVPlayerViewController(iOS8.0+使用)