Skip to content

Commit

Permalink
Release 3.4.5
Browse files Browse the repository at this point in the history
- 对`SJAliMediaPlayer`添加清晰度切换的`selectTrack`的支持
- 完善 长按手势结束后使其恢复为触发时的速率
  • Loading branch information
changsanjiang committed Dec 10, 2020
1 parent 1246acc commit 1d8cc51
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 2 deletions.
2 changes: 1 addition & 1 deletion SJBaseVideoPlayer.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'SJBaseVideoPlayer'
s.version = '3.4.4'
s.version = '3.4.5'
s.summary = 'video player.'
s.description = 'https://github.com/changsanjiang/SJBaseVideoPlayer/blob/master/README.md'
s.homepage = 'https://github.com/changsanjiang/SJBaseVideoPlayer'
Expand Down
6 changes: 6 additions & 0 deletions SJBaseVideoPlayer/AliPlayer/Core/SJAliMediaPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "SJMediaPlaybackController.h"
#import <AliyunPlayer/AVPMediaInfo.h>
#import <AliyunPlayer/AVPSource.h>
#import <AliyunPlayer/AVPConfig.h>
#import <AliyunPlayer/AVPDef.h>
Expand All @@ -26,8 +27,13 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong, readonly) __kindof AVPSource *source;
@property (nonatomic, readonly) BOOL firstVideoFrameRendered;
@property (nonatomic, strong, readonly) UIView *view;
@property (nonatomic, readonly, nullable) NSArray<AVPTrackInfo *> *trackInfos;
- (nullable AVPTrackInfo *)currentTrackInfo:(AVPTrackType)type;
- (void)selectTrack:(int)trackIndex accurateSeeking:(BOOL)accurateSeeking completed:(void(^)(BOOL finished))completionHandler;

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
@end

extern NSNotificationName const SJAliMediaPlayerOnTrackReadyNotification;
NS_ASSUME_NONNULL_END
32 changes: 32 additions & 0 deletions SJBaseVideoPlayer/AliPlayer/Core/SJAliMediaPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @interface SJAliMediaPlayer ()<AVPDelegate>
@property (nonatomic, nullable) SJFinishedReason finishedReason; ///< 播放结束的reason
@property (nonatomic) BOOL firstVideoFrameRendered;
@property (nonatomic, copy, nullable) void(^seekCompletionHandler)(BOOL);
@property (nonatomic, copy, nullable) void(^selectTrackCompletionHandler)(BOOL);
@property (nonatomic) NSTimeInterval startPosition;
@property (nonatomic) BOOL needSeekToStartPosition;
@property (nonatomic, nullable) SJWaitingReason reasonForWaitingToPlay;
Expand Down Expand Up @@ -91,6 +92,14 @@ - (UIView *)view {
return self.player.playerView;
}

- (nullable NSArray<AVPTrackInfo *> *)trackInfos {
return self.player.getMediaInfo.tracks;
}

- (nullable AVPTrackInfo *)currentTrackInfo:(AVPTrackType)type {
return [self.player getCurrentTrack:type];
}

- (void)seekToTime:(CMTime)time completionHandler:(void (^_Nullable)(BOOL))completionHandler {
if ( self.assetStatus != SJAssetStatusReadyToPlay ) {
if ( completionHandler ) completionHandler(NO);
Expand Down Expand Up @@ -152,6 +161,11 @@ - (nullable UIImage *)screenshot {
return nil;
}

- (void)selectTrack:(int)trackIndex accurateSeeking:(BOOL)accurateSeeking completed:(void(^)(BOOL finished))completionHandler {
[_player selectTrack:trackIndex accurate:accurateSeeking];
_selectTrackCompletionHandler = completionHandler;
}

#pragma mark -

-(void)onPlayerEvent:(AliPlayer *)player eventType:(AVPEventType)eventType {
Expand Down Expand Up @@ -249,6 +263,17 @@ - (void)onPlayerStatusChanged:(AliPlayer *)player oldStatus:(AVPStatus)oldStatus
});
}

- (void)onTrackReady:(AliPlayer *)player info:(NSArray<AVPTrackInfo *> *)info {
[self _postNotification:SJAliMediaPlayerOnTrackReadyNotification];
}

- (void)onTrackChanged:(AliPlayer *)player info:(AVPTrackInfo *)info {
if ( _selectTrackCompletionHandler != nil ) {
_selectTrackCompletionHandler(YES);
_selectTrackCompletionHandler = nil;
}
}

#pragma mark -

- (void)_postNotification:(NSNotificationName)name {
Expand Down Expand Up @@ -282,6 +307,11 @@ - (void)_toEvaluating {
if ( status != self.assetStatus ) {
self.assetStatus = status;

if ( _selectTrackCompletionHandler != nil ) {
_selectTrackCompletionHandler(false);
_selectTrackCompletionHandler = nil;
}

if ( status == SJAssetStatusReadyToPlay ) {
if ( self.needSeekToStartPosition ) {
self.needSeekToStartPosition = NO;
Expand Down Expand Up @@ -474,4 +504,6 @@ - (CMTime)_adjustSeekTimeIfNeeded:(CMTime)time {
return time;
}
@end

NSNotificationName const SJAliMediaPlayerOnTrackReadyNotification = @"SJAliMediaPlayerOnTrackReadyNotification";
NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "SJVideoPlayerURLAsset.h"
#import <AliyunPlayer/AVPSource.h>
#import <AliyunPlayer/AVPConfig.h>
#import <AliyunPlayer/AVPMediaInfo.h>

NS_ASSUME_NONNULL_BEGIN
@interface SJVideoPlayerURLAsset (SJAliMediaPlaybackAdd)
Expand All @@ -19,5 +20,13 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, strong, readonly, nullable) __kindof AVPSource *source;
@property (nonatomic, strong, nullable) AVPConfig *avpConfig;

@end

/// 切换清晰度时使用
@interface SJVideoPlayerURLAsset (SJAliMediaSelectTrack)
- (instancetype)initWithSource:(__kindof AVPSource *)source subTrackInfo:(AVPTrackInfo *)trackInfo;
- (instancetype)initWithSource:(__kindof AVPSource *)source subTrackInfo:(AVPTrackInfo *)trackInfo playModel:(__kindof SJPlayModel *)playModel;
@property (nonatomic, strong, readonly, nullable) AVPTrackInfo *avpTrackInfo;
@end
NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,23 @@ - (nullable AVPConfig *)avpConfig {
return objc_getAssociatedObject(self, _cmd);
}
@end

/// 切换清晰度时使用
@implementation SJVideoPlayerURLAsset (SJAliMediaSelectTrack)
- (instancetype)initWithSource:(__kindof AVPSource *)source subTrackInfo:(AVPTrackInfo *)trackInfo {
return [self initWithSource:source subTrackInfo:trackInfo playModel:SJPlayModel.new];
}
- (instancetype)initWithSource:(__kindof AVPSource *)source subTrackInfo:(AVPTrackInfo *)trackInfo playModel:(__kindof SJPlayModel *)playModel {
self = [self initWithSource:source playModel:playModel];
if ( self ) {
objc_setAssociatedObject(self, @selector(avpTrackInfo), trackInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return self;
}

- (nullable AVPTrackInfo *)avpTrackInfo {
return objc_getAssociatedObject(self, _cmd);
}
@end

NS_ASSUME_NONNULL_END
2 changes: 2 additions & 0 deletions SJBaseVideoPlayer/AliPlayer/SJAliMediaPlaybackController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface SJAliMediaPlaybackController : SJMediaPlaybackController
@property (nonatomic) AVPSeekMode seekMode;
@property (nonatomic, strong, readonly, nullable) SJAliMediaPlayer *currentPlayer;

@property (nonatomic, copy, nullable) void(^onTrackReadyExeBlock)(SJAliMediaPlaybackController *playbackController);
@end
NS_ASSUME_NONNULL_END
63 changes: 62 additions & 1 deletion SJBaseVideoPlayer/AliPlayer/SJAliMediaPlaybackController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "SJAliMediaPlaybackController.h"
#import "SJAliMediaPlayer.h"
#import "SJAliMediaPlayerLayerView.h"
#import "SJVideoPlayerURLAsset+SJAliMediaPlaybackAdd.h"

#if __has_include(<SJUIKit/SJRunLoopTaskQueue.h>)
#import <SJUIKit/SJRunLoopTaskQueue.h>
Expand All @@ -18,7 +19,7 @@

NS_ASSUME_NONNULL_BEGIN
@interface SJAliMediaPlaybackController ()

@property (nonatomic, strong, nullable) SJVideoPlayerURLAsset *avpTrackMedia;
@end

@implementation SJAliMediaPlaybackController
Expand All @@ -27,6 +28,8 @@ - (instancetype)init {
self = [super init];
if ( self ) {
_seekMode = AVP_SEEKMODE_INACCURATE;

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(_onTrackReadyWithNote:) name:SJAliMediaPlayerOnTrackReadyNotification object:nil];
}
return self;
}
Expand Down Expand Up @@ -79,5 +82,63 @@ - (SJPlaybackType)playbackType {
#endif
return SJPlaybackTypeUnknown;
}

#pragma mark - mark

- (void)_onTrackReadyWithNote:(NSNotification *)note {
if ( note.object == self.currentPlayer ) {
if ( self.onTrackReadyExeBlock ) self.onTrackReadyExeBlock(self);
}
}

- (void)switchVideoDefinition:(SJVideoPlayerURLAsset *)media {
AVPTrackInfo *trackInfo = media.avpTrackInfo;
if ( trackInfo == nil ) {
[super switchVideoDefinition:media];
return;
}

self.avpTrackMedia = media;
[self _avp_reportDefinitionSwitchStatusWithMedia:media status:SJDefinitionSwitchStatusUnknown];
[self _avp_reportDefinitionSwitchStatusWithMedia:media status:SJDefinitionSwitchStatusSwitching];
__weak typeof(self) _self = self;
[self.currentPlayer selectTrack:trackInfo.trackIndex accurateSeeking:_seekMode == AVP_SEEKMODE_ACCURATE ? YES : NO completed:^(BOOL finished) {
__strong typeof(_self) self = _self;
if ( !self ) return;
if ( media != self.avpTrackMedia ) return;
if ( !finished ) {
[self _avp_reportDefinitionSwitchStatusWithMedia:media status:SJDefinitionSwitchStatusFailed];
return;
}
[self replaceMediaForDefinitionMedia:media];
[self _avp_reportDefinitionSwitchStatusWithMedia:media status:SJDefinitionSwitchStatusFinished];
}];
}

- (void)_avp_reportDefinitionSwitchStatusWithMedia:(id<SJMediaModelProtocol>)media status:(SJDefinitionSwitchStatus)status {
if ( [self.delegate respondsToSelector:@selector(playbackController:switchingDefinitionStatusDidChange:media:)] ) {
[self.delegate playbackController:self switchingDefinitionStatusDidChange:status media:media];
}

#ifdef DEBUG
char *str = nil;
switch ( status ) {
case SJDefinitionSwitchStatusUnknown:
str = "Unknown";
break;
case SJDefinitionSwitchStatusSwitching:
str = "Switching";
break;
case SJDefinitionSwitchStatusFinished:
str = "Finished";
break;
case SJDefinitionSwitchStatusFailed:
str = "Failed";
break;
}
printf("SJAliMediaPlaybackController<%p>.switchStatus = %s\n", self, str);
#endif
}

@end
NS_ASSUME_NONNULL_END

0 comments on commit 1d8cc51

Please sign in to comment.