搜索插件、代码仓与 APP 分类

插件详情v2.1.0

cast

用于发现和连接 Chromecast 设备的 Dart 包

近 30 天下载
1.7万
喜欢数
94
Pub 分数
80

安装插件

flutter pub add cast

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

该 cast 包是一个 Dart 库,旨在使 Flutter 应用程序能够发现并连接 Chromecast 设备。它允许开发者将音频、视频和其他内容从其应用流式传输到兼容的 Chromecast 设备上。该包支持无缝设备检测和媒体控制,非常适合构建与智能电视和流媒体硬件集成的媒体中心应用。该库以简洁性为设计目标,提供了一个简单的 API 来实现投射功能,而无需深入了解底层协议。该库由 eivo.co 维护,并在 GitHub 上开放,供社区贡献。

适合场景

  • 将流媒体传输到 Chromecast 设备
  • 构建电视或家庭娱乐应用
  • 启用远程播放控制
  • 与智能家居生态系统集成
  • 创建跨设备的媒体体验

主要特点

  • 自动发现 Chromecast 设备
  • 支持音频和视频投射
  • 简单、直观的 API
  • 跨平台兼容性(带有平台特定实现)
  • 开源且由社区维护

适合对象

  • 开发媒体应用的开发者
  • 面向智能电视或流媒体设备的应用
  • 需要设备间投射的项目
  • 从事物联网或智能家居集成工作的团队
  • 旨在实现多屏幕体验的 Flutter 应用

使用建议

  • 需要 Android 和 iOS 平台设置才能实现完整功能
  • 仅限兼容 Chromecast 的设备
  • 可能需要为复杂的媒体流进行额外配置
  • 依赖外部网络连接
  • 不适用于非媒体投射的使用场景

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Pub Likes Pub Popularity Pub Points

cast

Dart package to discover and connect with Chromecast devices

Installation

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

dependencies:
  cast: ^2.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

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目