Я создал класс для отображения оповещения, когда громкость равна 0. Он должен скрываться, если пользователь изменяет громкость.Но это не реагирует на изменения объема.Я добавил UISlider и начал AVAudioSession.Чего не хватает?
#import "VolumeToast.h"
#import <MediaPlayer/MediaPlayer.h>
#import "MBProgressHUD.h"
@interface VolumeToast()
@property (strong, nonatomic) MBProgressHUD *volumeHud;
@property (strong, nonatomic) UISlider *volumeViewSlider; //Added for volume observations
@end
@implementation VolumeToast
- (void)showVolumeAlertIfNeededOnView:(UIView *)view {
if ([[AVAudioSession sharedInstance] outputVolume] == 0)
{
[self showToast:[NSString stringWithFormat:@"Increase volume to enjoy this game"] removeAfter:6.f shouldDisableScreen:FALSE onView:view];
[self setupVolumeObserver];
}
}
-(void)showToast:(NSString*)message removeAfter:(NSTimeInterval)timeout shouldDisableScreen: (BOOL)shouldDisableScreen onView: (UIView *)view
{
self.volumeHud = [MBProgressHUD showHUDAddedTo:view animated:YES];
self.volumeHud.mode = MBProgressHUDModeText;
self.volumeHud.labelText = message;
self.volumeHud.yOffset = view.frame.size.height/2 - 50;
[self.volumeHud hide:YES afterDelay:timeout];
self.volumeHud.userInteractionEnabled = shouldDisableScreen;
}
- (void)setupVolumeObserver
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
MPVolumeView *volumeView = [[MPVolumeView alloc] init];
for (UIView *view in [volumeView subviews]) {
if ([view.class.description isEqualToString:@"MPVolumeSlider"]) {
self.volumeViewSlider = (UISlider *)view;
[self addSubview:volumeView];
break;
}
}
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
NSError *error;
BOOL success = [audioSession setActive:YES error:&error];
if (!success) {
NSLog(@"Error activating audiosession: %@", error);
}
}
Эта функция никогда не вызывается ↓↓↓↓↓↓
- (void)volumeChanged:(NSNotification *)notification
{
[self.volumeHud hide:YES];
}
↑↑↑↑↑↑↑↑101
-(void)hide
{
[self.volumeHud hide:YES];
[self stopTrackingVolumeChanges];
}
- (void)stopTrackingVolumeChanges
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error;
BOOL success = [[AVAudioSession sharedInstance] setActive:NO error:&error];
if (!success) {
NSLog(@"Error deactivating audiosession: %@", error);
}
});
self.volumeViewSlider = nil;
}
@end