GithubHelp home page GithubHelp logo

gibahjoe / flutter_cast Goto Github PK

View Code? Open in Web Editor NEW

This project forked from johnvuko/flutter_cast

0.0 0.0 0.0 278 KB

Dart package to discover and connect with Chromecast devices

License: MIT License

Ruby 6.75% Objective-C 0.08% Kotlin 0.27% Dart 88.57% Swift 4.32%

flutter_cast's Introduction

cast

Dart package to discover and connect with Chromecast devices

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  cast: ^1.1.0

iOS

Required iOS 13 minimum. Since iOS 14 a few more steps are required https://developers.google.com/cast/docs/ios_sender/ios_permissions_changes

In ios/Runner/Info.plist add:

<key>NSBonjourServices</key>
<array>
  <string>_googlecast._tcp</string>
  <string>_YOUR_APP_ID._googlecast._tcp</string>
</array>

<key>NSLocalNetworkUsageDescription</key>
<string>${PRODUCT_NAME} uses the local network to discover Cast-enabled devices on your WiFi
network.</string>

Usage

List devices:

Widget build(BuildContext context) {
  return FutureBuilder<List<CastDevice>>(
    future: CastDiscoveryService().search(),
    builder: (context, snapshot) {
      if (snapshot.hasError) {
        return Center(
          child: Text(
            'Error: ${snapshot.error.toString()}',
          ),
        );
      } else if (!snapshot.hasData) {
        return Center(
          child: CircularProgressIndicator(),
        );
      }

      if (snapshot.data!.isEmpty) {
        return Column(
          children: [
            Center(
              child: Text(
                'No Chromecast founded',
              ),
            ),
          ],
        );
      }

      return Column(
        children: snapshot.data!.map((device) {
          return ListTile(
            title: Text(device.name),
            onTap: () {
              _connectToYourApp(context, device);
            },
          );
        }).toList(),
      );
    },
  );
}

Connect to device:

Future<void> _connect(BuildContext context, CastDevice object) async {
  final session = await CastSessionManager().startSession(object);

  session.stateStream.listen((state) {
    if (state == CastSessionState.connected) {
      _sendMessage(session);
    }
  });

  session.messageStream.listen((message) {
    print('receive message: $message');
  });

  session.sendMessage(CastSession.kNamespaceReceiver, {
    'type': 'LAUNCH',
    'appId': 'YouTube', // set the appId of your app here
  });
}

CastSessionManager is used to keep track of all sessions.

Send message:

void _sendMessage(CastSession session) {
  session.sendMessage('urn:x-cast:namespace-of-the-app', {
    'type': 'sample',
  });
}

Except for the launch message, you should wait until the session have a connected state before sending message.

Note

Some informations about the protocol used https://docs.rs/crate/gcast/0.1.5/source/PROTOCOL.md

$ pub global activate protoc_plugin
$ export PATH="$PATH":"$HOME/.pub-cache/bin"
$ protoc -I=./lib/cast_channel --dart_out=./lib/cast_channel ./lib/cast_channel/cast_channel.proto --plugin "pub run protoc_plugin"

Author

flutter_cast's People

Contributors

johnvuko avatar roman800 avatar pierophp avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.