Google Cast SDK for flutter
Find a file
gianlucaparadise cbab0e6601 Bump compileSdkVersion to 31
Fixes #4
2023-01-03 22:41:50 +01:00
.github Fix docs folder 2023-01-03 11:12:35 +01:00
.vscode Refactor classes and improve docs 2021-12-03 08:51:57 +01:00
android Bump compileSdkVersion to 31 2023-01-03 22:41:50 +01:00
assets Basic CastButton implementation; 2019-11-09 10:00:49 +01:00
docs/api Fix docs folder 2023-01-03 11:12:35 +01:00
example Bump compileSdkVersion to 31 2023-01-03 22:41:50 +01:00
ios MediaMetadata: strings workaround ios 2022-08-02 08:26:59 +02:00
lib MediaMetadata: strings workaround flutter 2022-07-31 19:47:33 +02:00
pigeon MediaMetadata: strings workaround pigeon 2022-07-31 19:47:33 +02:00
receiver RemoteMediaClient created demo receiver 2021-11-17 08:28:18 +01:00
test Initial CastContext setup; 2019-11-08 06:52:35 +01:00
.gitignore updated flutter 2019-11-07 07:35:17 +01:00
.metadata Initial commit 2019-11-07 07:33:27 +01:00
CHANGELOG.md Flutter pub: Change version 2022-07-31 18:41:09 +02:00
flutter_cast_framework.iml iOS: Initial CAF setup; 2019-11-19 06:10:10 +01:00
LICENSE Add license 2022-07-31 18:36:25 +02:00
Makefile Fix docs folder 2023-01-03 11:12:35 +01:00
pubspec.lock Upgrade min dart version and deps minor versions 2023-01-02 18:24:00 +01:00
pubspec.yaml Upgrade min dart version and deps minor versions 2023-01-02 18:24:00 +01:00
README.md Fix README and re-generate docs 2023-01-02 16:43:09 +01:00

Flutter Cast Framework

Overview

Pub Version (including pre-releases)

Flutter Cast Framework is a POC of a flutter plugin that lets you use Chromecast API in a flutter app.

Exposed APIs

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

Setup

Add Dependency

Run the following to add flutter_cast_framework to the dependencies:

flutter pub add flutter_cast_framework

Android Setup

1. Create CastOptionsProvider

Add the following class to your Android project:

import android.content.Context
import com.google.android.gms.cast.framework.CastOptions
import com.google.android.gms.cast.framework.OptionsProvider
import com.google.android.gms.cast.framework.SessionProvider

class CastOptionsProvider : OptionsProvider {
    override fun getCastOptions(context: Context): CastOptions {
        return CastOptions.Builder()
                .setReceiverApplicationId("4F8B3483") // Your receiver Application ID
                .build()
    }

    override fun getAdditionalSessionProviders(context: Context): List<SessionProvider>? {
        return null
    }
}

2. Load CastOptionsProvider

Add the following entry in the AndroidManifest.xml file under the <application> tag to reference the CastOptionsProvider class:

<application>
    <meta-data
        android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
        android:value="com.gianlucaparadise.flutter_cast_framework_example.CastOptionsProvider" />
</application>

3. Theme

Make sure that your application and your activity are using an AppCompat theme (as stated here).

iOS Setup

1. Minimum iOS version

Make sure you minimum iOS version is 10.0. Select Runner from left pane > General tab > Deployment Info > Target: set 10.0 or higher

2. Install iOS dependencies

When Xcode is closed, open a terminal at the root folder of your project and run:

cd ios && pod install

3. Open project in Xcode

To open your flutter project with Xcode, from root folder run open ios/Runner.xcworkspace

4. Chromecast SDK setup

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)
+      }
+  }
 }

Tech notes

I used this project to test the capabilities of the following technologies:

  • Chromecast API (Sender - Android SDK)
  • Flutter
  • Flutter custom platform-specific code

Roadmap

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