无法在JwPlayer的React Native插件中将ThemedReactContext强制转换为Activity

时间:2018-10-30 13:50:46

标签: android react-native react-native-android jwplayer

我已经实现了JwPlayer Android插件来响应本机,并且运行良好。 但是当设备受到干扰(移动/旋转等)时,它就会崩溃。

崩溃日志

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
查看实例 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()
        );
    }

1 个答案:

答案 0 :(得分:0)

如果有人遇到这个问题;

为了修复它通过:

reactContext.reactApplicationContext.currentActivity

到需要活动的组件。