不响应音量按钮-目标C

时间:2018-10-22 11:58:17

标签: ios objective-c nsnotificationcenter avaudiosession

我创建了一个Class在音量为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];
}

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

-(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

2 个答案:

答案 0 :(得分:2)

显然,我的代码运行正常。问题出在我的笔记本电脑上–它有一些音量问题,有些是在模拟器上更改音量时,音量没有真正改变。当我切换到真实设备时,一切正常

答案 1 :(得分:0)

尝试使用KV0,它对我有用:

- (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");
}

请注意,您需要以下行:  [[AVAudioSession sharedInstance] setActive:YES错误:无];

如果省略,您将不会收到通知。 还要导入AVFoundation。

如此处所述:  https://developer.apple.com/documentation/avfoundation/avaudiosession/1616533-outputvolume?language=objc

“您可以通过键值观察来观察此属性值的变化。”