Flutter微信视频通话插件wechat_video_call的使用

Flutter微信视频通话插件wechat_video_call的使用

简介

wechat_video_call 是一个用于 Flutter 的插件,它利用 Android 辅助功能服务实现一键拨打微信视频/语音通话的功能。

注意:

  • 需要在系统设置中启用应用的辅助功能服务。
  • 必须登录微信,并且视频通话的联系人已经在好友列表中。

平台支持

Android iOS MacOS Web Linux Windows

要求

  • Flutter >= 3.24.0
  • Dart >= 3.5.0
  • Android minSdk 24
  • Java 17
  • Android Gradle Plugin >= 8.1.0
  • Gradle wrapper >= 8.3

使用插件

请求辅助权限

bool ret = await WeChatVideoCall.requestAccessibilityPermission();

检查辅助权限

bool ret = await WeChatVideoCall.isAccessibilityPermissionEnabled();

拨打微信视频通话

bool ret = await WeChatVideoCall.videoCall(nickname);

警告

在应用被杀掉后,Android 辅助功能权限会自动禁用,需要重新启用!

示例代码

以下是一个完整的示例代码,展示了如何使用 wechat_video_call 插件:

import 'package:flutter/material.dart';
import 'package:wechat_video_call/wechat_video_call.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _controller = TextEditingController();
  final List<String> _nameList = [];
  bool _accessibilityPermissionEnabled = false;

  [@override](/user/override)
  void initState() {
    super.initState();
    WeChatVideoCall.isAccessibilityPermissionEnabled().then((res) {
      setState(() {
        _accessibilityPermissionEnabled = res;
      });
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('微信视频通话'),
        ),
        body: ListView(
          padding: const EdgeInsets.all(10),
          children: [
            ListTile(
              title: const Text('辅助权限状态'),
              trailing: Text(_accessibilityPermissionEnabled ? '已开启' : '已关闭'),
            ),
            FilledButton(
              onPressed: () async {
                bool ret = await WeChatVideoCall.requestAccessibilityPermission();
                debugPrint('requestAccessibilityPermission=$ret');
                setState(() {
                  _accessibilityPermissionEnabled = ret;
                });
              },
              child: const Text('请求辅助权限'),
            ),
            FilledButton(
              onPressed: () async {
                bool ret = await WeChatVideoCall.isAccessibilityPermissionEnabled();
                debugPrint('isAccessibilityPermissionEnabled=$ret');
                setState(() {
                  _accessibilityPermissionEnabled = ret;
                });
              },
              child: const Text('检查辅助权限'),
            ),
            TextField(
              controller: _controller,
              decoration: const InputDecoration(
                hintText: '微信昵称',
              ),
            ),
            FilledButton(
              onPressed: () async {
                String name = _controller.text;
                bool ret = await WeChatVideoCall.videoCall(name);
                debugPrint('videoCall=$ret');
                if (!_nameList.contains(name)) {
                  _nameList.add(name);
                  setState(() {});
                }
              },
              child: const Text('视频通话'),
            ),
            FilledButton(
              onPressed: () async {
                String name = _controller.text;
                bool ret = await WeChatVideoCall.voiceCall(name);
                debugPrint('voiceCall=$ret');
                if (!_nameList.contains(name)) {
                  _nameList.add(name);
                  setState(() {});
                }
              },
              child: const Text('语音通话'),
            ),
            Wrap(spacing: 10, children: [
              for (String name in _nameList)
                OutlinedButton(
                  onPressed: () async {
                    setState(() {
                      _controller.text = name;
                    });
                  },
                  onLongPress: () async {
                    _nameList.remove(name);
                    setState(() {});
                  },
                  child: Text(name),
                ),
            ]),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter微信视频通话插件wechat_video_call的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter微信视频通话插件wechat_video_call的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用wechat_video_call插件来实现微信视频通话功能的示例代码。请注意,wechat_video_call插件是一个假设的插件名称,因为实际上可能并不存在一个直接命名为wechat_video_call的官方或广泛使用的Flutter插件用于微信视频通话。不过,我会基于一般的视频通话插件使用逻辑来编写一个示例代码,以便你理解如何在Flutter项目中集成和使用视频通话功能。

首先,假设有一个名为video_call_plugin的Flutter插件,它提供了视频通话的功能。以下是如何在Flutter项目中集成和使用这个插件的步骤:

  1. 添加依赖: 在你的pubspec.yaml文件中添加video_call_plugin依赖(注意:这是一个假设的插件名,你需要替换为实际存在的视频通话插件)。

    dependencies:
      flutter:
        sdk: flutter
      video_call_plugin: ^x.y.z  # 替换为实际插件的版本号
    
  2. 导入插件: 在你的Dart文件中导入该插件。

    import 'package:video_call_plugin/video_call_plugin.dart';
    
  3. 初始化插件: 在应用的入口文件(通常是main.dart)中初始化插件。

    void main() {
      runApp(MyApp());
      // 假设插件需要初始化
      VideoCallPlugin.initialize();
    }
    
  4. 实现视频通话功能: 创建一个页面或组件来管理视频通话的开始、结束和界面展示。

    import 'package:flutter/material.dart';
    import 'package:video_call_plugin/video_call_plugin.dart';
    
    class VideoCallScreen extends StatefulWidget {
      @override
      _VideoCallScreenState createState() => _VideoCallScreenState();
    }
    
    class _VideoCallScreenState extends State<VideoCallScreen> {
      VideoCallController _callController;
    
      @override
      void initState() {
        super.initState();
        // 初始化VideoCallController
        _callController = VideoCallController();
    
        // 监听视频通话状态变化
        _callController.callStateChanged.listen((state) {
          print('Call state changed: $state');
          if (state == CallState.ended) {
            // 视频通话结束后可以执行一些操作,比如返回主页面
            Navigator.pop(context);
          }
        });
      }
    
      @override
      void dispose() {
        // 释放资源
        _callController.dispose();
        super.dispose();
      }
    
      void startCall() async {
        try {
          // 假设有一个对方的用户ID或房间ID
          String calleeId = 'user123';
          await _callController.startCall(calleeId);
        } catch (e) {
          print('Failed to start call: $e');
        }
      }
    
      void endCall() async {
        await _callController.endCall();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Video Call'),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                ElevatedButton(
                  onPressed: startCall,
                  child: Text('Start Call'),
                ),
                SizedBox(height: 20),
                ElevatedButton(
                  onPressed: endCall,
                  child: Text('End Call'),
                ),
                // 假设有一个VideoView用于显示视频画面
                // 注意:这里的VideoView是一个假设的组件,你需要根据插件提供的组件来使用
                // VideoView(controller: _callController.videoViewController),
              ],
            ),
          ),
        );
      }
    }
    

请注意,上述代码中的VideoCallControllerCallState以及VideoView等都是假设的类名和组件名,你需要根据实际的视频通话插件提供的API和组件来进行替换和调整。

此外,由于微信视频通话涉及到微信的SDK和API,通常这些功能是由微信官方提供的SDK来实现的,而不是通过第三方Flutter插件。因此,如果你需要在Flutter应用中实现微信视频通话功能,你可能需要查阅微信官方文档,了解如何在原生Android和iOS平台上集成微信视频通话SDK,并通过Flutter的platform_channel与原生代码进行通信。这将是一个更复杂的过程,需要深入了解Flutter与原生平台交互的机制。

回到顶部