Skip to content

Instantly share code, notes, and snippets.

public final class CastOptionsProvider implements OptionsProvider {
public CastOptions getCastOptions(Context appContext) {
// Register your receiver on Cast Developer Console to get this ID: https://cast.google.com/publish
String receiverId = "";
return new CastOptions.Builder()
.setReceiverApplicationId(receiverId)
.build();
}
<meta-data android:name= "com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="yourpackagename.CastOptionsProvider" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.app.MediaRouteButton
android:id="@+id/media_route_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
private int googlePlayServicesAvailabilityRequestCode = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaRouteButton mediaRouteButton = findViewById(R.id.media_route_button);
CastButtonFactory.setUpMediaRouteButton(this, mediaRouteButton);
private class SimpleChromecastConnectionListener implements ChromecastConnectionListener {
@Override
public void onChromecastConnecting() {
Log.d(getClass().getSimpleName(), "onChromecastConnecting");
}
@Override
public void onChromecastConnected(ChromecastYouTubePlayerContext chromecastYouTubePlayerContext) {
Log.d(getClass().getSimpleName(), "onChromecastConnected");
implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:chromecast-sender:last-version'
// this is not needed to use the library, but it provides the very useful MediaRouteButton.
implementation 'androidx.mediarouter:mediarouter:last-version'
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/play_pause_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play/Pause" />
View customUiView = youTubePlayerView.inflateCustomPlayerUI(R.layout.custom_player_ui);
class CustomUiController {
private boolean isPlaying = false;
CustomUiController(View customPlayerUI, YouTubePlayer youTubePlayer) {
playPauseButton = customPlayerUI.findViewById(R.id.play_pause_button);
playPauseButton.setOnClickListener( view -> {
if(isPlaying) youTubePlayer.pause();
else youTubePlayer.play();
@PierfrancescoSoffritti
PierfrancescoSoffritti / build.gradle
Created February 11, 2019 19:01
android-youtube-player, an open source alternative to the official YouTube Player API: https://medium.com/@soffritti.pierfrancesco/how-to-play-youtube-videos-in-your-android-app-c40427215230
dependencies {
implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:last_version'
}