|
| 1 | +// In your OptionsProvider class |
| 2 | +class CastOptionsProvider : OptionsProvider { |
| 3 | + override fun getCastOptions(context: Context): CastOptions { |
| 4 | + return CastOptions.Builder() |
| 5 | + .setReceiverApplicationId(context.getString(R.string.app_id)) // Your Receiver ID |
| 6 | + .build() |
| 7 | + } |
| 8 | + |
| 9 | + override fun getAdditionalSessionProviders(context: Context): List<SessionProvider>? { |
| 10 | + return null |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +// AI Assistant to resolve natural language into Castable Media |
| 17 | +suspend fun resolveMediaRequest(userPrompt: String): MediaInfo { |
| 18 | + val genModel = Firebase.vertexAI.generativeModel("gemini-1.5-pro") |
| 19 | + |
| 20 | + val systemInstruction = "Extract the movie title and stream URL from the user's request. Return JSON." |
| 21 | + val response = genModel.generateContent(userPrompt) |
| 22 | + |
| 23 | + // Logic to parse the AI response and fetch the actual stream URL |
| 24 | + val videoUrl = parseUrlFromAI(response.text) |
| 25 | + |
| 26 | + return MediaInfo.Builder(videoUrl) |
| 27 | + .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED) |
| 28 | + .setContentType("video/mp4") |
| 29 | + .setMetadata(MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE).apply { |
| 30 | + putString(MediaMetadata.KEY_TITLE, "AI Recommended Content") |
| 31 | + }) |
| 32 | + .build() |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +fun playAiContent(mediaInfo: MediaInfo) { |
| 38 | + val castSession = CastContext.getSharedInstance(context).sessionManager.currentCastSession |
| 39 | + |
| 40 | + if (castSession != null && castSession.isConnected) { |
| 41 | + val remoteMediaClient = castSession.remoteMediaClient |
| 42 | + val loadRequestData = MediaLoadRequestData.Builder() |
| 43 | + .setMediaInfo(mediaInfo) |
| 44 | + .setAutoplay(true) |
| 45 | + .build() |
| 46 | + |
| 47 | + remoteMediaClient?.load(loadRequestData) |
| 48 | + } else { |
| 49 | + // Prompt user to connect to a device first |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +// Example of the 2026 Suggester API |
| 56 | +val suggestionRequest = CastDeviceSuggestionRequest.Builder() |
| 57 | + .setContextualSignal(CastSignal.MEDIA_BROWSED) |
| 58 | + .build() |
| 59 | + |
| 60 | +castContext.getDeviceStatus(suggestionRequest).addOnSuccessListener { suggestion -> |
| 61 | + if (suggestion.hasNearbyDevice()) { |
| 62 | + showOneTapTransferHUD(suggestion.deviceName) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +// Android Sender using Vertex AI for Gemini |
| 69 | +class IntelligentCastManager(private val context: Context) { |
| 70 | + |
| 71 | + private val model = Firebase.vertexAI.generativeModel("gemini-2.5-flash") |
| 72 | + |
| 73 | + suspend fun processAndCast(userPrompt: String) { |
| 74 | + // AI analyzes the request and returns structured media data |
| 75 | + val aiResponse = model.generateContent(""" |
| 76 | + User wants: $userPrompt. |
| 77 | + Return JSON: { "title": "string", "url": "url", "description": "string", "3d_hud_style": "string" } |
| 78 | + """.trimIndent()) |
| 79 | +
|
| 80 | + val mediaData = parseJson(aiResponse.text) |
| 81 | +
|
| 82 | + // Create Media Metadata with custom 'HUD' instructions |
| 83 | + val metadata = MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE).apply { |
| 84 | + putString(MediaMetadata.KEY_TITLE, mediaData.title) |
| 85 | + // Custom data sent to the receiver to render the HUD |
| 86 | + val customData = JSONObject().apply { |
| 87 | + put("hud_mode", mediaData.hudStyle) // e.g., "minimalist_3d" |
| 88 | + put("ai_summary", mediaData.description) |
| 89 | + } |
| 90 | + loadMedia(mediaData.url, metadata, customData) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private fun loadMedia(url: String, metadata: MediaMetadata, customData: JSONObject) { |
| 95 | + val castSession = CastContext.getSharedInstance(context).sessionManager.currentCastSession |
| 96 | + val remoteMediaClient = castSession?.remoteMediaClient |
| 97 | + |
| 98 | + val loadRequestData = MediaLoadRequestData.Builder() |
| 99 | + .setMediaInfo(MediaInfo.Builder(url) |
| 100 | + .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED) |
| 101 | + .setMetadata(metadata) |
| 102 | + .setCustomData(customData) |
| 103 | + .build()) |
| 104 | + .setAutoplay(true) |
| 105 | + .build() |
| 106 | + |
| 107 | + remoteMediaClient?.load(loadRequestData) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +<!DOCTYPE html> |
| 114 | +<html> |
| 115 | +<head> |
| 116 | + <script src="//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js"></script> |
| 117 | + <style> |
| 118 | + #hud-overlay { |
| 119 | + position: absolute; |
| 120 | + top: 10%; left: 5%; |
| 121 | + color: cyan; |
| 122 | + font-family: 'Orbitron', sans-serif; |
| 123 | + text-shadow: 0 0 10px rgba(0, 255, 255, 0.8); |
| 124 | + transform: perspective(500px) rotateY(10deg); /* 3D Perspective Effect */ |
| 125 | + opacity: 0; |
| 126 | + transition: opacity 1s; |
| 127 | + } |
| 128 | + </style> |
| 129 | +</head> |
| 130 | +<body> |
| 131 | + <cast-media-player></cast-media-player> |
| 132 | + <div id="hud-overlay"> |
| 133 | + <h2 id="ai-title">AI ANALYSIS ACTIVE</h2> |
| 134 | + <p id="ai-description"></p> |
| 135 | + </div> |
| 136 | + |
| 137 | + <script> |
| 138 | + const context = cast.framework.CastReceiverContext.getInstance(); |
| 139 | + const playerManager = context.getPlayerManager(); |
| 140 | + |
| 141 | + // Listen for the custom data from our AI Sender |
| 142 | + playerManager.setMessageInterceptor( |
| 143 | + cast.framework.messages.MessageType.LOAD, |
| 144 | + loadRequestData => { |
| 145 | + const customData = loadRequestData.media.customData; |
| 146 | + if (customData && customData.hud_mode) { |
| 147 | + show3DHUD(customData.ai_summary); |
| 148 | + } |
| 149 | + return loadRequestData; |
| 150 | + } |
| 151 | + ); |
| 152 | + |
| 153 | + function show3DHUD(summary) { |
| 154 | + const hud = document.getElementById('hud-overlay'); |
| 155 | + document.getElementById('ai-description').innerText = summary; |
| 156 | + hud.style.opacity = '1'; |
| 157 | + // Auto-hide HUD after 10 seconds |
| 158 | + setTimeout(() => { hud.style.opacity = '0'; }, 10000); |
| 159 | + } |
| 160 | + |
| 161 | + context.start(); |
| 162 | + </script> |
| 163 | +</body> |
| 164 | +</html> |
0 commit comments