Google Cast Sdk3 Android Sample App Is Crashing On Device Below 5.0
Solution 1:
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:
GoogleApiAvailabilityapiAvailability= GoogleApiAvailability.getInstance();
intresultCode= 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
:
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);
Create
ViewStub
in your layout pointing toMiniControllerFragment
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"?><fragmentxmlns: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()
:
ViewStubminiControllerStub= (ViewStub) findViewById(R.id.cast_minicontroller);
if (isGPSAvailable) {
miniControllerStub.inflate();
}
I prefer the ViewStub
approach as it does not duplicate your layouts.
Post a Comment for "Google Cast Sdk3 Android Sample App Is Crashing On Device Below 5.0"