Skip to content

Instantly share code, notes, and snippets.

@EyMaddis
Created February 5, 2021 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EyMaddis/4ccbed64c25f6652477b719050e6d3ac to your computer and use it in GitHub Desktop.
Save EyMaddis/4ccbed64c25f6652477b719050e6d3ac to your computer and use it in GitHub Desktop.
React Native Google Cast MiniPlayer Base Implementation (Android)
package com.yourapp.cast;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.facebook.react.ReactActivity;
import com.facebook.react.bridge.ReactContext;
public class GoogleCastMiniPlayer extends FrameLayout {
public GoogleCastMiniPlayer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
public GoogleCastMiniPlayer(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public GoogleCastMiniPlayer(Context context) {
super(context);
initView();
}
private void initView() {
// FIXME: there are still cases where this can throw because of a duplicate ID (especially with react fast refresh!)
inflate(this.getContext(), R.layout.google_cast_miniplayer, this);
}
@Override
protected void onDetachedFromWindow() {
// required to handle react reloads. We need to destroy the old fragment because it uses a static id that would crash with "duplicate"
// There is only one instance of the fragment possible!
FragmentManager fm = ((ReactActivity) ((ReactContext) this.getContext()).getCurrentActivity()).getSupportFragmentManager();
Fragment xmlFragment = fm.findFragmentById(R.id.castMiniController);
if (xmlFragment != null) {
fm.beginTransaction().remove(xmlFragment).commit();
}
super.onDetachedFromWindow();
}
}
package com.yourapp.cast;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.google.android.gms.cast.framework.media.widget.MiniControllerFragment;
public class GoogleCastMiniPlayerManager extends SimpleViewManager<GoogleCastMiniPlayer> {
public static final String REACT_CLASS = "RNGoogleCastMiniPlayer";
ReactApplicationContext mCallerContext;
public GoogleCastMiniPlayerManager(ReactApplicationContext reactContext) {
mCallerContext = reactContext;
}
@Override
public String getName() {
return REACT_CLASS;
}
@Override
public GoogleCastMiniPlayer createViewInstance(ThemedReactContext context) {
return new GoogleCastMiniPlayer(context);
}
}
package com.yourapp.cast;
import androidx.annotation.NonNull;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GoogleCastPackage implements ReactPackage {
@NonNull
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
List<ViewManager> modules = new ArrayList<>();
modules.add(new GoogleCastMiniPlayerManager(reactContext));
return modules;
}
@NonNull
@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
return modules;
}
}
@peterwiebe
Copy link

Thank you for posting this, saved my butt on a project I am working on to have this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment