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

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

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:

回答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:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!