1313static _VBPAVAudioSessionObserver *observer = NULL ;
1414
1515@interface _VBPAVAudioSessionObserver : NSObject {
16- float _currentVolumeLevel;
17- NSMutableArray <NSString *> *_gameObjectNames;
18- MPVolumeView *_volumeView;
16+ float _currentVolumeLevel;
17+ NSMutableArray <NSString *> *_gameObjectNames;
18+ MPVolumeView *_volumeView;
1919}
2020
2121-(void ) addGameObject : (NSString *)objectName ;
@@ -27,96 +27,96 @@ -(void) removeGameObject:(NSString *)objectName;
2727@implementation _VBPAVAudioSessionObserver
2828
2929-(id ) init {
30- if (self = [super init ]) {
31- _currentVolumeLevel = [[AVAudioSession sharedInstance ] outputVolume ];
32- _gameObjectNames = [NSMutableArray new ];
33- }
30+ if (self = [super init ]) {
31+ _currentVolumeLevel = [[AVAudioSession sharedInstance ] outputVolume ];
32+ _gameObjectNames = [NSMutableArray new ];
33+ }
3434
35- return self;
35+ return self;
3636}
3737
3838-(void ) addGameObject : (NSString *)objectName {
39- @synchronized (_gameObjectNames) {
40- if (_gameObjectNames.count == 0 ) {
41- AVAudioSession *session = [AVAudioSession sharedInstance ];
42- if (!session.isOtherAudioPlaying ) {
43- [session setActive: YES error: nil ];
44- }
45- [session addObserver: self forKeyPath: @" outputVolume" options: NSKeyValueObservingOptionNew context: nil ];
46- }
47-
48- [_gameObjectNames addObject: objectName];
39+ @synchronized (_gameObjectNames) {
40+ if (_gameObjectNames.count == 0 ) {
41+ AVAudioSession *session = [AVAudioSession sharedInstance ];
42+ if (!session.isOtherAudioPlaying ) {
43+ [session setActive: YES error: nil ];
44+ }
45+ [session addObserver: self forKeyPath: @" outputVolume" options: NSKeyValueObservingOptionNew context: nil ];
4946 }
47+
48+ [_gameObjectNames addObject: objectName];
49+ }
5050}
5151
5252- (void )observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary *)change context : (void *)context {
53- if ([@" outputVolume" isEqualToString: keyPath]) {
54- float newVolumeLevel = [[AVAudioSession sharedInstance ] outputVolume ];
55- @synchronized (_gameObjectNames) {
56- for (NSString *gameObjectName in _gameObjectNames) {
57- UnitySendMessage ([gameObjectName cStringUsingEncoding: NSUTF8StringEncoding], kMessageName , (newVolumeLevel > _currentVolumeLevel ? kVolumeUp : kVolumeDown ));
58- }
59- }
60-
61- // https://forums.developer.apple.com/thread/107241
62- if (newVolumeLevel > 0 .999f ) {
63- _currentVolumeLevel = 0 .9375f ;
64- [self hackSystemVolume: _currentVolumeLevel];
65- } else if (newVolumeLevel < 0 .001f ) {
66- _currentVolumeLevel = 0 .0625f ;
67- [self hackSystemVolume: _currentVolumeLevel];
68- } else {
69- _currentVolumeLevel = newVolumeLevel;
70- }
53+ if ([@" outputVolume" isEqualToString: keyPath]) {
54+ float newVolumeLevel = [[AVAudioSession sharedInstance ] outputVolume ];
55+ @synchronized (_gameObjectNames) {
56+ for (NSString *gameObjectName in _gameObjectNames) {
57+ UnitySendMessage ([gameObjectName cStringUsingEncoding: NSUTF8StringEncoding], kMessageName , (newVolumeLevel > _currentVolumeLevel ? kVolumeUp : kVolumeDown ));
58+ }
59+ }
60+
61+ // https://forums.developer.apple.com/thread/107241
62+ if (newVolumeLevel > 0 .999f ) {
63+ _currentVolumeLevel = 0 .9375f ;
64+ [self hackSystemVolume: _currentVolumeLevel];
65+ } else if (newVolumeLevel < 0 .001f ) {
66+ _currentVolumeLevel = 0 .0625f ;
67+ [self hackSystemVolume: _currentVolumeLevel];
7168 } else {
72- [ super observeValueForKeyPath: keyPath ofObject: object change: change context: context] ;
69+ _currentVolumeLevel = newVolumeLevel ;
7370 }
71+ } else {
72+ [super observeValueForKeyPath: keyPath ofObject: object change: change context: context];
73+ }
7474}
7575
7676-(void ) removeGameObject : (NSString *)objectName {
77- @synchronized (_gameObjectNames) {
78- [_gameObjectNames removeObject: objectName];
77+ @synchronized (_gameObjectNames) {
78+ [_gameObjectNames removeObject: objectName];
7979
80- if (_gameObjectNames.count == 0 ) {
81- AVAudioSession *session = [AVAudioSession sharedInstance ];
82- [session removeObserver: self forKeyPath: @" outputVolume" ];
83- }
80+ if (_gameObjectNames.count == 0 ) {
81+ AVAudioSession *session = [AVAudioSession sharedInstance ];
82+ [session removeObserver: self forKeyPath: @" outputVolume" ];
8483 }
84+ }
8585}
8686
8787-(void )hackSystemVolume : (float )volumeLevel {
88- UISlider *view = [[[[MPVolumeView new ] subviews ] filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @" classForCoder == \" MPVolumeSlider\" " ]] firstObject ];
89- if ([view isKindOfClass: [UISlider class ]]) {
90- UISlider *slider = (UISlider *)view;
91- [slider setValue: volumeLevel];
92- }
88+ UISlider *view = [[[[MPVolumeView new ] subviews ] filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @" classForCoder == \" MPVolumeSlider\" " ]] firstObject ];
89+ if ([view isKindOfClass: [UISlider class ]]) {
90+ UISlider *slider = (UISlider *)view;
91+ [slider setValue: volumeLevel];
92+ }
9393}
9494
9595@end
9696
9797void VBP_addGameObjectListener (const char *go_name, int go_name_length) {
98- if (!observer) {
99- observer = [_VBPAVAudioSessionObserver new ];
100- }
98+ if (!observer) {
99+ observer = [_VBPAVAudioSessionObserver new ];
100+ }
101101
102- // https://answers.unity.com/questions/363476/how-to-pass-unicode-text-from-c-to-objective-c.html
103- NSString * gon = [[NSString alloc ]
104- initWithBytes: go_name
105- length: sizeof (__CHAR16_TYPE__) * go_name_length
106- encoding: NSUTF16LittleEndianStringEncoding];
102+ // https://answers.unity.com/questions/363476/how-to-pass-unicode-text-from-c-to-objective-c.html
103+ NSString * gon = [[NSString alloc ]
104+ initWithBytes: go_name
105+ length: sizeof (__CHAR16_TYPE__) * go_name_length
106+ encoding: NSUTF16LittleEndianStringEncoding];
107107
108- [observer addGameObject: gon];
108+ [observer addGameObject: gon];
109109}
110110
111111void VBP_removeGameObjectListener (const char *go_name, int go_name_length) {
112- NSString * gon = [[NSString alloc ]
113- initWithBytes: go_name
114- length: sizeof (__CHAR16_TYPE__) * go_name_length
115- encoding: NSUTF16LittleEndianStringEncoding];
112+ NSString * gon = [[NSString alloc ]
113+ initWithBytes: go_name
114+ length: sizeof (__CHAR16_TYPE__) * go_name_length
115+ encoding: NSUTF16LittleEndianStringEncoding];
116116
117- [observer removeGameObject: gon];
117+ [observer removeGameObject: gon];
118118}
119119
120120float VBP_getSystemVolumeLevel (void ) {
121- return [[AVAudioSession sharedInstance ] outputVolume ];
121+ return [[AVAudioSession sharedInstance ] outputVolume ];
122122}
0 commit comments