20. <script type="text/javascript" src=“//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js"/>
window.onload = function() {
loadSounds();
cast.receiver.logger.setLevelValue(cast.receiver.LoggerLevel.DEBUG);
window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
console.log('starting the receiver application');
castReceiverManager.onReady = function(event) {
console.log('receiver application is ready: ' + JSON.stringify(event.data));
window.castReceiverManager.setApplicationState('Application status is ready...');
};
castReceiverManager.onSenderConnected = function(event) {
console.log('sender application connected: ' + event.data);
console.log(window.castReceiverManager.getSender(event.data).userAgent);
};
castReceiverManager.onSenderDisconnected = function(event) {
console.log('sender application disconnected: ' + event.data);
//if we dont have any connected devices then just close this app, this might not be necessary for your use case though
if (window.castReceiverManager.getSenders().length == 0) {
window.close();
}
};//register the namespace that we can handle messaging from. NOTE, THIS SHOULD be the same as the one the SENDER app uses to send
messages
window.messageBus = window.castReceiverManager.getCastMessageBus('urn:x-cast:mbanje.kurt.soundboard');
//handler for the incoming CastMessageBus message events from sender applications
window.messageBus.onMessage = function(event) {
console.log('Message [' + event.senderId + ']: ' + event.data);
var json = JSON.parse(event.data);
processMessage(json);
}
// initialize the CastReceiverManager with an application status message
window.castReceiverManager.start({statusText: 'Application is starting'});
console.log('Receiver Manager started');
27. public class CastOptionsProvider implements OptionsProvider {
//This is the custom namespace we will use to communicate with the cast receiver application
public static final String NAMESPACE = "urn:x-cast:mbanje.kurt.soundboard";
@Override
public CastOptions getCastOptions(Context context) {
CastOptions castOptions = new CastOptions.Builder()
.setReceiverApplicationId(context.getString(R.string.app_id))
.build();
return castOptions;
}
@Override
public List<SessionProvider> getAdditionalSessionProviders(Context context) {
return null;
}
}
OptionsProvider
<!—AndroidManifest.xml—>
<meta-data
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="mbanje.kurt.soundboard.CastOptionsProvider" />
30. final JSONObject obj = new JSONObject();
obj.put("action",isChecked?ACTION_PLAY:ACTION_STOP);
obj.put("sound",sound);
final String json = obj.toString(1);
//we only send the message if we have an active session
if(buttonView.isEnabled() && sessionManager.getCurrentCastSession() != null) {
sessionManager.getCurrentCastSession().sendMessage(CastOptionsProvider.NAMESPACE, json).setResultCallback(new
ResultCallback<Status>() {
@Override
public void onResult(@NonNull final Status status) {
if (status.isSuccess()) {
Log.d(TAG, "message sent successfully: " + json);
} else {
Log.e(TAG, "failed to send message: " + json);
}
}
});
}
Activity Send Messages