React native: volume buttons press (not for volume control)

How to determine if buttons have been pressed +

or -

on iOS / Android?

+3


source to share


1 answer


I think this is useful for 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");
        }
    }

      



And 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);
        }
    }

      

0


source







All Articles