0

Im trying to add an new function to an existing react native library called react-native-system-setting, for my application i need to be able to simulate key events for playing, pausing and skipping songs on different media players as described here so i added the function "MediaControl" to the react-native-system-setting library:

public SystemSetting(ReactApplicationContext reactContext) {
    super(reactContext);
    mContext = reactContext;
    reactContext.addLifecycleEventListener(this);
    am = (AudioManager) mContext.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    wm = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    lm = (LocationManager) mContext.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

    listenVolume(reactContext);
}

@ReactMethod
public void MediaControl(String Command ) {
long eventtime = SystemClock.uptimeMillis();

    switch (Command) {
        case "next":
            KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
            am.dispatchMediaKeyEvent(downEvent);

            KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT, 0);
            am.dispatchMediaKeyEvent(upEvent);

        case "previous":
            KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
            am.dispatchMediaKeyEvent(downEvent);

            KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS, 0);
            am.dispatchMediaKeyEvent(upEvent);

        case "play":
            KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
            am.dispatchMediaKeyEvent(downEvent);

        case "pause":
            KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
            am.dispatchMediaKeyEvent(upEvent);
    }
}

it builds ok on android studio, but when i try to use the function i get an error:

undefined is not a function (evaluating '_reactNativeSystemSetting2.default.MediaControl("play")')

am i missing something?

  • Did you add your module to your ReactPackage? Do you have a custom ReactPackage? – Gabe Sechan Jul 05 '18 at 15:05
  • Also, you aren't passing in a Promise to the function. It may be possible to do it without it, but generally every ReactMethod's last parameter is a Promise that you resolve or reject when appropriate. – Gabe Sechan Jul 05 '18 at 15:07
  • It was a silly mistake, i forgot to declare the function on the javascript side of things, at node_modules\react-native-system-setting\SystemSetting.js – José Roberto Canuto Vasconcelo Jul 05 '18 at 15:20

0 Answers0