不响应音量按钮 - 目标 c

Posted

技术标签:

【中文标题】不响应音量按钮 - 目标 c【英文标题】:Not responding to volume buttons - objective c 【发布时间】:2018-10-22 11:58:17 【问题描述】:

我创建了一个类来在音量为 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

【问题讨论】:

我无法直接回答您的问题 - 但是有一种不同的方法可以根据键值编码检测音量变化。这里有描述:***.com/a/22601280 【参考方案1】:

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

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

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

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

【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于不响应音量按钮 - 目标 c的主要内容,如果未能解决你的问题,请参考以下文章

带有 IBAction 的按钮导致“无法连接操作,目标类 NSNibExternalObjectPlaceholder 不响应 -action”错误

C/C++ 视频播放器开发

按钮没有响应

导航栏中的 SwiftUI 元素不响应状态

UIButton touch在目标c的xcode 4.5中没有响应[关闭]

闪亮,两个动作按钮,它只响应第二个按钮而不响应第一个按钮