- Podfile
target 'VideoPlay' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for VideoPlay
pod 'JPVideoPlayer', '~> 3.2.0-beta'
pod 'Masonry'
end
2、vc
#import "ViewController.h"
#import "HZTableViewCell.h"
#import <JPVideoPlayer/JPVideoPlayerKit.h>
@interface ViewController ()
<UITableViewDelegate,UITableViewDataSource,JPScrollViewPlayVideoDelegate,JPVideoPlayerManagerDelegate>
@property (nonatomic, strong) UITableView *customTableView;
@property (nonatomic, strong) NSMutableArray <NSDictionary *>*dataAry;
@property (nonatomic, strong) JPVideoPlayerManager *manager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.dataAry = [NSMutableArray array];
[self setup];
[self.view addSubview:self.customTableView];
self.customTableView.frame = self.view.bounds;
}
- (IBAction)playMusic:(id)sender {
[self.manager stopPlay];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"222" ofType:@"mp3"];
NSURL *fileUrl = [NSURL URLWithString:@"http://mp3.9ku.com/hot/2009/08-27/186947.mp3"];
JPVideoPlayerManager *manager = [[JPVideoPlayerManager alloc] init];
manager.volume = 0.7;
manager.delegate = self;
self.manager = manager;
[manager playVideoWithURL:fileUrl showOnLayer:[CALayer layer] options:JPVideoPlayerRetryFailed configuration:^(JPVideoPlayerModel * _Nonnull playerModel) {
}];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.customTableView jp_handleCellUnreachableTypeInVisibleCellsAfterReloadData];
[self.customTableView jp_playVideoInVisibleCellsIfNeed];
// 用来防止选中 cell push 到下个控制器时, scrollView 再次调用 scrollViewDidScroll 方法, 造成 playingVideoCell 被置空.
// self.customTableView.delegate = self;
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
// 用来防止选中 cell push 到下个控制器时, scrollView 再次调用 scrollViewDidScroll 方法, 造成 playingVideoCell 被置空.
self.customTableView.delegate = nil;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGRect customTableViewFrame = self.customTableView.frame;
customTableViewFrame.size.height -= self.tabBarController.tabBar.bounds.size.height;
self.customTableView.jp_scrollViewVisibleFrame = customTableViewFrame;
}
- (CGFloat)customTableView:(UITableView *)customTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 400;
}
/**
* Called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
* 松手时已经静止, 只会调用scrollViewDidEndDragging
*/
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[self.customTableView jp_scrollViewDidEndDraggingWillDecelerate:decelerate];
}
/**
* Called on customTableView is static after finger up if the user dragged and customTableView is scrolling.
* 松手时还在运动, 先调用scrollViewDidEndDragging, 再调用scrollViewDidEndDecelerating
*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[self.customTableView jp_scrollViewDidEndDecelerating];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self.customTableView jp_scrollViewDidScroll];
}
#pragma mark - JPScrollViewPlayVideoDelegate
- (void)scrollView:(UIScrollView<JPVideoPlayerScrollViewProtocol> *)scrollView
willPlayVideoOnCell:(UIView<JPVideoPlayerCellProtocol> *)cell {
[cell.jp_videoPlayView jp_resumeMutePlayWithURL:cell.jp_videoURL bufferingIndicator:nil progressView:nil configuration:nil];
}
- (UITableView *)customTableView {
if (!_customTableView) {
_customTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_customTableView.jp_delegate = self;
_customTableView.dataSource = self;
_customTableView.delegate = self;
}
return _customTableView;
}
#pragma mark - Setup
- (void)setup{
NSArray *array = @[
@"http://www.w3school.com.cn/example/html5/mov_bbb.mp4",
@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
@"https://media.w3.org/2010/05/sintel/trailer.mp4",
@"http://mvvideo2.meitudata.com/576bc2fc91ef22121.mp4",
@"http://mvvideo10.meitudata.com/5a92ee2fa975d9739_H264_3.mp4",
@"http://mvvideo11.meitudata.com/5a44d13c362a23002_H264_11_5.mp4",
@"http://mvvideo10.meitudata.com/572ff691113842657.mp4",
@"https://api.tuwan.com/apps/Video/play?key=aHR0cHM6Ly92LnFxLmNvbS9pZnJhbWUvcGxheWVyLmh0bWw%2FdmlkPXUwNjk3MmtqNWV6JnRpbnk9MCZhdXRvPTA%3D&aid=381374",
@"https://api.tuwan.com/apps/Video/play?key=aHR0cHM6Ly92LnFxLmNvbS9pZnJhbWUvcGxheWVyLmh0bWw%2FdmlkPWswNjk2enBud2xvJnRpbnk9MCZhdXRvPTA%3D&aid=381395",
];
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.dataAry addObject:@{
@"name":[NSNumber numberWithUnsignedInteger:idx].stringValue,
@"url":obj?:@""
}];
}];
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
HZTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(HZTableViewCell.class)];
if (!cell) {
cell = [[HZTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(HZTableViewCell.class)];
}
NSDictionary *dic = [self.dataAry objectAtIndex:indexPath.row];
NSString *url = dic[@"url"];
cell.jp_muted = YES;
cell.jp_videoURL = [NSURL URLWithString:url];
cell.jp_videoPlayView = cell.playView;
[tableView jp_handleCellUnreachableTypeForCell:cell
atIndexPath:indexPath];
return cell;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 400;
}
/**
* Controls which video should be downloaded when the video is not found in the cache.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param videoURL The url of the video to be downloaded.
*
* @return Return NO to prevent the downloading of the video on cache misses. If not implemented, YES is implied.
*/
- (BOOL)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager
shouldDownloadVideoForURL:(NSURL *)videoURL; {
return YES;
}
/**
* Controls which video should automatic replay when the video is play completed.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param videoURL The url of the video to be play.
*
* @return Return NO to prevent replay for the video. If not implemented, YES is implied.
*/
- (BOOL)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager
shouldAutoReplayForURL:(NSURL *)videoURL;{
return YES;
}
/**
* Notify the playing status.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param playerStatus The current playing status.
*/
- (void)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager
playerStatusDidChanged:(JPVideoPlayerStatus)playerStatus; {
}
/**
* Notify the video file length.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param videoLength The file length of video data.
*/
- (void)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager
didFetchVideoFileLength:(NSUInteger)videoLength; {
}
/**
* Notify the download progress value. this method will be called on main thread.
* If the video is local or cached file, this method will be called once and the receive size equal to expected size,
* If video is existed on web, this method will be called when the download progress value changed,
* If some error happened, the error is no nil.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param cacheType The video data cache type.
* @param fragmentRanges The fragment of video data that cached in disk.
* @param expectedSize The expected data size.
* @param error The error when download video data.
*/
- (void)videoPlayerManagerDownloadProgressDidChange:(JPVideoPlayerManager *)videoPlayerManager
cacheType:(JPVideoPlayerCacheType)cacheType
fragmentRanges:(NSArray<NSValue *> * _Nullable)fragmentRanges
expectedSize:(NSUInteger)expectedSize
error:(NSError *_Nullable)error; {
}
/**
* Notify the playing progress value. this method will be called on main thread.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param elapsedSeconds The current played seconds.
* @param totalSeconds The total seconds of this video for given url.
* @param error The error when playing video.
*/
- (void)videoPlayerManagerPlayProgressDidChange:(JPVideoPlayerManager *)videoPlayerManager
elapsedSeconds:(double)elapsedSeconds
totalSeconds:(double)totalSeconds
error:(NSError *_Nullable)error; {
}
/**
* Called when application will resign active.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param videoURL The url of the video to be play.
*/
- (BOOL)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager
shouldPausePlaybackWhenApplicationWillResignActiveForURL:(NSURL *)videoURL; {
return NO;
}
/**
* Called when application did enter background.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param videoURL The url of the video to be play.
*/
- (BOOL)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager
shouldPausePlaybackWhenApplicationDidEnterBackgroundForURL:(NSURL *)videoURL;{
return NO;
}
/**
* Called only when application become active from `Control Center`,
* `Notification Center`, `pop UIAlert`, `double click Home-Button`.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param videoURL The url of the video to be play.
*/
- (BOOL)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager
shouldResumePlaybackWhenApplicationDidBecomeActiveFromResignActiveForURL:(NSURL *)videoURL;{
return NO;
}
/**
* Called only when application become active from `Share to other application`,
* `Enter background`, `Lock screen`.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param videoURL The url of the video to be play.
*/
- (BOOL)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager
shouldResumePlaybackWhenApplicationDidBecomeActiveFromBackgroundForURL:(NSURL *)videoURL;{
return NO;
}
/**
* Called when call resume play but can not resume play.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param videoURL The url of the video to be play.
*/
- (BOOL)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager
shouldTranslateIntoPlayVideoFromResumePlayForURL:(NSURL *)videoURL;{
return NO;
}
/**
* Called when receive audio session interruption notification.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param videoURL The url of the video to be play.
*/
- (BOOL)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager
shouldPausePlaybackWhenReceiveAudioSessionInterruptionNotificationForURL:(NSURL *)videoURL;{
return NO;
}
/**
* Provide custom audio session category to play video.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
*
* @return The prefer audio session category.
*/
//- (AVAudioSessionCategory)videoPlayerManagerPreferAudioSessionCategory:(JPVideoPlayerManager *)videoPlayerManager;{
//
//}
/**
* Called when play a already played video.
*
* @param videoPlayerManager The current `JPVideoPlayerManager`.
* @param videoURL The url of the video to be play.
* @param elapsedSeconds The elapsed seconds last playback recorded.
*/
- (BOOL)videoPlayerManager:(JPVideoPlayerManager *)videoPlayerManager
shouldResumePlaybackFromPlaybackRecordForURL:(NSURL *)videoURL
elapsedSeconds:(NSTimeInterval)elapsedSeconds;{
return NO;
}
@end
- cell
#import "HZTableViewCell.h"
#import <JPVideoPlayer/JPVideoPlayer.h>
#import <JPVideoPlayer/UITableViewCell+WebVideoCache.h>
#import <JPVideoPlayer/UIView+WebVideoCache.h>
@implementation HZTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self.contentView addSubview:self.playView];
[self.playView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsMake(10, 20, 10, 20));
}];
}
return self;
}
- (UIView *)playView {
if (!_playView) {
_playView = [[UIView alloc] init];
_playView.backgroundColor = [UIColor redColor];
}
return _playView;
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
AVURLAsset *videoURLAsset = [AVURLAsset URLAssetWithURL:self.jp_videoURL options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:videoURLAsset];
gen.appliesPreferredTrackTransform = YES;
gen.maximumSize = CGSizeMake(1121/3, 633/3);
CMTime time = self.jp_videoPlayView.jp_currentTime;
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *shotImage;
shotImage = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
Simulator Screen Shot - iPod touch (7th generation) - 2021-06-13 at 17.46.09.png
网友评论