@capnative/capacitor-chromecast
v8.2.1
Published
Open the native Google Cast device picker from Capacitor
Maintainers
Readme
Capacitor Chromecast
A Capacitor plugin that opens Google Cast / Chromecast native picker from JavaScript or TypeScript.
Compatibility
| Plugin version | Capacitor compatibility | Maintained | | -------------- | ----------------------- | ---------- | | v8.*.* | v8.*.* | ✅ |
Supported platforms
| Platform | Support | Minimum version | | -------- | ------- | -------------------------- | | iOS | ✅ | iOS 15 | | Android | ✅ | API level 24 (Android 7.0) | | Web | ❌ | — |
Chromecast functionality is provided by the native iOS and Android implementations. Calls on the web platform reject with an UNSUPPORTED_PLATFORM error.
Install
npm install @capnative/capacitor-chromecast
npx cap syncInitialization
You must provide your Google Cast receiver application ID either in JavaScript or in Capacitor config.
JavaScript initialization
import { Chromecast } from '@capnative/capacitor-chromecast';
await Chromecast.initialize({
receiverApplicationId: 'YOUR_RECEIVER_APPLICATION_ID',
});Capacitor config initialization
You can either modify your capacitor.config.ts file
import { CapacitorConfig } from '@capacitor/cli';
export default CapacitorConfig({
plugins: {
Chromecast: {
receiverApplicationId: 'YOUR_RECEIVER_APPLICATION_ID',
},
},
});or your capacitor.config.json file
{
"plugins": {
"Chromecast": {
"receiverApplicationId": "YOUR_RECEIVER_APPLICATION_ID"
}
}
}The receiver application ID must be an 8-character hexadecimal Google Cast application ID.
Basic usage
import { Chromecast } from '@capnative/capacitor-chromecast';
await Chromecast.initialize({
receiverApplicationId: 'YOUR_RECEIVER_APPLICATION_ID',
});
await Chromecast.addListener('sessionStateChanged', async (event) => {
if (event.state === 'connected') {
await Chromecast.loadMedia({
url: 'https://example.com/video.mp4',
});
}
});
await Chromecast.show();iOS setup
Google Cast discovery on iOS requires local-network and Bonjour declarations in the consuming app's Info.plist.
Add:
<key>NSLocalNetworkUsageDescription</key>
<string>This app uses Google Cast to discover and connect to nearby devices.</string>
<key>NSBonjourServices</key>
<array>
<string>_googlecast._tcp</string>
<string>_YOUR_RECEIVER_APPLICATION_ID._googlecast._tcp</string>
</array>If you use the full Bluetooth-enabled Google Cast SDK in your app setup, Google may also require Bluetooth-related usage descriptions. Review the latest Google Cast iOS setup guidance for your chosen SDK distribution.
API
initialize(...)show()loadMedia(...)isConnected()addListener('sessionStateChanged', ...)removeAllListeners()- Interfaces
- Type Aliases
initialize(...)
initialize(options: InitializeOptions) => Promise<void>Initializes the native Google Cast SDK with your receiver application ID.
Call this once during application startup, or configure the same value in
capacitor.config.* under plugins.Chromecast.receiverApplicationId.
| Param | Type |
| ------------- | --------------------------------------------------------------- |
| options | InitializeOptions |
show()
show() => Promise<void>Opens the official native Google Cast device picker.
loadMedia(...)
loadMedia(options: LoadMediaOptions) => Promise<void>Loads media from a URL on the currently connected Chromecast device.
| Param | Type |
| ------------- | ------------------------------------------------------------- |
| options | LoadMediaOptions |
isConnected()
isConnected() => Promise<boolean>Returns whether there is an active Chromecast device connection.
Returns: Promise<boolean>
addListener('sessionStateChanged', ...)
addListener(eventName: 'sessionStateChanged', listenerFunc: (event: SessionStateChangedEvent) => void) => Promise<PluginListenerHandle>| Param | Type |
| ------------------ | ------------------------------------------------------------------------------------------------- |
| eventName | 'sessionStateChanged' |
| listenerFunc | (event: SessionStateChangedEvent) => void |
Returns: Promise<PluginListenerHandle>
removeAllListeners()
removeAllListeners() => Promise<void>Interfaces
InitializeOptions
| Prop | Type | Description |
| --------------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| receiverApplicationId | string | Google Cast receiver application ID from the Google Cast Developer Console. This must be an 8-character hexadecimal application ID such as CC1AD845. |
LoadMediaOptions
| Prop | Type | Description |
| --------- | ------------------- | --------------------------------------------------------------------- |
| url | string | Absolute URL of the media to load on the connected Chromecast device. |
PluginListenerHandle
| Prop | Type |
| ------------ | ----------------------------------------- |
| remove | () => Promise<void> |
SessionStateChangedEvent
| Prop | Type |
| ----------- | ----------------------------------------------------- |
| state | SessionState |
Type Aliases
SessionState
'connecting' | 'connected' | 'disconnected'
Future media-casting capabilities
The repository is structured so future Cast functionality can be added without breaking the simple show() API. Likely follow-up APIs include:
getSession()disconnect()play()pause()stop()seek()setVolume()getMediaStatus()
