- Step 1 - Add the dependency on Vizbee Sender SDK in your Android app’s module build.gradle
- Step 1 - Create an instance of OptionsProvider as shown in
CastOptionsProvider.kt
. - Step 2 - Register app’s
OptionsProvider
implementation class as shown inAndroidManifest.xml
- Step 3 - Start Google Cast Framework by invoking CastContext as shown in
VizbeeWrapper.kt
Last active
December 18, 2023 09:32
-
-
Save vizbee/9127ae2fbf402ae5808b30243e5b3fae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
// Vizbee Sender SDK | |
implementation 'tv.vizbee:android-sender-sdk:6.4.5' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.reactnativeappexample; | |
import android.content.Context; | |
import com.google.android.gms.cast.LaunchOptions; | |
import com.google.android.gms.cast.framework.CastOptions; | |
import com.google.android.gms.cast.framework.OptionsProvider; | |
import com.google.android.gms.cast.framework.SessionProvider; | |
import com.google.android.gms.cast.framework.media.CastMediaOptions; | |
import com.google.android.gms.cast.framework.media.MediaIntentReceiver; | |
import com.google.android.gms.cast.framework.media.NotificationOptions; | |
import java.util.ArrayList; | |
import java.util.List; | |
import tv.vizbee.api.RemoteActivity; | |
public class CastOptionsProvider implements OptionsProvider { | |
@Override | |
public CastOptions getCastOptions(Context context) { | |
// Showing 4 buttons: "rewind", "play/pause", "forward" and "stop casting". | |
List<String> buttonActions = new ArrayList<>(); | |
buttonActions.add(MediaIntentReceiver.ACTION_REWIND); | |
buttonActions.add(MediaIntentReceiver.ACTION_TOGGLE_PLAYBACK); | |
buttonActions.add(MediaIntentReceiver.ACTION_FORWARD); | |
buttonActions.add(MediaIntentReceiver.ACTION_STOP_CASTING); | |
// Showing "play/pause" and "stop casting" in the compat view of the notification. | |
int[] compatButtonActionsIndicies = new int[]{ 1, 3 }; | |
// Builds a notification with the above actions. Each tap on the "rewind" and | |
// "forward" buttons skips 30 seconds. | |
NotificationOptions notificationOptions = new NotificationOptions.Builder() | |
.setActions(buttonActions, compatButtonActionsIndicies) | |
.setSkipStepMs(30000l) | |
.setTargetActivityClassName(RemoteActivity.class.getName()) | |
.build(); | |
final CastMediaOptions mediaOptions = new CastMediaOptions.Builder() | |
.setNotificationOptions(notificationOptions) | |
.build(); | |
LaunchOptions launchOptions = new LaunchOptions.Builder() | |
.setAndroidReceiverCompatible(true) | |
.build(); | |
// TODO | |
// IMPORTANT: | |
// Replace the string "<CHROMECAST_APP_ID>" with the | |
// app ID issues by Chromecast for your app's Chromecast receiver | |
return new CastOptions.Builder() | |
.setReceiverApplicationId("<CHROMECAST_APP_ID>") | |
.setCastMediaOptions(mediaOptions) | |
.setLaunchOptions(launchOptions) | |
.build(); | |
} | |
@Override | |
public List<SessionProvider> getAdditionalSessionProviders(Context appContext) { | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<application | |
... | |
<!-- Register app’s OptionsProvider implementation class --> | |
<meta-data | |
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME" | |
android:value="com.reactnativeappexample.CastOptionsProvider" | |
/> | |
... | |
<activity | |
android:name="tv.vizbee.api.RemoteActivity" | |
..."> | |
.... | |
<!-- Set RemoteActivity’s parent activity to enable navigating back to the application’s Activity | |
when RemoteActivity is dismissed after tapping on L/N controls --> | |
<meta-data | |
android:name="android.support.PARENT_ACTIVITY" | |
android:value="com.app.ui.activities.MainActivity"/> | |
</activity> | |
... | |
</application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.reactnativeappexample; | |
import android.app.Application; | |
import android.content.Context; | |
import com.facebook.react.ReactApplication; | |
import com.google.android.gms.cast.framework.CastContext; | |
import tv.vizbee.rnsender.VizbeeBootstrap; | |
public class MainApplication extends Application implements ReactApplication { | |
... | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
... | |
/* | |
* Initialize CastContext to display cast notification when casting to Chromecast devices. | |
* It is important to add a try catch in order to avoid crash due to incorrect Google Play Services on the device | |
*/ | |
try { | |
CastContext.getSharedInstance(this); | |
} catch (Exception e){ | |
Log.e("MainApplication", | |
"Exception while initializing CastContext:\n" + | |
"Cast notifications will not work. Check the availability of the right Google Play Services on this device"); | |
} | |
// Vizbee initialization | |
VizbeeBootstrap.getInstance().initialize(this,"vzbxxxxxxxxx"); | |
... | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment