zene-capacitor-chromecast
v1.0.1
Published
This is a plugin for Capacitor that enables Chromecast functionality for iOS and Android.
Downloads
21
Readme
zene-capacitor-chromecast
This is a plugin for Capacitor that enables Chromecast functionality for iOS and Android. Forked from https://github.com/gameleap/capacitor-chromecast
Features
- Initialize Chromecast SDK
- Discover and connect to Chromecast devices
- Load and control media playback
- Volume control
- Session management
- Route scanning
- Custom message support
- Event listeners for session and media updates
Install
npm install zene-capacitor-chromecast
npx cap syncConfiguration
Android
Add the following to your android/app/src/main/AndroidManifest.xml:
<application>
<!-- Add Chromecast receiver app ID -->
<meta-data
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="com.gameleap.plugins.chromecast.CastOptionsProvider" />
</application>iOS
Add the following to your ios/App/Info.plist:
<key>NSLocalNetworkUsageDescription</key>
<string>This app uses the local network to discover Cast-enabled devices on your WiFi network.</string>Usage
Basic Implementation
import { Chromecast } from 'zene-capacitor-chromecast';
// Initialize the plugin
await Chromecast.initialize('4F8B3483'); // Optional: Custom receiver app ID
// Request a casting session
await Chromecast.requestSession();
// Load media
await Chromecast.launchMedia('https://example.com/video.mp4');
// Listen to events
const sessionListener = await Chromecast.addListener('SESSION_LISTENER', (session) => {
console.log('Session started:', session);
});
const mediaListener = await Chromecast.addListener('MEDIA_UPDATE', (media) => {
console.log('Media update:', media);
});
// Don't forget to remove listeners when done
sessionListener.remove();
mediaListener.remove();Advanced Media Loading
await Chromecast.loadMedia({
contentId: 'https://example.com/video.mp4',
contentType: 'video/mp4',
streamType: 'BUFFERED', // or 'LIVE'
duration: 3600, // in seconds
autoPlay: true,
currentTime: 0,
metadata: {
title: 'My Video',
subtitle: 'Episode 1',
images: [{
url: 'https://example.com/poster.jpg',
width: 1920,
height: 1080
}]
},
customData: {
// Your custom data here
}
});Media Control
// Play/Pause
await Chromecast.mediaPlay();
await Chromecast.mediaPause();
// Stop media
await Chromecast.mediaStop();
// Seek to position (in seconds)
await Chromecast.mediaSeek(120, 'PLAYBACK_START'); // or 'PLAYBACK_PAUSE'
// Volume control
await Chromecast.setMediaVolume(0.5, false); // volume: 0-1, muted: booleanSession Management
// Stop casting session
await Chromecast.sessionStop();
// Leave session (but keep it running for other devices)
await Chromecast.sessionLeave();
// Set receiver volume
await Chromecast.setReceiverVolumeLevel(0.8);
await Chromecast.setReceiverMuted(false);Route Scanning
// Start scanning for available devices
await Chromecast.startRouteScan();
// Select a specific route/device
await Chromecast.selectRoute('device-id');
// Stop scanning (important for battery life)
await Chromecast.stopRouteScan();Custom Messages
// Add message listener for custom namespace
await Chromecast.addMessageListener('urn:x-cast:custom.namespace');
// Send custom message
await Chromecast.sendMessage('urn:x-cast:custom.namespace', JSON.stringify({
type: 'customCommand',
data: { foo: 'bar' }
}));API Reference
Methods
initialize(appId?: string): Promise<void>
Initialize the Chromecast SDK with an optional custom receiver app ID.
requestSession(): Promise<void>
Request a new casting session by showing the device picker.
launchMedia(mediaUrl: string): Promise<boolean>
Simple method to load and play media. Returns true if successfully cast, false if opened in browser.
loadMedia(options: LoadMediaOptions): Promise<void>
Load media with detailed options including metadata, custom data, and playback settings.
mediaPlay(): Promise<void>
Resume playback of the current media.
mediaPause(): Promise<void>
Pause the current media.
mediaStop(): Promise<void>
Stop the current media playback.
mediaSeek(seekTime: number, resumeState?: string): Promise<void>
Seek to a specific time position. resumeState can be 'PLAYBACK_START' or 'PLAYBACK_PAUSE'.
setMediaVolume(level: number, muted: boolean): Promise<void>
Control media volume (0-1) and mute state.
setReceiverVolumeLevel(level: number): Promise<void>
Set the receiver device volume (0-1).
setReceiverMuted(muted: boolean): Promise<void>
Mute or unmute the receiver device.
sessionStop(): Promise<void>
End the current casting session.
sessionLeave(): Promise<void>
Leave the session while keeping it active for other devices.
startRouteScan(): Promise<void>
Start scanning for available Cast devices.
stopRouteScan(): Promise<void>
Stop scanning for devices.
selectRoute(routeId: string): Promise<void>
Connect to a specific Cast device.
sendMessage(namespace: string, message: string): Promise<void>
Send a custom message to the receiver app.
addMessageListener(namespace: string): Promise<void>
Add a listener for custom messages from a specific namespace.
addListener(eventName: string, listenerFunc: Function): Promise<PluginListenerHandle>
Add an event listener for Chromecast events.
Events
The plugin emits the following events that you can listen to:
SESSION_LISTENER
Fired when a new session is established.
SESSION_UPDATE
Fired when session information is updated.
RECEIVER_LISTENER
Fired when receiver availability changes.
MEDIA_LOAD
Fired when media is successfully loaded.
MEDIA_UPDATE
Fired when media status is updated (play, pause, seek, etc.).
RECEIVER_MESSAGE
Fired when custom messages are received from the receiver app.
Types
interface LoadMediaOptions {
contentId: string;
contentType?: string;
streamType?: 'BUFFERED' | 'LIVE';
duration?: number;
autoPlay?: boolean;
currentTime?: number;
metadata?: {
title?: string;
subtitle?: string;
images?: Array<{
url: string;
width?: number;
height?: number;
}>;
};
customData?: any;
textTrackStyle?: any;
}Platform Support
| Platform | Status | |----------|--------| | Android | ✅ Full support | | iOS | ⚠️ Limited support | | Web | ✅ Full support |
