2

I've created a Class to show an alert when the volume is 0. It should hide if the user changes the volume. But it doesn't react to volume changes. I have added the UISlider and started the AVAudioSession. What is missing?

#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);
    }
}

This function is never called ↓↓↓↓↓↓

- (void)volumeChanged:(NSNotification *)notification
{
    [self.volumeHud hide:YES];
}

↑↑↑↑↑↑↑↑↑↑↑↑↑

-(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
Luda
  • 7,282
  • 12
  • 79
  • 139
  • I have no direct answer for your question - but there's a different way of detecting volume changes based on key-value-coding. It's described here: https://stackoverflow.com/a/22601280 – Lutz Oct 22 '18 at 12:21

2 Answers2

2

Try using KV0, it works for me:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [[AVAudioSession sharedInstance] setActive:YES error:nil];
   [[AVAudioSession sharedInstance] addObserver:self forKeyPath:@"outputVolume" options:0 context:nil];


    return YES;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"Volumechanged");
}

Note that you need the line: [[AVAudioSession sharedInstance] setActive:YES error:nil];

if omitted you will not get notifications. Also import AVFoundation.

As stated here: https://developer.apple.com/documentation/avfoundation/avaudiosession/1616533-outputvolume?language=objc

"You can observe changes to the value of this property by using Key-value observing."

joan
  • 2,407
  • 4
  • 29
  • 35
1

Apparently, my code worked ok. The problem was with my laptop - it had some volume issue, some when I was changing the volume on the simulator, the volume didn't really changed. When I switched to a real device, everything worked properly

Luda
  • 7,282
  • 12
  • 79
  • 139