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 <ZFPlayer/ZFAVPlayerManager.h>
#import <ZFPlayer/ZFPlayerControlView.h>

@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>
  • 如下任何一个地方都行
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值