You need to enable JavaScript to run this app.
视频直播

视频直播

Copy page
Download PDF
Android
基础功能
Copy page
Download PDF
基础功能

本章节为您介绍推流基础功能的接入方法,根据文档提供的操作步骤进行配置,您可接入直播推流能力。

前提条件

已集成并初始化 Android 推流 SDK。详见集成 Android 推流 SDK初始化 Android 推流 SDK

注意事项

真机调试:由于 SDK 使用了大量 Android 系统的音视频接口,这些接口在仿真模拟器下可能会出现异常,推荐您使用真机进行代码调试。

功能接入

本节为您详细介绍如何通过推流 SDK 实现直播推流控制的能力,包括但不限于创建推流引擎、初始化配置、事件监听、配置渲染 View、配置推流地址。

初始化配置

通过修改参数属性值,配置推流引擎的视频采集参数和音频采集参数。每个参数均有默认值,您可以使用默认配置,也可以根据业务场景进行修改,关于参数含义和默认值请参见注释。

  • 视频采集配置

    // 初始化视频采集配置类
    VeLiveVideoCaptureConfiguration videoCaptureConfig = new VeLiveVideoCaptureConfiguration();
    // 视频采集宽度,单位为 px,默认值为 720。
    videoCaptureConfig.setWidth(720);
    // 视频采集高度,单位为 px,默认值为 1280。
    videoCaptureConfig.setHeight(1280);
    // 视频采集帧率,单位为 fps,默认值为 15。
    videoCaptureConfig.setFps(15);
    
  • 音频采集配置

    // 初始化音频采集配置类
    VeLiveAudioCaptureConfiguration audioCaptureConfig = new VeLiveAudioCaptureConfiguration();
    // 音频采样率,默认值为 `VeLiveAudioSampleRate44100`
    audioCaptureConfig.setSampleRate(VeLiveAudioSampleRate44100);
    // 音频采集声道数,默认值为 `VeLiveAudioChannelStereo`
    audioCaptureConfig.setChannel(VeLiveAudioChannelStereo);
    
  • 推流引擎配置

    // 初始化推流引擎配置类
    VeLivePusherConfiguration config = new VeLivePusherConfiguration();
    // 设置上下文
    config.setContext(mContext);
    // 推流失败后,尝试重连的次数,默认值为 3。
    config.setReconnectCount(3);
    // 推流失败后,尝试重连的时间间隔,单位为 s,默认值为 5。
    config.setReconnectIntervalSeconds(5);
    // 视频采集参数设置
    config.setVideoCaptureConfig(videoCaptureConfig);
    // 音频采集参数设置
    config.setAudioCaptureConfig(audioCaptureConfig);
    

创建引擎

本节为您介绍创建推流引擎的方法,需要您提前完成初始化配置。代码示例如下所示:

mLivePusher = config.build();

配置事件监听

本节为您介绍推流引擎的事件监听方式。代码示例如下所示:

// 设置推流引擎基础事件回调监听
mLivePusher.setObserver(this);
// 设置周期性信息回调监听
mLivePusher.setStatisticsObserver(this, 3);

// 添加视频帧回调监听
mLivePusher.addVideoFrameListener(this);
// 移除视频帧回调监听
mLivePusher.removeVideoFrameListener(this);

// 添加音频帧回调监听
mLivePusher.addAudioFrameListener(this);
// 移除音频帧回调监听
mLivePusher.removeAudioFrameListener(this);

配置预览视图

如果您使用推流引擎自带的预览视图,需要调用下面方法进行配置。

mLivePusher.setRenderView(findViewById(R.id.render_view));

配置镜像方案

如果您需要使用镜像,我们提供了 3 种镜像方案供您选择。

  • 采集镜像
    本功能会在采集时对视频帧进行镜像效果处理,预览和推流都会产生镜像效果。代码示例如下所示:

    mLivePusher.setVideoMirror(VeLiveVideoMirrorCapture, true);
    
  • 本地预览镜像
    本功能会镜像本地的预览视图,不影响采集和推流。代码示例如下所示:

    mLivePusher.setVideoMirror(VeLiveVideoMirrorPreview, true);
    
  • 推流镜像
    本功能会镜像编码前的视频帧,不影响预览和采集。代码示例如下所示:

    mLivePusher.setVideoMirror(VeLiveVideoMirrorPushStream, true);
    

视频采集控制

为了适应多种业务需求,我们提供了 7 种视频采集模式,并支持在采集模式之间进行切换。

  • 前置摄像头采集
    本功能启用前置摄像头采集功能。代码示例如下所示:

    // 开启前置摄像头采集
    mLivePusher.startVideoCapture(VeLiveVideoCaptureFrontCamera);
    // 切换至前置摄像头采集
    mLivePusher.switchVideoCapture(VeLiveVideoCaptureFrontCamera);
    
  • 后置摄像头采集
    本功能启用后置摄像头采集功能。代码示例如下所示:

    // 开启后置摄像头采集
    mLivePusher.startVideoCapture(VeLiveVideoCaptureBackCamera);
    // 切换至后置摄像头采集
    mLivePusher.switchVideoCapture(VeLiveVideoCaptureBackCamera);
    
  • 外部视频采集
    当您不需要使用推流引擎自带的视频采集能力时,可以开启外部视频采集。更多详情请参考外部采集源推流。代码示例如下所示:

    warning

    当您开启了外部视频采集,需要调用 mLivePusher.pushExternalVideoFrame(new VeLiveVideoFrame()); 送入视频帧数据。

    // 开始外部视频采集
    mLivePusher.startVideoCapture(VeLiveVideoCaptureExternal);
    // 切换至外部视频采集
    mLivePusher.switchVideoCapture(VeLiveVideoCaptureExternal);
    
  • 自定义图片
    自定义图片模式支持您使用自定义图片作为视频源进行直播,或在主播 App 进入后台时使用自定义图片作为视频源。更多详情请参考图片推流

    warning

    在使用本接口之前,您需要先调用 mLivePusher.updateCustomImage(Bitmap.createBitmap(100,100, Bitmap.Config.ARGB_8888)); 更新自定义图片。

    代码示例如下所示:

    // 开始自定义图片推流
    mLivePusher.startVideoCapture(VeLiveVideoCaptureCustomImage);
    // 切换至自定义图片推流
    mLivePusher.switchVideoCapture(VeLiveVideoCaptureCustomImage);
    
  • 尾帧
    当主播 App 进入后台时,您可以使用最近采集的最后一帧图像作为视频源继续进行直播。代码示例如下所示:

    warning

    • 使用尾帧作为视频源,前提必须至少要有一个视频帧数据传入给推流引擎。
    • 使用场景一般为由其它采集类型切换至尾帧采集类型。
    // 切换至尾帧推流
    mLivePusher.switchVideoCapture(VeLiveVideoCaptureLastFrame);
    
  • 黑帧
    当主播 App 进入后台时,您可以使用纯黑色视频帧继续进行直播。代码示例如下所示:

    // 开始黑帧推流
    mLivePusher.startVideoCapture(VeLiveVideoCaptureDummyFrame);
    // 切换至黑帧推流
    mLivePusher.switchVideoCapture(VeLiveVideoCaptureDummyFrame);
    
  • 屏幕采集
    通过屏幕采集推流,主播可以将手机屏幕上的内容实时推送到服务器,让观众能够看到主播的屏幕操作或游戏画面。屏幕采集推流适用于游戏直播、移动端 App 演示等场景。具体实现方法,详见录屏推流

音频采集控制

为了适应多种业务需求,我们提供了 3 种音频采集模式,并支持在采集模式之间进行切换。

  • 麦克风采集
    使用推流引擎自带的麦克风采集能力。代码示例如下所示:

    // 开启麦克风采集
    mLivePusher.startAudioCapture(VeLiveAudioCaptureMicrophone);
    // 切换至麦克风采集
    mLivePusher.switchAudioCapture(VeLiveAudioCaptureMicrophone);
    
  • 外部采集
    当您不需要使用推流引擎自带的麦克风采集能力时,您可以自行采集音频数据并送入推流引擎中。代码示例如下所示:

    warning

    开启了外部音频采集,需要调用 mLivePusher.pushExternalAudioFrame(new VeLiveAudioFrame()); 送入音频帧数据。

    // 开启外部音频采集
    mLivePusher.startAudioCapture(VeLiveAudioCaptureExternal);
    // 切换至外部音频采集
    mLivePusher.switchAudioCapture(VeLiveAudioCaptureExternal);
    
  • 静音帧
    当您只需要进行视频直播而不需要音频时,可以开启音频静音采集。代码示例如下所示:

    tip

    主播使用静音帧采集和开启静音,在观众端看到的效果一致。区别在于静音帧采集不会真正启用设备的音频采集能力。

    // 开启静音帧
    mLivePusher.startAudioCapture(VeLiveAudioCaptureMuteFrame);
    // 切换至静音帧
    mLivePusher.switchAudioCapture(VeLiveAudioCaptureMuteFrame);
    

视频编码参数控制

您可以通过调用 setVideoEncoderConfiguration 设置视频编码参数。您可以在推流开始后通过该接口动态修改分辨率和码率。我们为您提供了不同清晰度的最佳视频编码参数,您也可以根据您的需求进行调整。代码示例如下所示:

VeLiveVideoEncoderConfiguration videoEncoderConfig = new VeLiveVideoEncoderConfiguration();
// 设置清晰度,SDK 会根据不同的清晰度配置最佳默认参数
videoEncoderConfig.setResolution(VeLiveVideoResolution720P);
// 推流视频编码格式,默认值为 VeLiveVideoCodecH264
videoEncoderConfig.setCodec(VeLiveVideoCodecH264);
// 视频目标编码码率,单位为 kbps,默认值为 1200。
videoEncoderConfig.setBitrate(1200);
// 视频最小编码码率,单位为 kbps,默认值为 800;如果开启自适应码率,推流 SDK 根据网络情况,进行编码码率自适应的最小码率。
videoEncoderConfig.setMinBitrate(800);
// 视频最大编码码率,单位为 kbps,默认值为 1900;如果开启自适应码率,推流 SDK 根据网络情况,进行编码码率自适应的最大码率。
videoEncoderConfig.setMaxBitrate(1900);
// 视频 GOP 大小,单位为 s,默认值为 2。
videoEncoderConfig.setGopSize(2);
// 视频编码帧率,单位为 fps,默认值为 15。
videoEncoderConfig.setFps(15);
// 是否启用 B 帧,默认值为 false
videoEncoderConfig.setEnableBFrame(false);
// 是否开启硬件编码,默认值为 true
videoEncoderConfig.setEnableAccelerate(true);
// 配置编码参数
mLivePusher.setVideoEncoderConfiguration(videoEncoderConfig);

音频编码参数控制

我们为您提供了一套最佳音频编码参数配置,您也可以根据需求进行调整。代码示例如下所示:

VeLiveAudioEncoderConfiguration audioEncoderConfig = new VeLiveAudioEncoderConfiguration();
// 音频编码码率,单位为 kbps,默认值为 64。
audioEncoderConfig.setBitrate(64);
// 音频编码采样率,默认值为 VeLiveAudioSampleRate44100
audioEncoderConfig.setSampleRate(VeLiveAudioSampleRate44100);
// 音频声道数,默认值为 VeLiveAudioChannelStereo
audioEncoderConfig.setChannel(VeLiveAudioChannelStereo);
// AAC 编码类型,默认值为 VeLiveAudioAACProfileLC
audioEncoderConfig.setProfile(VeLiveAudioAACProfileLC);
// 配置音频编码参数
mLivePusher.setAudioEncoderConfiguration(audioEncoderConfig);

美颜控制

美颜特效是直播场景的基础功能,视频直播 SDK 内置了智能美化特效 SDK 的相关接口,帮助您完成美颜特效能力的快速接入。在开始对接前,请先联系商务,获取对应版本的 SDK(推荐使用 4.2.3 及以上版本)、授权文件 License 和特效资源包。

warning

在使用本节介绍的美颜方法时,请确保工程已完成集成智能美化特效 SDK

  • 初始化美颜管理器

    // 注意:本方法只在工程中集成过智能美化特效的SDK时生效
    VeLiveVideoEffectManager effectManager = mLivePusher.getVideoEffectManager();
    // 特效鉴权License路径,请根据工程配置查找正确的路径
    String licPath = VeLiveEffectHelper.getLicensePath("xxx.licbag");
    // 特效模型资源包路径
    String algoModePath = VeLiveEffectHelper.getModelPath();
    // 创建美颜配置
    VeLivePusherDef.VeLiveVideoEffectLicenseConfiguration licConfig = VeLivePusherDef.VeLiveVideoEffectLicenseConfiguration.create(licPath);
    // 设置美颜配置
    effectManager.setupWithConfig(licConfig);
    // 设置算法包路径
    effectManager.setAlgorithmModelPath(algoModePath);
    // 开启美颜特效处理
    effectManager.setEnable(true, new VeLivePusherDef.VeLiveVideoEffectCallback() {
        @Override
        public void onResult(int result, String msg) {
            if (result != 0) {
                Log.e("VeLiveQuickStartDemo", "Effect init error:" + msg);
            }
        }
    });
    
  • 设置美颜特效

    • 设置美颜美型

      // 根据特效资源包,查找正确的资源路径,一般到 reshape_lite, beauty_Android_lite 目录
      String beautyPath = "xxx/ComposeMakeup.bundle/xxx";
      // 设置美颜美型特效资源包
      mLivePusher.getVideoEffectManager().setComposeNodes(new String[]{ beautyPath });
      // 设置美颜美型特效强度, NodeKey 可在 资源包下的 .config_file 中获取,如果没有 .config_file ,请联系商务咨询
      mLivePusher.getVideoEffectManager().updateComposerNodeIntensity(beautyPath, "whiten", 0.5F);
      
    • 设置滤镜

      // 滤镜资源包,查找正确的资源路径,一般到 Filter_01_xx 目录
      String filterPath = "xxx/FilterResource.bundle/xxx";
      // 设置滤镜资源包路径
      mLivePusher.getVideoEffectManager().setFilter(filterPath);
      // 设置滤镜特效强度
      mLivePusher.getVideoEffectManager().updateFilterIntensity(0.5F);
      
    • 设置贴纸

      // 贴纸资源包,查找正确的资源路径,一般到 stickers_xxx 目录
      String stickerPath = "xxx/StickerResource.bundle/xxx";
      // 设置贴纸资源包路径
      mLivePusher.getVideoEffectManager().setSticker(stickerPath);
      

水印控制

支持半透明水印,您可以根据需要设置半透明的PNG图片。代码示例如下所示:

// Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888),为创建一个大小为 100*100 像素的水印图片,取值为 null 时,表示禁用水印
// 0.2f :水印的水平偏移量,即水印图片相对视频流左上角的横向偏移与视频流宽度的比值,0.2 表示水印在视频宽度的 20% 处水平偏移,取值范围为 [0.0,1.0]
// 0.3f :水印的垂直偏移量,即水印图片相对视频流左上角的纵向偏移与视频流高度的比值,0.3 表示水印在视频高度的 30% 处垂直偏移,取值范围为 [0.0,1.0]
// 1.0f :水印图片的缩放比例,1.0表示不进行缩放,保持原始大小,取值范围为 [0.0,1.0]
mLivePusher.setWatermark(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888), 0.2f, 0.3f, 1.0f);

相机控制

在使用推流引擎自带的摄像头采集时,我们提供了多种相机控制能力。代码示例如下所示:

  • 闪光灯

    // 开启闪光灯
    mLivePusher.getCameraDevice().enableTorch(true);
    // 关闭闪光灯
    mLivePusher.getCameraDevice().enableTorch(false);
    
  • 摄像头缩放

    // 获取最大支持缩放倍数
    float maxRatio = mLivePusher.getCameraDevice().getMaxZoomRatio();
    // 获取最小支持缩放倍数
    float minRatio = mLivePusher.getCameraDevice().getMinZoomRatio();
    // 设置缩放倍数
    float ratio = 3.0f;
    mLivePusher.getCameraDevice().setZoomRatio(Float.min(maxRatio, Float.max(minRatio, ratio)));
    
  • 自动对焦

    if (mLivePusher.getCameraDevice().isAutoFocusEnabled()) {
        // 关闭自动对焦
        mLivePusher.getCameraDevice().enableAutoFocus(false);
    } else {
        // 开启自动对焦
        mLivePusher.getCameraDevice().enableAutoFocus(true);
    }
    
  • 手动对焦

    // 设置手动对焦焦点
    // viewW: Surface 宽, 单位 px viewH: Surface 高,单位 px
    // x: 距离屏幕左侧的距离,单位 px
    // y: 距离屏幕顶部的距离,单位 px
    mLivePusher.getCameraDevice().setFocusPosition(100, 100, 50, 50);
    

音频控制

在使用推流引擎自带音频采集能力时,我们提供了多种音频控制能力。代码示例如下所示:

  • 音量响度

    // 获取当前音量响度
    float voiceLoudness = mLivePusher.getAudioDevice().getVoiceLoudness();
    // 设置音量响度,取值范围 [0.0-1.0]
    mLivePusher.getAudioDevice().setVoiceLoudness(0.5);
    
  • 耳返

    // 是否开启了耳返
    boolean echoEnable = mLivePusher.getAudioDevice().isEnableEcho();
    if (echoEnable) {
        // 关闭耳返
        mLivePusher.getAudioDevice().enableEcho(false);
    } else {
        // 开启耳返
        mLivePusher.getAudioDevice().enableEcho(true);
    }
    

横竖屏控制

我们提供了横竖屏控制能力。代码示例如下所示:

// 设置横竖屏方向
mLivePusher.setOrientation(VeLiveOrientationLandscape);

静音控制

我们提供了静音控制能力。代码示例如下所示:

if (mLivePusher.isMute()) {
    // 取消静音
    mLivePusher.setMute(false);
} else {
    // 开启静音
    mLivePusher.setMute(true);
}

日志级别

您可以通过调用 setLogLevel 设置打印到控制台的日志级别,代码示例如下:

// 输出 DEBUG、INFO、WARNING 和 ERROR 级别的日志

VeLivePusher.setLogLevel(VeLiveLogLevelDebug);

开始推流

调用推流引擎的 startPush 开始推流。代码示例如下所示:

tip

推流地址获取方式请参见生成推流地址

mLivePusher.startPush("rtmp://push.example.com/push");

停止推流

调用推流引擎的 stopPush 停止推流。代码示例如下所示:

mLivePusher.stopPush();

销毁引擎

调用推流引擎的 release 销毁引擎。代码示例如下所示:

mLivePusher.release();
mLivePusher = null;
Last updated: 2026.03.31 15:33:48