Skip to content

Commit

Permalink
custom ActionBar on AVPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
LuKane committed Jan 11, 2023
1 parent d69de00 commit 573b01d
Show file tree
Hide file tree
Showing 18 changed files with 441 additions and 58 deletions.
24 changes: 22 additions & 2 deletions KNPhotoBrowser.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
791260AE23FE75F300BC4181 /* KNPhotoBrowserImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 7912609D23FE75F300BC4181 /* KNPhotoBrowserImageView.m */; };
791260AF23FE75F300BC4181 /* KNPhotoBrowser.m in Sources */ = {isa = PBXBuildFile; fileRef = 7912609E23FE75F300BC4181 /* KNPhotoBrowser.m */; };
791260B023FE75F300BC4181 /* KNPhotoBrowserNumView.m in Sources */ = {isa = PBXBuildFile; fileRef = 7912609F23FE75F300BC4181 /* KNPhotoBrowserNumView.m */; };
7930C289296E982900BE99C8 /* KNPhotoBrowserConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 7930C288296E982900BE99C8 /* KNPhotoBrowserConfig.m */; };
7930C28D296F02C300BE99C8 /* CustomAVPlayerActionBarController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7930C28C296F02C300BE99C8 /* CustomAVPlayerActionBarController.m */; };
7935CAD12935DF9F00D89B2C /* PushPhotoBrowserController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7935CAD02935DF9F00D89B2C /* PushPhotoBrowserController.m */; };
793E42DC2653C773006AF893 /* NineSquareController.m in Sources */ = {isa = PBXBuildFile; fileRef = 793E42DB2653C773006AF893 /* NineSquareController.m */; };
793E42E22653C792006AF893 /* NineSquareModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 793E42E12653C792006AF893 /* NineSquareModel.m */; };
Expand Down Expand Up @@ -127,6 +129,10 @@
7912609D23FE75F300BC4181 /* KNPhotoBrowserImageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KNPhotoBrowserImageView.m; sourceTree = "<group>"; };
7912609E23FE75F300BC4181 /* KNPhotoBrowser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KNPhotoBrowser.m; sourceTree = "<group>"; };
7912609F23FE75F300BC4181 /* KNPhotoBrowserNumView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KNPhotoBrowserNumView.m; sourceTree = "<group>"; };
7930C287296E982900BE99C8 /* KNPhotoBrowserConfig.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KNPhotoBrowserConfig.h; sourceTree = "<group>"; };
7930C288296E982900BE99C8 /* KNPhotoBrowserConfig.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KNPhotoBrowserConfig.m; sourceTree = "<group>"; };
7930C28B296F02C300BE99C8 /* CustomAVPlayerActionBarController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CustomAVPlayerActionBarController.h; sourceTree = "<group>"; };
7930C28C296F02C300BE99C8 /* CustomAVPlayerActionBarController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CustomAVPlayerActionBarController.m; sourceTree = "<group>"; };
7935CACF2935DF9F00D89B2C /* PushPhotoBrowserController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PushPhotoBrowserController.h; sourceTree = "<group>"; };
7935CAD02935DF9F00D89B2C /* PushPhotoBrowserController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PushPhotoBrowserController.m; sourceTree = "<group>"; };
793E42DA2653C773006AF893 /* NineSquareController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NineSquareController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -311,11 +317,22 @@
7912609823FE75F300BC4181 /* KNPhotoAVPlayerView.m */,
79ECDCD7264B7F21009EAAF4 /* KNPhotoLocateAVPlayerView.h */,
79ECDCD8264B7F21009EAAF4 /* KNPhotoLocateAVPlayerView.m */,
7930C287296E982900BE99C8 /* KNPhotoBrowserConfig.h */,
7930C288296E982900BE99C8 /* KNPhotoBrowserConfig.m */,
79ECDCDD264B7FC0009EAAF4 /* KNPhotoPlayerProtocol.h */,
);
path = KNPhotoAVPlayer;
sourceTree = "<group>";
};
7930C28A296F02AC00BE99C8 /* CustomAVPlayerActionBarController */ = {
isa = PBXGroup;
children = (
7930C28B296F02C300BE99C8 /* CustomAVPlayerActionBarController.h */,
7930C28C296F02C300BE99C8 /* CustomAVPlayerActionBarController.m */,
);
path = CustomAVPlayerActionBarController;
sourceTree = "<group>";
};
7935CACE2935DF8400D89B2C /* PushPhotoBrower */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -428,6 +445,7 @@
7994AEA02652798500267860 /* CustomSourceViewController */,
7994AE9D2652798500267860 /* CustomViewController */,
7935CACE2935DF8400D89B2C /* PushPhotoBrower */,
7930C28A296F02AC00BE99C8 /* CustomAVPlayerActionBarController */,
);
path = Class;
sourceTree = "<group>";
Expand Down Expand Up @@ -747,9 +765,11 @@
79ED3C8126568E990062AAE8 /* IMController.m in Sources */,
791260AF23FE75F300BC4181 /* KNPhotoBrowser.m in Sources */,
798CB6CD26590B5900F56291 /* CustomSourceViewController.m in Sources */,
7930C289296E982900BE99C8 /* KNPhotoBrowserConfig.m in Sources */,
7953DADE265A20E400B73551 /* CustomSourceImageView2.m in Sources */,
790E78C42656823E0025929B /* ScrollViewLocateController.m in Sources */,
79E115D122F032310022106B /* FirstViewController.m in Sources */,
7930C28D296F02C300BE99C8 /* CustomAVPlayerActionBarController.m in Sources */,
793E42E22653C792006AF893 /* NineSquareModel.m in Sources */,
7953DAD8265A20D300B73551 /* CustomSourceView2.m in Sources */,
79AA377B264FAC61001FE0B3 /* ViewController.m in Sources */,
Expand Down Expand Up @@ -926,7 +946,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = "2.6.3-dev";
MARKETING_VERSION = "2.6.4-dev";
PRODUCT_BUNDLE_IDENTIFIER = com.LuKane.test.KNPhotoBrowser;
PRODUCT_NAME = "$(TARGET_NAME)";
TARGETED_DEVICE_FAMILY = "1,2";
Expand All @@ -947,7 +967,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = "2.6.3-dev";
MARKETING_VERSION = "2.6.4-dev";
PRODUCT_BUNDLE_IDENTIFIER = com.LuKane.test.KNPhotoBrowser;
PRODUCT_NAME = "$(TARGET_NAME)";
TARGETED_DEVICE_FAMILY = "1,2";
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,5 @@
stopOnStyle = "0">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "B878C48F-DDB8-4FE0-B46A-493F46B62817"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "KNPhotoBrowser/KNPhotoBrowser/KNPhotoBrowser.m"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "450"
endingLineNumber = "450"
landmarkName = "-panDidGesture:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
Binary file modified KNPhotoBrowser/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// CustomAVPlayerActionBarController.h
// KNPhotoBrowser
//
// Created by LuKane on 2023/1/11.
// Copyright © 2023 LuKane. All rights reserved.
//

#import "BaseViewController.h"

NS_ASSUME_NONNULL_BEGIN

@interface CustomAVPlayerActionBarController : BaseViewController

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
//
// CustomAVPlayerActionBarController.m
// KNPhotoBrowser
//
// Created by LuKane on 2023/1/11.
// Copyright © 2023 LuKane. All rights reserved.
//

#import "CustomAVPlayerActionBarController.h"
#import "UIView+Extension.h"

#import "KNPhotoBrowser.h"
#import "KNPhotoAVPlayerActionBar.h"
#import <UIImageView+WebCache.h>
#import <SDAnimatedImageView.h>


@interface KNPhotoAVPlayerActionSubBar : KNPhotoAVPlayerActionBar

@end

@implementation KNPhotoAVPlayerActionSubBar


@end

@interface CustomAVPlayerActionBarController () <KNPhotoBrowserDelegate>

/// contain all urls
@property (nonatomic,strong) NSMutableArray *urlArr;

@property (nonatomic,strong) NSMutableArray<KNPhotoItems *> *itemsArr;

@property (nonatomic,copy ) NSString *videoPlaceHolderUrl0;
@property (nonatomic,copy ) NSString *videoPlaceHolderUrl1;

@property (nonatomic,weak ) UIView *orangeView;

@end


@implementation CustomAVPlayerActionBarController

- (NSMutableArray<KNPhotoItems *> *)itemsArr {
if (!_itemsArr) {
_itemsArr = [NSMutableArray array];
}
return _itemsArr;
}
- (NSMutableArray *)urlArr {
if (!_urlArr) {
_urlArr = [NSMutableArray array];
/// net image
[_urlArr addObject:@"http://wx3.sinaimg.cn/thumbnail/9bbc284bgy1frtdh1idwkj218g0rs7li.jpg"];
/// loc video
[_urlArr addObject:[[NSBundle mainBundle] pathForResource:@"location_video.MP4" ofType:nil]];
/// net video
[_urlArr addObject:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"];
[_urlArr addObject:@"https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200ff00000bdkpfpdd2r6fb5kf6m50&line=0.MP4"];

/// net image
[_urlArr addObject:@"http://ww2.sinaimg.cn/thumbnail/677febf5gw1erma104rhyj20k03dz16y.jpg"];
[_urlArr addObject:@"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr1xydcj20gy0o9q6s.jpg"];
[_urlArr addObject:@"http://ww2.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr39ht9j20gy0o6q74.jpg"];

/// loc gif image
[_urlArr addObject:[[NSBundle mainBundle] pathForResource:@"gif3.GIF" ofType:nil]];

/// net gif image
[_urlArr addObject:@"http://ww2.sinaimg.cn/bmiddle/642beb18gw1ep3629gfm0g206o050b2a.gif"];
}
return _urlArr;
}

- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Custom AVPlayer ActionBar";

self.videoPlaceHolderUrl0 = @"https://edu-201121.oss-cn-beijing.aliyuncs.com/WX20210106-103823.png";
self.videoPlaceHolderUrl1 = @"https://edu-201121.oss-cn-beijing.aliyuncs.com/WX20210106-103742.png";

[self setupViews];
}
- (void)setupViews {

/// background view
UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(20, 80, self.view.width - 40, self.view.width - 40)];
orangeView.backgroundColor = UIColor.orangeColor;
[self.view addSubview:orangeView];
self.orangeView = orangeView;

// nine photo or video
CGFloat width = (self.view.frame.size.width - 40 - 40) / 3;

for (NSInteger i = 0; i < self.urlArr.count; i++) {
if (i != 7 && i != 8) {
UIImageView *imageView = [[UIImageView alloc] init];
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidClick:)]];
imageView.tag = i;
[orangeView addSubview:imageView];

if(i == 2 || i == 3){
// net video
if (i == 2) {
[imageView sd_setImageWithURL:[NSURL URLWithString:self.videoPlaceHolderUrl0] placeholderImage:nil];
}
if (i == 3) {
[imageView sd_setImageWithURL:[NSURL URLWithString:self.videoPlaceHolderUrl1] placeholderImage:nil];
}
}else if ( i == 1) {
// locate video , get the first image of video
AVURLAsset *avAsset = nil;
avAsset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:_urlArr[i]]];
if (avAsset) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:avAsset];
generator.appliesPreferredTrackTransform = YES;
NSError *error = nil;
CGImageRef cgImage = [generator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:NULL error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image = [UIImage imageWithCGImage:cgImage];
});
});
}
}else {
[imageView sd_setImageWithURL:[NSURL URLWithString:_urlArr[i]] placeholderImage:nil];
}

imageView.backgroundColor = [UIColor grayColor];

NSInteger row = i / 3;
NSInteger col = i % 3;
CGFloat x = 10 + col * (10 + width);
CGFloat y = 10 + row * (10 + width);
imageView.frame = CGRectMake(x, y, width, width);

KNPhotoItems *items = [[KNPhotoItems alloc] init];
items.sourceView = imageView;

if(i == 2 || i == 3 || i == 1){
items.isVideo = true;
items.url = _urlArr[i];
if (i == 2) {
items.videoPlaceHolderImageUrl = self.videoPlaceHolderUrl0;
}
if (i == 3) {
items.videoPlaceHolderImageUrl = self.videoPlaceHolderUrl1;
}
}else{
items.url = [_urlArr[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
}

[self.itemsArr addObject:items];
}else if (i == 7) {
NSData *data = [NSData dataWithContentsOfFile:_urlArr[i]];
SDAnimatedImage *animatedImage = [[SDAnimatedImage alloc] initWithData:data];
SDAnimatedImageView *imageView = [[SDAnimatedImageView alloc] init];
imageView.userInteractionEnabled = true;
[imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidClick:)]];
imageView.tag = i;
imageView.backgroundColor = [UIColor grayColor];
[orangeView addSubview:imageView];
imageView.image = animatedImage;

NSInteger row = i / 3;
NSInteger col = i % 3;
CGFloat x = 10 + col * (10 + width);
CGFloat y = 10 + row * (10 + width);
imageView.frame = CGRectMake(x, y, width, width);

KNPhotoItems *items = [[KNPhotoItems alloc] init];
items.sourceView = imageView;
items.isLocateGif = true;
items.url = [_urlArr[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
[self.itemsArr addObject:items];
}else if (i == 8) {
SDAnimatedImageView *imageView = [[SDAnimatedImageView alloc] init];
imageView.userInteractionEnabled = true;
[imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDidClick:)]];
imageView.tag = i;
imageView.backgroundColor = [UIColor grayColor];
[orangeView addSubview:imageView];
[imageView sd_setImageWithURL:_urlArr[i] placeholderImage:nil];

NSInteger row = i / 3;
NSInteger col = i % 3;
CGFloat x = 10 + col * (10 + width);
CGFloat y = 10 + row * (10 + width);
imageView.frame = CGRectMake(x, y, width, width);

KNPhotoItems *items = [[KNPhotoItems alloc] init];
items.sourceView = imageView;
items.url = [_urlArr[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
[self.itemsArr addObject:items];
}
}
}

- (void)imageViewDidClick:(UITapGestureRecognizer *)tap {

KNPhotoBrowser *photoBrowser = [[KNPhotoBrowser alloc] init];

/// custom ActionBar must set value after `KNPhotoBrowser init`
[KNPhotoBrowserConfig share].isNeedCustomActionBar = true;

photoBrowser.delegate = self;
photoBrowser.itemsArr = [self.itemsArr copy];
photoBrowser.placeHolderColor = UIColor.lightTextColor;
photoBrowser.currentIndex = tap.view.tag;
photoBrowser.isSoloAmbient = true;
photoBrowser.isNeedPageNumView = true;
photoBrowser.isNeedRightTopBtn = true;
photoBrowser.isNeedLongPress = true;
photoBrowser.isNeedPanGesture = true;
photoBrowser.isNeedPrefetch = true;
photoBrowser.isNeedAutoPlay = true;
photoBrowser.isNeedOnlinePlay = true;

[photoBrowser present];
}

- (KNPhotoAVPlayerActionBar *)photoBrowserCustomActionBar:(KNPhotoBrowser *)photoBrowser {
/// custom video player progress bar
/// create a subClass of `KNPhotoAVPlayerActionBar`
KNPhotoAVPlayerActionSubBar *actionBar = [[KNPhotoAVPlayerActionSubBar alloc] init];
actionBar.backgroundColor = UIColor.cyanColor;
return actionBar;
}

@end
Loading

0 comments on commit 573b01d

Please sign in to comment.