ios - 对音量按钮没有响应 - objective-c

标签 ios objective-c nsnotificationcenter avaudiosession

我创建了一个类,用于在音量为 0 时显示警报。如果用户更改音量,它应该隐藏。但它不会对音量变化使用react。我已经添加了 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

最佳答案

尝试使用 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 错误:nil];

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

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

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

关于ios - 对音量按钮没有响应 - objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52928798/

相关文章:

ios - 如何将中文 Html String 转换成 NSString

ios - NSDictionary 填充 JSON 数据

java - 如何将 NSDate 转换为整数以将 PHP date() 作为 unix 时间戳传递?

ios - UIButton 在 CAKeyframeAnimation 之后没有收到触摸

ios - 我将如何在加载 tableView 时运行一些代码?

ios - 单击按钮时如何以编程方式 ScrollView

python - NSUserNotificationCenter.defaultUserNotificationCenter() 在 python 中返回 None

nsnotificationcenter - Mountain Lion 上的同步 NSUserNotification 警报

ios - 1 个物理对象上的多个碰撞位掩码?

ios - 在 Xcode 10.2 中调整 View 大小