ZXVideoPlayerVolumeView.m
4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// ZXVideoPlayerVolumeView.m
// ZXVideoPlayer
//
// Created by Shawn on 16/4/22.
// Copyright © 2016年 Shawn. All rights reserved.
//
#import "ZXVideoPlayerVolumeView.h"
#import "ZXVideoPlayerControlView.h"
static const CGFloat kViewSpacing = 21.0;
static const CGFloat kVolumeIndicatorAutoFadeOutTimeInterval = 1.0;
@interface ZXVideoPlayerVolumeView ()
@property (nonatomic, strong, readwrite) NSMutableArray *blocksArray;
@property (nonatomic, strong, readwrite) UIImageView *volumeImageView;
@end
@implementation ZXVideoPlayerVolumeView
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.hidden = YES;
self.layer.cornerRadius = 5;
self.layer.masksToBounds = YES;
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
[self createVolumeIndicator];
[self configVolumeNotification];
}
return self;
}
- (void)createVolumeIndicator
{
// 音量图标
_volumeImageView = [[UIImageView alloc] initWithFrame:CGRectMake((kVideoVolumeIndicatorViewSide - 50) / 2, kViewSpacing, 50, 50)];
[_volumeImageView setImage:[UIImage imageNamed:@"zx-video-player-volume"]];
[self addSubview:_volumeImageView];
// 音量条
self.blocksArray = [NSMutableArray arrayWithCapacity:16];
UIView *blockBackgroundView = [[UIView alloc] initWithFrame:CGRectMake((kVideoVolumeIndicatorViewSide - 105) / 2, 50 + kViewSpacing * 2, 105, 2.75 + 2)];
blockBackgroundView.backgroundColor = [UIColor colorWithRed:0.25f green:0.22f blue:0.21f alpha:0.65];
[self addSubview:blockBackgroundView];
CGFloat margin = 1;
CGFloat blockW = 5.5;
CGFloat blockH = 2.75;
for (int i = 0; i < 16; i++) {
CGFloat locX = i * (blockW + margin) + margin;
UIImageView *blockView = [[UIImageView alloc] init];
blockView.backgroundColor = [UIColor whiteColor];
blockView.frame = CGRectMake(locX, margin, blockW, blockH);
[blockBackgroundView addSubview:blockView];
[self.blocksArray addObject:blockView];
}
}
- (void)configVolumeNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
}
- (void)volumeChanged:(NSNotification *)notification
{
float outputVolume = [[[notification userInfo] objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
[self updateVolumeIndicator:outputVolume];
}
- (void)updateVolumeIndicator:(CGFloat)value
{
self.hidden = NO;
// 防止重叠显示
if (self.superview.accessibilityIdentifier) {
ZXVideoPlayerControlView *playerView = (ZXVideoPlayerControlView *)self.superview;
playerView.timeIndicatorView.hidden = YES;
playerView.brightnessIndicatorView.hidden = YES;
} else {
self.superview.accessibilityIdentifier = @"";
}
CGFloat stage = 1 / 16.0;
NSInteger level = value / stage;
for (NSInteger i=0; i<self.blocksArray.count; i++) {
UIImageView *img = self.blocksArray[i];
if (i <= level) {
img.hidden = NO;
} else {
img.hidden = YES;
}
}
if (value == 0.0) {
[self.blocksArray[0] setHidden:YES];
if (!self.volumeImageView.accessibilityIdentifier) {
self.volumeImageView.accessibilityIdentifier = @"";
self.volumeImageView.image = [UIImage imageNamed:@"zx-video-player-volumeMute"];
}
} else {
if (self.volumeImageView.accessibilityIdentifier) {
self.volumeImageView.accessibilityIdentifier = nil;
self.volumeImageView.image = [UIImage imageNamed:@"zx-video-player-volume"];
}
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(animateHide) object:nil];
[self performSelector:@selector(animateHide) withObject:nil afterDelay:kVolumeIndicatorAutoFadeOutTimeInterval];
}
- (void)animateHide
{
[UIView animateWithDuration:.3 animations:^{
self.alpha = 0;
} completion:^(BOOL finished) {
self.hidden = YES;
self.alpha = 1;
self.superview.accessibilityIdentifier = nil;
}];
}
@end