android - react native : catch volume buttons press (not for volume adjusting)

标签 android ios react-native

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

最佳答案

我认为它对 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");
        }
    }

还有安卓。

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

关于android - react native : catch volume buttons press (not for volume adjusting),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43361599/

相关文章:

android - OkHttp的enqueue方法是否实现了AsyncTaskLoader?

android - 我的 Android 应用程序占用太多内存?

java - CollapsingToolbarLayout 和 FloatingActionButton

ios - 使用 iPhone GPS 运行时获得最准确的 "current pace"

ios - XCode 同时调试主应用程序和今日扩展

android - 无法使用 android 将 react-native 更新到 0.59.2

android - 如何在 Android 中通过 URL 加载 ImageView?

ios - 如何制作 "required"UITextFields?

React-native 不存在包 URL

java - React-native 添加一个小部件到 android 主屏幕