React native:捕捉音量按钮按下(不用于音量调节)

时间:2017-04-12 06:10:48

标签: android ios react-native

如何检测iOS / Android上是否按下+-按钮?

1 个答案:

答案 0 :(得分:0)

我认为它对iOS有帮助。

 - (void)viewWillAppear:(BOOL)animated {

        AVAudioSession* audioSession = [AVAudioSession sharedInstance];

        [audioSession setActive:YES error:nil];
        [audioSession addObserver:self
                       forKeyPath:@"outputVolume"
                          options:0
                          context:nil];
    }

    -(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

        if ([keyPath isEqual:@"outputVolume"]) {
            NSLog(@"volume changed");
        }
    }

Android。

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_DOWN) {
                //TODO
            }
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_DOWN) {
                //TODO
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }
相关问题