Skip to content

Instantly share code, notes, and snippets.

View PierfrancescoSoffritti's full-sized avatar

Pierfrancesco Soffritti PierfrancescoSoffritti

View GitHub Profile
@PierfrancescoSoffritti
PierfrancescoSoffritti / eventBus.js
Last active February 15, 2024 14:16
A simple implementation of an event bus in Javascript. More details here: https://medium.com/@soffritti.pierfrancesco/create-a-simple-event-bus-in-javascript-8aa0370b3969
/**
* subscriptions data format:
* { eventType: { id: callback } }
*/
const subscriptions = { }
const getNextUniqueId = getIdGenerator()
function subscribe(eventType, callback) {
const id = getNextUniqueId()
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'
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");
<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();