Flutter Cast Framework is a POC of a flutter plugin that lets you use Chromecast API in a flutter app.
Currently only the following APIs are integrated (both Android and iOS):
- Cast State
- Session state
- Send custom message
- Listen to received custom messages
- Load RemoteMediaRequestData
- Play, Pause, Stop media
- Expanded controls
- Mini Controller
- Cast Button
- Chromecast connection
Run the following to add flutter_cast_framework to the dependencies:
flutter pub add flutter_cast_frameworkAdd the following class to your Android project:
importandroid.content.Contextimportcom.google.android.gms.cast.framework.CastOptionsimportcom.google.android.gms.cast.framework.OptionsProviderimportcom.google.android.gms.cast.framework.SessionProviderclassCastOptionsProvider : OptionsProvider {
overridefungetCastOptions(context:Context): CastOptions {
returnCastOptions.Builder()
.setReceiverApplicationId("4F8B3483") // Your receiver Application ID
.build()
}
overridefungetAdditionalSessionProviders(context:Context): List<SessionProvider>? {
returnnull
}
}Add the following entry in the AndroidManifest.xml file under the <application> tag to reference the CastOptionsProvider class:
<application>
<meta-dataandroid:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"android:value="com.gianlucaparadise.flutter_cast_framework_example.CastOptionsProvider" />
</application>Make sure that your application and your activity are using an AppCompat theme (as stated here).
Make sure you minimum iOS version is 10.0. Select Runner from left pane > General tab > Deployment Info > Target: set 10.0 or higher
When Xcode is closed, open a terminal at the root folder of your project and run:
cd ios && pod installTo open your flutter project with Xcode, from root folder run open ios/Runner.xcworkspace
Add the following lines to your AppDelegate.swift:
import UIKit
import Flutter
+import GoogleCast
@UIApplicationMain
-@objc class AppDelegate: FlutterAppDelegate {+@objc class AppDelegate: FlutterAppDelegate, GCKLoggerDelegate {+ let kReceiverAppID = "4F8B3483" // Your receiver Application ID+ let kDebugLoggingEnabled = true+
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
+ let criteria = GCKDiscoveryCriteria(applicationID: kReceiverAppID)+ let options = GCKCastOptions(discoveryCriteria: criteria)+ GCKCastContext.setSharedInstanceWith(options)++ // Enable logger.+ GCKLogger.sharedInstance().delegate = self+
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
++ // MARK: - GCKLoggerDelegate++ func logMessage(_ message: String,+ at level: GCKLoggerLevel,+ fromFunction function: String,+ location: String) {+ if (kDebugLoggingEnabled) {+ print(function + " - " + message)+ }+ }
}I used this project to test the capabilities of the following technologies:
- Chromecast API (Sender - Android SDK)
- Flutter
- Flutter custom platform-specific code
Next features to be developed:
- CC in Expanded Controls (iOS)
- Expanded Controls cosmetics (ad in progress bar, full screen, progress bar handle)
- Title in MiniController and ExpandedControls (blocked because of a pigeon issue, but solved with a workaround)
- Handle queue
- Handle progress seek
- Understand if it is better to refactor using streams instead of listeners
- Add tests
- Various glitches and cosmetic fixes