2016-10-25 110 views
1

當我按下音量按鈕三次需要顯示通知。我想在我的應用程序中集成此功能。在ios上獲取音量按鈕按下事件的通知

任何幫助將不勝感激。提前感謝。

+0

您需要警報/本地通知嗎? –

+0

@JamshedAlam我想發送消息到特定數量的音量按鈕按 – NilamPari

+0

可能是此鏈接將幫助您瞭解有關音量事件:http://stackoverflow.com/questions/28193626/cleanest-way-of-capturing-音量 - 上 - 下 - 按鈕 - 按下 - ios - 8 –

回答

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以確定預期結果。