我正在尝试向名为react-native-system-setting的现有React本机库中添加新功能,对于我的应用程序,我需要能够模拟关键事件,以如{{所述,在不同的媒体播放器上播放,暂停和跳过歌曲3}},所以我将函数“ MediaControl”添加到here库中:
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);
}
}
它可以在android studio上正常构建,但是当我尝试使用该功能时,出现错误:
undefined is not a function (evaluating '_reactNativeSystemSetting2.default.MediaControl("play")')
我错过了什么吗?