【问题标题】:Get notification on volume button press event in ios [duplicate]在ios中获取有关音量按钮按下事件的通知[重复]
【发布时间】:2016-10-25 04:07:36
【问题描述】:

当我按三下音量按钮时,它需要显示通知。我想将此功能集成到我的应用程序中。

任何帮助将不胜感激。在此先感谢。

【问题讨论】:

标签: ios objective-c


【解决方案1】:

首先,您初始化 AVAudioSession 并通过以下方式添加监听器:

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

然后您通过以下方式存储当前系统卷:

float currentVolume = [audioSession outputVolume];

然后观察音量变化通知:

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqual:@"outputVolume"]) {
        float newVolume = [[AVAudioSession sharedInstance] outputVolume];
    }
}

您比较 newVolume 和 currentVolume 以确定预期结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 2015-07-28
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多