Google Cast SDK3 Android sample app is crashing on device below 5.0

后端 未结 1 988
野的像风
野的像风 2021-01-16 10:02

Ive tried the Google Cast Android sample app, and is crashing for device below 5.0. Anyone has any idea why? below is my crash log:

08­30 12:38:57.242: E/AndroidRun

相关标签:
1条回答
  • 2021-01-16 10:47

    There was a change in the latest Cast SDK that made it incompatible (crashing) with older versions of Google Play Services. Unfortunately, even cast sample app crashes when using latest Cast SDK with outdated GPS (or on emulators). The issue has been discussed here: https://github.com/googlecast/CastVideos-android/issues/12

    The solution is to check Google Play Services version before initialising any cast components, including mini controller (i.e. you can't just put mini controller fragment into your xml layout file - you either have to inflate it dynamically, or have two layout files - one with, and one without your mini controller).

    Code to check GPS version:

    GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
    int resultCode = apiAvailability.isGooglePlayServicesAvailable(context);
    isGPSAvailable = (resultCode == ConnectionResult.SUCCESS);
    

    If the results is not ConnectionResult.SUCCESS, don't initialise your MiniControllerFragment and do not access CastContext.

    Also, please keep in mind that it is not possible to instantiate MiniControllerFragment using new MiniControllerFragment(). You have to inflate it from xml or you will get NullPointerException.

    There are two ways to inflate MiniControllerFragment:

    1. Create separate xml layout files and inflate appropriate one in your Activity.onCreate:

      setContentView(isGPSAvailable ? R.layout.activity_main_with_controller : R.layout.activity_main_without_controller);
      
    2. Create ViewStub in your layout pointing to MiniControllerFragment and inflate it only when you have play services.

    activity layout:

    <ViewStub
    android:id="@+id/cast_minicontroller"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout="@layout/cast_mini_controller_fragment"
    />
    

    cast_mini_controller_fragment:

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/castMiniController"
              class="com.google.android.gms.cast.framework.media.widget.MiniControllerFragment"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:visibility="gone"/>
    

    code in your activity onCreate():

    ViewStub miniControllerStub = (ViewStub) findViewById(R.id.cast_minicontroller);
    if (isGPSAvailable) {
        miniControllerStub.inflate();
    }
    

    I prefer the ViewStub approach as it does not duplicate your layouts.

    0 讨论(0)
提交回复
热议问题