iOS开发-ZFPlayer的简单使用 播放单个网络视频

iOS开发-ZFPlayer的简单使用 播放单个网络视频

    • 前言
    • 开发准备
    • 代码
    • 注意

前言

  • 关于ZFPlayer播放单个网络视频案例,它的网络列表视频案例在gitHub上面很多。

开发准备

  • podfile导入
    pod 'ZFPlayer', '~> 3.3.3' #, '~> 3.2.17' # 视频播放
    pod 'ZFPlayer/ControlView'
    pod 'ZFPlayer/AVPlayer'

代码

  • ViewController.m
#import "ViewController.h"
// ZFPlayer
#import 
#import 

@interface ViewController ()

@property(nonatomic, strong) UIView *forPlayView;
@property(nonatomic, strong) ZFPlayerController *player;
@property(nonatomic, strong) ZFAVPlayerManager *playerManager;
@property(nonatomic, strong) ZFPlayerControlView *controlView;
@property(nonatomic, strong) UIButton *closeBtn;

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSString *videoUrl = @"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
    [self.player.currentPlayerManager setAssetURL:[NSURL URLWithString:videoUrl]];
    self.forPlayView.hidden = NO;
    self.closeBtn.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.forPlayView];
    [self.view addSubview:self.closeBtn];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.forPlayView.frame = self.view.bounds;
    self.closeBtn.frame = self.view.bounds;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self playerDealloc];
}

- (UIView *)forPlayView {
    if(_forPlayView == nil) {
        _forPlayView = [[UIView alloc]init];
        _forPlayView.hidden = YES;
    }
    return _forPlayView;
}

- (UIButton *)closeBtn {
    if(_closeBtn == nil) {
        _closeBtn = [[UIButton alloc]init];
        [_closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

#pragma mark - 网络视频模块

- (ZFAVPlayerManager *)playerManager {
    if(_playerManager == nil) {
        _playerManager = [[ZFAVPlayerManager alloc] init];
    }
    return _playerManager;
}

- (ZFPlayerController *)player {
    if(_player == nil) {
        _player = [[ZFPlayerController alloc] initWithPlayerManager:self.playerManager containerView:self.forPlayView];
        _player.controlView = self.controlView;
        _player.playerDisapperaPercent = 1.0f;
        //__weak typeof(self) weakSelf = self;
        _player.playerDidToEnd = ^(id<ZFPlayerMediaPlayback> asset) { //结束播放
            
        };
        [_player setPlayerPlayStateChanged:^(id<ZFPlayerMediaPlayback> asset, ZFPlayerPlaybackState playState) {
            if(playState == ZFPlayerPlayStatePlayStopped) {
                NSLog(@"播放暂停");
            }
        }];
        [_player setPlayerPlayTimeChanged:^(id<ZFPlayerMediaPlayback> asset, NSTimeInterval currentTime, NSTimeInterval duration) {
            NSLog(@"播放改变currentTime - %f duration - %f", currentTime, duration);
        }];
    }
    return _player;
}

- (ZFPlayerControlView *)controlView {
    if (_controlView == nil) {
        _controlView = [[ZFPlayerControlView alloc]init];
        _controlView.prepareShowLoading = YES;
    }
    return _controlView;
}

- (void)playerDealloc {
    //网络视频
    if(_playerManager) {
        _playerManager = nil;
    }
    if(_player) {
        _player.viewControllerDisappear = YES;
        [_player stop];
        _player = nil;
    }
}

- (void)closeBtnClick {
    self.closeBtn.hidden = YES;
    self.forPlayView.hidden = YES;
    //[self playerDealloc];
}

@end

注意

  • 因为播放的是网络视频, 记得在info.plist ->右击->sourceCode->插入ATS
<key>NSAppTransportSecurity</key>
<dict>
	<key>NSAllowsArbitraryLoads</key>
	<true/>
</dict>
  • 如下任何一个地方都行
    iOS开发-ZFPlayer的简单使用 播放单个网络视频_第1张图片

你可能感兴趣的:(ios,移动开发)