android - ThemedReactContext 无法在 JwPlayer 的 React Native 插件中转换为 Activity
问题描述
我已经为 react native 实现了 JwPlayer Android Plugin,它工作正常。但是当设备受到干扰(移动/旋转等)时,它会崩溃。
崩溃日志
2018-10-30 17:48:02.746 9935-9935/com.jwplayerdemo E/SensorManager: Exception dispatching input event.
2018-10-30 17:48:02.751 9935-9935/com.jwplayerdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jwplayerdemo, PID: 9935
java.lang.ClassCastException: com.facebook.react.uimanager.ThemedReactContext cannot be cast to android.app.Activity
at com.longtailvideo.jwplayer.b.j$1.onOrientationChanged(SourceFile:30)
at android.view.OrientationEventListener$SensorEventListenerImpl.onSensorChanged(OrientationEventListener.java:143)
at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:832)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:142)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
环境
react: "16.6.0-alpha.8af6728"
react-native: "0.57.3"
buildToolsVersion = "27.0.3"
minSdkVersion = 16
compileSdkVersion = 27
targetSdkVersion = 26
supportLibVersion = "27.1.1"
JwPlayerView
管理器类的视图管理器类
public class JwPlayerWrapperManager extends SimpleViewManager<JwPlayerView> {
public static final String REACT_CLASS = "JwPlayerWrapper";
@Override
public String getName() {
return REACT_CLASS;
}
@Override
public JwPlayerView createViewInstance(ThemedReactContext context){
return new JwPlayerView(context);
}
}
JwplayerView 类初始化 JwPlayer
View Instance JwPlayerView.class
public class JwPlayerView extends RelativeLayout {
private JWPlayerView playerView;
private PlayerConfig playerConfig;
private ReactContext reactContext;
public JwPlayerView(ThemedReactContext context) {
this(context, null);
}
public JwPlayerView(final ThemedReactContext context, AttributeSet attrs) {
super(context, attrs);
this.setBackgroundColor(Color.BLACK);
reactContext = context;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.jwplayercontainer, null);
playerConfig = new PlayerConfig.Builder()
.file("http://www.example.video.mp4")
.autostart(true)
.build();
playerView = new JWPlayerView(context, playerConfig);
ViewGroup jwPlayerViewContainer = v.findViewById(R.id.playerView);
jwPlayerViewContainer.addView(playerView);
this.addView(v);
}
@Override
public void requestLayout() {
super.requestLayout();
post(measureAndLayout); //Render video surface
}
private final Runnable measureAndLayout = new Runnable() {
@Override
public void run() {
measure(
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
layout(getLeft(), getTop(), getRight(), getBottom());
}
};
}
JwPlayer 注册查看管理器
ViewManager
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.<ViewManager>asList(
new JwPlayerWrapperManager()
);
}
解决方案
如果有人偶然发现这个问题;
为了修复它,通过:
reactContext.reactApplicationContext.currentActivity
到需要活动的组件。
推荐阅读
- c# - 禁止在文本框中输入无效字符列表的自定义验证属性
- c# - iOS 13 中的 Xamarin 基本共享
- php - php mysqli 表单 - 从选择下拉更新中填写输入
- php - 我该怎么做preg_replace?
- ffmpeg - 为什么这个 mp4 不能在 video-js 中播放
- php - 使用正则表达式获取字符串的结尾
- python - 无法导入 X 问题。Oregonator 模型的刚性 ODE 求解器
- python - 如何运行 BigQuery 查询,然后将输出 CSV 发送到 Apache Airflow 中的 Google Cloud Storage?
- batch-file - 2 个几乎相同的脚本,一个有效,一个无效。为什么?
- python - Python 包管理器和 Node.js