Skip to content

Commit

Permalink
1.Optimize startup playback speed
Browse files Browse the repository at this point in the history
  • Loading branch information
ianyanzhang committed Apr 28, 2022
1 parent 23ffb31 commit 226afc5
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ - (void)setFeedData:(NSArray *)feedData isCleanData:(BOOL)isNeedCleanData {
[self.tableView reloadData];

if (isNeedCleanData) {
if (self.currentTableViewCell) {
[self.currentTableViewCell pause];
self.currentTableViewCell = nil;
}
[self playVideoInVisiableCells];
}
}
Expand Down Expand Up @@ -109,7 +113,9 @@ - (void)playVideoInVisiableCells {
}

- (void)playVideoFromCell:(FeedTableViewCell *)cell {
[self.currentTableViewCell pause];
if (self.currentTableViewCell) {
[self.currentTableViewCell pause];
}
self.currentTableViewCell = cell;

self.currentPlayIndex = [self indexOfModel:cell.model];
Expand Down
14 changes: 12 additions & 2 deletions Demo/TXLiteAVDemo/SuperPlayerKit/SuperPlayer/SuperPlayerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,7 @@ - (void)centerPlayBtnClick {
* 应用退到后台
*/
- (void)appDidEnterBackground:(NSNotification *)notify {
[self fastViewUnavaliable];
NSLog(@"appDidEnterBackground");
self.didEnterBackground = YES;
if (self.isLive) {
Expand All @@ -1136,6 +1137,7 @@ - (void)appDidEnterBackground:(NSNotification *)notify {
* 应用进入前台
*/
- (void)appDidEnterPlayground:(NSNotification *)notify {
[self fastViewUnavaliable];
NSLog(@"appDidEnterPlayground");
self.didEnterBackground = NO;
if (self.isLive) {
Expand All @@ -1148,7 +1150,6 @@ - (void)appDidEnterPlayground:(NSNotification *)notify {
CGFloat playable = _vodPlayer.playableDuration / _vodPlayer.duration;
self.controlView.isDragging = NO;
[self.controlView setProgressTime:self.playCurrentTime totalTime:_vodPlayer.duration progressValue:value playableValue:playable];
[self fastViewUnavaliable];
}
}

Expand Down Expand Up @@ -1683,6 +1684,11 @@ - (void)controlViewSwitch:(SuperPlayerControlView *)controlView withDefinition:(
}

[self.controlView setResolutionViewState:NO];

[self.vodPlayer setRate:self.playerConfig.playRate];
[self.vodPlayer setMirror:self.playerConfig.mirror];
[self.vodPlayer setMute:self.playerConfig.mute];
[self.vodPlayer setRenderMode:self.playerConfig.renderMode];
}

- (void)controlViewConfigUpdate:(SuperPlayerView *)controlView withReload:(BOOL)reload {
Expand Down Expand Up @@ -2012,8 +2018,12 @@ - (void)onPlayEvent:(int)EvtID withParam:(NSDictionary *)param {
NSString *desc = [param description];
NSLog(@"%@", [NSString stringWithCString:[desc cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding]);
}

if (EvtID == PLAY_EVT_RCV_FIRST_I_FRAME) {
self.state = StateFirstFrame;
}

if (EvtID == PLAY_EVT_PLAY_BEGIN || EvtID == PLAY_EVT_RCV_FIRST_I_FRAME) {
if (EvtID == PLAY_EVT_PLAY_BEGIN) {
if (!self.isLoaded) {
[self setNeedsLayout];
[self layoutIfNeeded];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,52 @@ @interface ShortVideoMainViewController ()

@property (nonatomic, strong) NSMutableArray *videosArray;

@property (nonatomic, assign) BOOL isLoadVideo;

@end

@implementation ShortVideoMainViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

self.isLoadVideo = NO;
[self.view addSubview:self.videoView];

WEAKIFY(self);
[self.videoView.viewModel refreshNewListWithsuccess:^(NSArray * _Nonnull list) {
STRONGIFY(self);
[self.videoView showLoading];

NSData *listData = [[NSUserDefaults standardUserDefaults] objectForKey:SHORT_VIDEO_CACHE_DATA_KEY];
NSArray <TXVideoModel *> *list = [NSKeyedUnarchiver unarchiveObjectWithData:listData];
if (list.count > 0) {
[self.videosArray removeAllObjects];
[self.videosArray addObjectsFromArray:list];
if (self.videoDataBlock) {
self.videoDataBlock(self.videosArray);
}
[self showVideoView];
} failure:^(NSError * _Nonnull error) {

}];

[self.videoView showLoading];
self.isLoadVideo = YES;
}

WEAKIFY(self);
dispatch_async(dispatch_get_global_queue(0,0), ^{
[self.videoView.viewModel refreshNewListWithsuccess:^(NSArray * _Nonnull list) {
STRONGIFY(self);
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:list];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:SHORT_VIDEO_CACHE_DATA_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.videosArray addObjectsFromArray:list];

if (!self.isLoadVideo) {
[self showVideoView];
if (self.videoDataBlock) {
self.videoDataBlock(self.videosArray);
}
}
} failure:^(NSError * _Nonnull error) {
if (!self.isLoadVideo) {
[self.videoView showNoNetView];
}
}];
});
}

- (void)viewWillAppear:(BOOL)animated {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,8 @@ - (void)viewDidLoad {
make.edges.equalTo(self.view);
}];

self.childVCs = @[self.mainVC, self.videoListVC];

[self.childVCs enumerateObjectsUsingBlock:^(UIViewController *vc, NSUInteger idx, BOOL * _Nonnull stop) {
[self addChildViewController:vc];
[self.mainScrollView addSubview:vc.view];

vc.view.frame = CGRectMake(idx * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}];
[self addChildViewController:self.videoListVC];
[self addChildViewController:self.mainVC];

WEAKIFY(self);
self.mainVC.videoDataBlock = ^(NSMutableArray * _Nonnull videoArray) {
Expand All @@ -52,6 +46,12 @@ - (void)viewDidLoad {
[self.videoListVC reloadData];
};

self.childVCs = @[self.mainVC, self.videoListVC];
[self.childVCs enumerateObjectsUsingBlock:^(UIViewController *vc, NSUInteger idx, BOOL * _Nonnull stop) {
[self.mainScrollView addSubview:vc.view];
vc.view.frame = CGRectMake(idx * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}];

self.videoListVC.selectedItemBlock = ^(NSInteger index) {
STRONGIFY(self);
[self.mainVC jumpToCellWithIndex:index];
Expand Down
49 changes: 49 additions & 0 deletions Demo/TXLiteAVDemo/UGCShortVideoPlayDemo/Model/TXVideoModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,54 @@
#import "TXVideoModel.h"

@implementation TXVideoModel
@synthesize name;
@synthesize videourl;
@synthesize width;
@synthesize height;
@synthesize duration;
@synthesize requestId;
@synthesize coverUrl;
@synthesize bitrateIndex;

#pragma mark NSCoping
- (id)copyWithZone:(NSZone *)zone {
TXVideoModel *copy = [[[self class] allocWithZone:zone] init];
copy.name = [self.name copyWithZone:zone];
copy.videourl = [self.videourl copyWithZone:zone];
copy.width = [self.width copyWithZone:zone];
copy.height = [self.height copyWithZone:zone];
copy.duration = [self.duration copyWithZone:zone];
copy.requestId = [self.requestId copyWithZone:zone];
copy.coverUrl = [self.coverUrl copyWithZone:zone];
copy.bitrateIndex = self.bitrateIndex;
return copy;
}

#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:name forKey:@"name"];
[aCoder encodeObject:videourl forKey:@"videourl"];
[aCoder encodeObject:width forKey:@"width"];
[aCoder encodeObject:height forKey:@"height"];
[aCoder encodeObject:duration forKey:@"duration"];
[aCoder encodeObject:requestId forKey:@"requestId"];
[aCoder encodeObject:coverUrl forKey:@"coverUrl"];
[aCoder encodeObject:@(bitrateIndex) forKey:@"bitrateIndex"];
}


- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.videourl = [aDecoder decodeObjectForKey:@"videourl"];
self.width = [aDecoder decodeObjectForKey:@"width"];
self.height = [aDecoder decodeObjectForKey:@"height"];
self.duration = [aDecoder decodeObjectForKey:@"duration"];
self.requestId = [aDecoder decodeObjectForKey:@"requestId"];
self.coverUrl = [aDecoder decodeObjectForKey:@"coverUrl"];
self.bitrateIndex = [[aDecoder decodeObjectForKey:@"bitrateIndex"] integerValue];
}
return self;
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@

#define DEFAULT_VIDEO_COUNT_SCREEN 1

#define SHORT_VIDEO_CACHE_DATA_KEY @"shortVideoCacheData"

#endif /* TXVideoPlayMem_h */
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "TXLiveBase.h"
#import "TXVodPlayer.h"
#import "TXVodPlayListener.h"
#import "TXPlayerGlobalSetting.h"

@interface TXVideoPlayer()<TXVodPlayListener>

Expand Down Expand Up @@ -158,11 +159,14 @@ - (TXVodPlayer *)player {
_player.enableHWAcceleration = YES;
_player.vodDelegate = self;
TXVodPlayConfig *config = [TXVodPlayConfig new];
config.maxBufferSize = 3;
config.maxBufferSize = 8;
NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [NSString stringWithFormat:@"%@/shortVideoCache",cachesDir];
[TXPlayerGlobalSetting setCacheFolderPath:path];
[TXPlayerGlobalSetting setMaxCacheSize:8];
config.maxBufferSize = 1;
config.smoothSwitchBitrate = NO;
_player.config = config;
[_player setRenderMode:RENDER_MODE_FILL_SCREEN];
[_player setRenderMode:RENDER_MODE_FILL_EDGE];
}
return _player;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSStr
return self;
}

- (void)prepareForReuse {
[super prepareForReuse];
[self.baseView.videoFatherView removeFromSuperview];
self.baseView.videoFatherView = nil;
}

- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
Expand Down Expand Up @@ -75,7 +81,8 @@ - (void)controlViewDidClickSelf:(TXVideoBaseView *)baseView {

- (void)seekToTime:(float)time {
if (self.delegate && [self.delegate respondsToSelector:@selector(seekToTime:)]) {
[self.delegate seekToTime:time];
float duration = [self.model.duration floatValue];
[self.delegate seekToTime:time * duration];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)showGuideView;

/**
* 显示网络异常控件
*/
- (void)showNoNetView;

/**
* 暂停
*/
Expand Down
Loading

0 comments on commit 226afc5

Please sign in to comment.