Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复ZFPlayerControlViewiOS15以后系统音量变化无法显示音量条问题 #1428

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions ZFPlayer/Classes/ControlView/ZFPlayerControlView.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,17 @@ - (instancetype)initWithFrame:(CGRect)frame {
self.horizontalPanShowControlView = YES;
self.autoFadeTimeInterval = 0.25;
self.autoHiddenTimeInterval = 2.5;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
if (@available(iOS 15, *)) {

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(volumeChanged:)
name:@"SystemVolumeDidChange" object:nil];
}else{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
}
}
return self;
}
Expand Down Expand Up @@ -165,7 +172,11 @@ - (void)layoutSubviews {
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
if (@available(iOS 15, *)) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"SystemVolumeDidChange" object:nil];
}else{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
}
[self cancelAutoFadeOutControlView];
}

Expand Down Expand Up @@ -243,17 +254,27 @@ - (void)showControlViewWithAnimated:(BOOL)animated {
}

/// 音量改变的通知
- (void)volumeChanged:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo;
NSString *reasonstr = userInfo[@"AVSystemController_AudioVolumeChangeReasonNotificationParameter"];
if ([reasonstr isEqualToString:@"ExplicitVolumeChange"]) {
float volume = [ userInfo[@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
- (void)volumeChanged:(NSNotification *)notification {
//iOS 15以前的Key
NSString *reasonKey = @"AudioVolumeChangeReason";
NSString *volumeKey = @"AudioVolume";
//iOS 15以后的Key
if (@available(iOS 15, *)) {
reasonKey = @"Reason";
volumeKey = @"Volume";
}

if (![[[notification userInfo] objectForKey:reasonKey] isEqualToString:@"ExplicitVolumeChange"]) {
return;
}
float volume = [[[notification userInfo] objectForKey:volumeKey] floatValue];
dispatch_async(dispatch_get_main_queue(), ^{
if (self.player.isFullScreen) {
[self.volumeBrightnessView updateProgress:volume withVolumeBrightnessType:ZFVolumeBrightnessTypeVolume];
} else {
[self.volumeBrightnessView addSystemVolumeView];
}
}
});
}

#pragma mark - Public Method
Expand Down