flutter_livepush_plugin 6.7.0 copy "flutter_livepush_plugin: ^6.7.0" to clipboard
flutter_livepush_plugin: ^6.7.0 copied to clipboard

Aliyun Live Push SDK for Flutter(Android and iOS)

flutter_livepush_plugin #

The project supports Android and iOS live stream pushing base on AlivcLivePusher SDK.

Installation #

dependencies:
  flutter_livepush_plugin: ^{{latest version}}

Flutter插件更新flutter_livepush_plugin

说明 #

阿里云直播推流SDK(ApsaraVideo Pusher SDK,以下简称直播推流SDK)是基于阿里云强大内容分发网络和音视频实时通讯技术的直播客户端推流开发工具,提供简单易用的开放接口、网络自适应的流畅体验、多节点的低延迟优化、功能强大的实时美颜等音视频直播技术服务。

Flutter直播推流SDK在原生层基于 Android/iOS 直播推流SDK,以下是Flutter接口相关调用流程。详细接口调用可参考插件相关dart文件。

官网文档Flutter推流SDK

功能特性 #

  • 基础直播

    • 摄像头推流
  • 互动直播

    • 连麦互动
    • PK互动

文档指引Demo体验

Plugin结构说明 #

.
├── CHANGELOG.md														版本更新说明
├── README.md																项目文档
├── android																	安卓原生桥接层
├── example																	推流Flutter Demo层
├── ios																			iOS原生桥接层
├── lib																			推流Flutter Dart层
│   ├── base
│   │   ├── live_base.dart									推流基础接口
│   ├── beauty
│   │   └── live_beauty.dart								美颜调用接口
│   ├── def
│   │   └── live_def.dart										推流自定义参数&枚举
│   ├── player
│   │   ├── live_player.dart								直播播放类(当前仅支持互动直播)
│   │   ├── live_player_config.dart					直播播放配置类
│   │   └── live_player_def.dart						直播播放自定义参数&枚举
│   ├── pusher
│   │   ├── live_push_config.dart						直播推流配置类
│   │   ├── live_push_def.dart							直播推流自定义参数&枚举
│   │   ├── live_pusher.dart								直播推流接口,通过AlivcLivePusher类可以完成音视频的采集推流。
│   │   ├── live_pusher_preview.dart				直播推流预览View
│   │   └── live_transcoding_config.dart		互动直播,混流配置类
│   └── util
│       └── live_utils.dart									工具类
├── pubspec.yaml														依赖管理工具

Demo下载SDK下载与发布记录

文档指引Demo编译

快速集成 #

文档指引SDK集成

1.在项目中的 pubspec.yaml 中添加如下依赖: #

dependencies:
  flutter_livepush_plugin: ^{{latest version}}

2.添加相关系统权限声明 #

Android

打开 /android/app/src/main/AndroidManifest.xml 文件,声明需要申请的权限。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

iOS

需要在iOS工程中的 Info.plist 中加入对相机和麦克风的权限申请:

<key>NSCameraUsageDescription</key>
<string>授权摄像头权限才能正常视频通话</string>
<key>NSMicrophoneUsageDescription</key>
<string>授权麦克风权限才能正常语音通话</string>

3.配置License授权 #

推流SDK升级到4.4.2及以后版本,接入一体化License服务,您需要推流SDK License集成指南

功能使用 #

功能使用功能使用

1.摄像头推流 #

1.1.注册SDK

/// 1.创建[AlivcBase]实例
AlivcBase alivcBase = AlivcBase.init();

/// 2.注册SDK
/// 注意:在调用注册SDK方法前,需要按照[registerSDK]接口文档配置好licenseKey和licenseFile。
alivcBase.registerSDK();

/// 3.设置监听回调接口
alivcBase.setListener();
/// 4.SDK Licence 校验接口回调
alivcBase.setOnLicenceCheck((result, reason) {
    if (result == AlivcLiveLicenseCheckResultCode.success) {
      // 注册SDK成功
    }
});

/// 5.使用[AlivcBase]其它接口
/// 获取原生直播推流SDK版本号
String sdkVersion = await AlivcBase.getSdkVersion();
/// 启用控制台日志打印
AlivcBase.setConsoleEnable(true);
/// 设置log级别为Debug调试级别
AlivcBase.setLogLevel(AlivcLivePushLogLevel.debug);

/// 设置Log路径
String logPath = "xxxx"; // xxxx为手机存放的路径
int maxPartFileSizeInKB = 100 * 1024 * 1024; // 自定义设置
AlivcBase.setLogPath(logPath, maxPartFileSizeInKB);

1.2.配置推流参数

/// 1.创建[AlivcLivePusher]实例
AlivcLivePusher livePusher = AlivcLivePusher.init();
/// 2.创建Config,将将[AlivcLivePusherConfig]同[AlivcLivePusher]联系起来
livePusher.createConfig();
/// 3.创建[AlivcLivePusherConfig]实例
AlivcLivePusherConfig pusherConfig = AlivcLivePusherConfig.init();

/// 4.设置推流参数(根据使用场景,自定义设置)
/// 设置分辨率为540P
pusherConfig.setResolution(AlivcLivePushResolution.resolution_540P);
/// 设置视频采集帧率为20fps。建议用户使用20fps
pusherConfig.setFps(AlivcLivePushFPS.fps_20);
/// 打开码率自适应,默认为true
pusherConfig.setEnableAutoBitrate(true);
/// 设置关键帧间隔。关键帧间隔越大,延时越高。建议设置为1-2
pusherConfig.setVideoEncodeGop(AlivcLivePushVideoEncodeGOP.gop_2);
/// 设置重连时长为2s。单位为毫秒,设置不小于1秒,建议使用默认值即可。
pusherConfig.setConnectRetryInterval(2000);
/// 设置预览镜像为关闭
pusherConfig.setPreviewMirror(false);
/// 设置推流方向为竖屏
pusherConfig.setOrientation(AlivcLivePushOrientation.portrait);
/// 设置打开分辨率自适应
pusherConfig.setEnableAutoResolution(true);

/// 设置暂停图片
String pauseImagePath = "xxxx"; // xxxx为手机存放的图片路径
pusherConfig.setPauseImg(pauseImagePath);

/// 设置预览显示模式为保持视频比例
pusherConfig.setPreviewDisplayMode(AlivcPusherPreviewDisplayMode.preview_aspect_fit);

1.3.进行推流

/// 1.创建推流引擎实例
livePusher.initLivePusher();

/// 2.注册推流听回调
/// 设置推流状态监听回调
livePusher.setInfoDelegate();
/// 设置推流错误监听回调
livePusher.setErrorDelegate();
/// 设置推流网络监听回调
livePusher.setNetworkDelegate();

/// 3.创建推流预览视图
var x = 0.0; // 自定义数值
var y = 0.0; // 自定义数值
var width = MediaQuery.of(context).size.width; // 自定义数值
var height = MediaQuery.of(context).size.height; // 自定义数值
AlivcPusherPreview pusherPreviewView = AlivcPusherPreview(
      onCreated: _onPusherPreviewCreated,
      viewType: AlivcPusherPreviewType.push, 
      x: x,
      y: y,
      width: width,
      height: height);
  return Container(
        color: Colors.black,
        width: width,
        height: height,
        child: pusherPreviewView);

// 视图创建回调
_onPusherPreviewCreated(id) {
    /// 4.开始预览
    livePusher.startPreview();
}

/// 开始预览调用之后执行下面的步骤

/// 5.开始推流。预览成功后才可以开始推流
String pushURL = "推流测试地址(rtmp://......)"; 
livePusher.startPushWithURL(pushURL);

/// 6.设置其他推流控制
/// 暂停摄像头推流。可以调用[setPauseImg]后调用[pause]接口,从摄像头推流切换成静态图片推流,音频推流继续。
livePusher.pause();
/// 从静态图片推流切换成摄像头推流,音频推流继续
livePusher.resume();
/// 推流状态下可调用停止推流,完成后推流停止
livePusher.stopPush();
/// 在预览状态下才可以调用停止预览,正在推流状态下,调用停止预览无效。预览停止后,预览画面定格在最后一帧
livePusher.stopPreview();
/// 推流状态下或者接收到所有Error相关回调状态下可调用重新推流,且Error状态下只可以调用此接口(或者[reconnectPushAsync]重连)或者调用[destory]销毁推流。完成后重新开始推流,重启[AlivcLivePusher]内部的一切资源,包括预览、推流等等restart
livePusher.restartPush();
/// 推流状态下或者接收到[setNetworkDelegate]相关的Error回调状态下可调用此接口, 且Error状态下只可以调用此接口(或者[restartPush]重新推流)或者调用[destory]销毁推流。完成后推流重连,重新链接推流
livePusher.reconnectPushAsync();
/// 销毁推流后,推流停止,预览停止,预览画面移除。[AlivcLivePusher]相关的一切资源销毁
livePusher.destory();

1.4.设置背景音乐

/// 开始播放背景音乐
String musicPath = "xxxx"; // xxxx为手机存放的音乐资源路径
livePusher.startBGMWithMusicPathAsync(musicPath);
/// 停止播放背景音乐。若当前正在播放BGM,并且需要切换歌曲,只需要调用开始播放背景音乐接口即可,无需停止当前正在播放的背景音乐
livePusher.stopBGMAsync();
/// 暂停播放背景音乐,背景音乐开始播放后才可调用此接口
livePusher.pauseBGM();
/// 恢复播放背景音乐,背景音乐暂停状态下才可调用此接口
livePusher.resumeBGM();
/// 开启循环播放音乐
livePusher.setBGMLoop(true);
/// 设置降噪开关。打开降噪后,将对采集到的声音中非人声的部分进行过滤处理。可能存在对人声稍微抑制作用,建议让用户自由选择是否开启降噪功能,默认不使用
livePusher.setAudioDenoise(true);
/// 设置耳返开关。耳返功能主要应用于KTV场景。打开耳返后,插入耳机将在耳机中听到主播说话声音。关闭后,插入耳机无法听到人声。未插入耳机的情况下,耳返不起作用
livePusher.setBGMEarsBack(true);
/// 混音设置,设置背景音乐音量
livePusher.setBGMVolume(50); // 设置数值范围:[0 ~ 100],默认:50
/// 混音设置,设置人声采集音量
livePusher.setCaptureVolume(50); // 设置数值范围:[0 ~ 100] 默认:50
/// 设置静音。静音后音乐声音和人声输入都会静音。要单独设置音乐或人声静音可以通过混音音量设置接口来调整
livePusher.setMute(true);

1.5.设置推流截图

/// 调用截图
String dir = "xxxx"; // xxxx代表设置路径
if (Platform.isIOS) {
    /// dir设置要求:iOS系统下是指定存放相对的路径,会在系统沙盒路径下自动生成自定义的目录,设置为""时,则保存在系统沙盒路径下。
    /// dirTypeForIOS:可选设置。不设置默认是放在系统沙盒的[document]路径下。
    livePusher.snapshot(1, 0, dir, dirTypeForIOS: AlivcLiveSnapshotDirType.document);
} else {
    livePusher.snapshot(1, 0, dir);
}
/// 设置截图回调,需要在调用[snapshot]后调用
livePusher.setSnapshotDelegate();

1.6.摄像头相关操作

/// 切换前后摄像头
livePusher.switchCamera();
/// 开启/关闭闪光灯,在前置摄像头时开启闪关灯无效
livePusher.setFlash(false);

/// 焦距调整,即可实现采集画面的缩放功能。传入参数为正数,则放大焦距,传入参数为负数则缩小焦距
double max = await livePusher.getMaxZoom();
livePusher.setZoom(min(1.0, max));

/// 手动对焦
/// [autoFocus]参数表示是否需要自动对焦,该参数仅对调用接口的该次对焦操作生效。后续是否自动对焦沿用上述自动聚焦接口设置值。
double pointX = 50.0; // 自定义
double pointY = 50.0; // 自定义
bool autoFocus = true;
livePusher.focusCameraAtAdjustedPoint(pointX, pointY, autoFocus);

/// 设置不打开自动对焦
livePusher.setAutoFocus(false);
/// 设置不打开预览镜像
livePusher.setPreviewMirror(false);
/// 设置不打开推流镜像
livePusher.setPushMirror(false);

2.直播连麦互动 #

2.1.配置推流参数

/// 1.创建[AlivcLivePusher]实例等步骤可参考[#### 1.2.配置推流参数]

/// 2.设置互动模式
pusherConfig.setLivePushMode(AlivcLivePushMode.interactive);

2.2.进行推流

/// 1.创建推流引擎实例等步骤可参考[#### 1.3.进行推流]

/// 2.使用连麦互动模式下推流地址URL进行推流。可以通过控制台生成或通过自定义拼接主播和连麦观众的推拉流地址,及普通观众(非连麦观众)的CDN播放地址
String pushURL = "RTC推流地址(artc://......)"; 
livePusher.startPushWithURL(pushURL);

2.3.创建AlivcLivePlayer RTC播放

/// 1.创建[AlivcLivePlayer]实例
AlivcLivePlayer livePlayer = AlivcLivePlayer.init();
/// 2.绑定Config,将[AlivcLivePlayConfig]同[AlivcLivePlayer]联系起来
livePlayer.bindPlayConfig();
/// 3.创建[AlivcLivePlayConfig]实例
AlivcLivePlayConfig livePlayConfig = AlivcLivePlayConfig.init();
/// 4.初始化直播连麦播放引擎
livePlayer.initLivePlayer();

/// 5.创建直播拉流视图
var x = 0.0; // 自定义数值
var y = 0.0; // 自定义数值
var width = MediaQuery.of(context).size.width; // 自定义数值
var height = MediaQuery.of(context).size.height; // 自定义数值
AlivcPusherPreview livePlayView = AlivcPusherPreview(
      onCreated: _onLivePlayViewCreated,
      viewType: AlivcPusherPreviewType.play, 
      x: x,
      y: y,
      width: width,
      height: height);
  return Container(
        color: Colors.black,
        width: width,
        height: height,
        child: livePlayView);

// 视图创建回调
_onLivePlayViewCreated(id) {
    /// 6.设置播放View
    livePlayer.startPreview();
}
// 7.开始播放音视频流
String livePlayURL = "RTC拉流地址(artc://......)"; 
livePlayer.startPlayWithURL(livePlayURL);
/// 8.停止RTC拉流
livePlayer.stopPlay();

2.4.创建AliPlayer CDN播放

/// 1.工程yaml集成[flutter_aliplayer] plugin插件,版本号建议选择较新的
flutter_aliplayer: ^5.4.9
/// 2.创建播放器
FlutterAliplayer aliPlayer = FlutterAliPlayerFactory.createAliPlayer();
/// 3.设置渲染的 View
var x = 0.0; // 自定义数值
var y = 0.0; // 自定义数值
var width = MediaQuery.of(context).size.width; // 自定义数值
var height = MediaQuery.of(context).size.height; // 自定义数值
AliPlayerView aliPlayerView = AliPlayerView(
  onCreated: onViewPlayerCreated,
  x: x,
  y: y,
  width: width,
  height: height);
return Container(
        color: Colors.black,
        width: width,
        height: height,
        child: aliPlayerView);
// 视图创建回调
void onViewPlayerCreated(viewId) async {
  /// 4.绑定视图
  aliPlayer.setPlayerView(viewId);
  /// 5.设置播放源
  String playerURL = "CDN拉流地址(artc://......)"; 
  aliPlayer.setUrl(playerURL);
}

/// 6.开始CDN拉流
aliPlayer.setAutoPlay(true);
aliPlayer.prepare();
/// 7.停止CDN拉流
aliPlayer.stop();
/// 8.销毁播放器
aliPlayer.destroy();

2.5.连麦混流

/// 1.创建混流配置实例
AlivcLiveTranscodingConfig transcodingConfig = AlivcLiveTranscodingConfig.init();
/// 2.绑定云端的混流(转码)参数
livePusher.bindLiveMixTranscodingConfig();
/// 3.设置混流
AlivcLiveMixStream mixStream = AlivcLiveMixStream();
mixStream.userId = "<userId>";
mixStream.x = 0; // 自定义数值
mixStream.y = 0; // 自定义数值
mixStream.width = 0; // 自定义数值
mixStream.height = 0; // 自定义数值
mixStream.zOrder = 1;
List mixStreams = [mixStream, ...];
transcodingConfig.setMixStreams(mixStreams);

/// 4.设置混流参数
livePusher.setLiveMixTranscodingConfig(true);

3.添加美颜 #

Flutter直播推流SDK提供插件化的美颜处理能力。如需使用美颜功能,请下载Flutter直播推流Demo源码,找到example包plugins目录下的flutter_livepush_beauty_plugin插件,配合使用。注意:美颜插件不对外发布。

/// 1.初始化美颜对象
AlivcLiveBeautyManager beautyManager = AlivcLiveBeautyManager.init();
beautyManager.setupBeauty();
/// 2.打开美颜面板
beautyManager.showPanel();
/// 3.关闭美颜面板(安卓使用)
beautyManager.hidePanel();
/// 4.销毁美颜对象
beautyManager.destroyBeauty();

文档指引 #

2
likes
90
pub points
51%
popularity

Publisher

unverified uploader

Aliyun Live Push SDK for Flutter(Android and iOS)

Homepage

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on flutter_livepush_plugin