Android SDK

优质
小牛编辑
131浏览
2023-12-01

手机推流Android SDK文档

目录

  1. 如何接入
  2. 推流步骤
  3. 拉流步骤
  4. 错误码说明
  5. 添加混淆文件
  6. SDK和Demo下载

如何接入

环境需求:

  • JDK版本1.8
  • Android SDK API Level ≥ 18
  • App要求 Android 4.3及以上设备
  • cpu架构支持 ARM、ARMV7、ARM64

接入步骤:

  1. yagupusher.aaryaguplayer.aar全部拖入app目录下的libs文件夹中。
  2. build.gradle中的dependencies下修改如下代码后同步工程:
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
  1. AndroidManifest文件下添加如下权限代码:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

说明: 请您务必注意添加录音权限和相机权限。

推流步骤

1.创建surfaceView

mSurfaceView = new YaguGLSurfaceView(this);
RelativeLayout.LayoutParams surfaceView_layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

mSurfaceView.setLayoutParams(surfaceView_layoutParams);
mSurfaceView.setIsRemoveOrAdd(true);
mSurfaceContainer.addView(mSurfaceView);
mSurfaceHolder = mSurfaceView.getHolder(); // 绑定SurfaceView,取得SurfaceHolder对象
mSurfaceHolder.addCallback(this);
mSurfaceView.setPreserveEGLContextOnPause(true);
mSurfaceView.setOnTouchListener(this);
mSurfaceView.setKeepScreenOn(true);

2.初始化推流引擎

mEngine = new YaguLiveEngine_Ex(mSurfaceView, this, mEngine_Type, mBeauty_Type,true);
mEngine.init(this, mEngine_Type);
mEngine.setLog(true);

3.开启预览

//屏幕方向,竖屏|横屏
int rotation = ((WindowManager)this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
mEngine.setPreviewSizeType(CameraCapture.PreviewSizeType.PREVIEWSIZE16_9);
//land横屏为true, 竖屏为false
mEngine.startPreview(mCamraID, null, YaguLiveEngine_Ex.Render_Mode.fit_out.ordinal(), rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270);

4.推荐推流配置

mconfig.setEncodeType(PushParam.PUSH_DATA_HARD_ENCODE);
mconfig.setVideoBitRate(1200*1000);//视频码率
mconfig.setAudioBitRate(48000);//声音码率
mconfig.setCrf(26);//画面压缩参数
mconfig.setNoVideo(false);

mconfig.setVideoFormat(ImageFormat.NV21);
mconfig.setVideoFrameRate(30);//帧率

mconfig.setVideoSize(YaguPushConfig.Push_Video_Size.SIZE_720);//分辨率
mconfig.setProfile(PushParam.PUSH_MAIN_PROFILE);
mconfig.setLogFlag(mconfig.PUSH_LOGFLAG_DEBUG);

5.开始推流

mEngine.setIsAdaptiveBitrate(true, YaguLiveEngine_Ex.AdaptiveBitrate_level.AdaptiveBitrate_1.ordinal());
mEngine.configEngine(mconfig);
mEngine.createEngine();
mEngine.setOnMessageListener(this, this, this,this);

mEngine.startPush(pushUrl, true);
mEngine.setAudioDataCallbackInterface(this);

6.退出销毁

if (isStartPreview) {
    try {
        mEngine.stopPreview();
        isStartPreview = false;
    } catch (Exception e) {
        Log.d(TAG, "stopPreview error");
    }

}
if (startPush) {

    try {
        mEngine.stopPush();
        mEngine.uinit();
        mEngine.destroyEngine();
        startPush = false;
    } catch (Exception e) {
        Log.d(TAG, "stopPreview error");
    }
}
进入后台
  • 当推流端页面进入后台后,需调用enterBackGround做响应。进入后台后,不会引起断流,继续推音频流。在回到前台时,需在onResume中重新调用startPreview
@Override
protected void onResume() {
    Log.d(TAG, "MainActivity: onResume");
    super.onResume();

    if (mEngine != null) {
        int rotation = ((WindowManager)this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
        mEngine.startPreview(mCamraID, null, YaguLiveEngine_Ex.Render_Mode.fit_out.ordinal(), rotation== Surface.ROTATION_90||rotation== Surface.ROTATION_270);
    }
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
    Log.e(TAG, "MainActivity: --------surfaceCreated-----");
    if (startPush && mEngine != null) {
        mEngine.enterBackGround(false);
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    Log.e(TAG, "MainActivity: --------surfaceDestroyed-----");
    mEngine.stopPreview();
    if (startPush && mEngine != null) {
        mEngine.enterBackGround(true);
    }
}
附加功能

1.静音

//开启
mEngine.mutedLocalStream(0, 2, true);
//关闭
mEngine.mutedLocalStream(0, 2, false);

2.开关闪光灯

//开启
mEngine.getCamCapture().turnLightOn();
//关闭
mEngine.getCamCapture().turnLightOff();

3.美颜

//开启
mEngine.setEffectType(EffectParam.PREVIEW_EFFECT_BEAUTY, beautyParam);
//关闭
mEngine.setEffectType(EffectParam.PREVIEW_EFFECT_NORMAL, null);

拉流步骤

  1. AndroidManifesat文件下播放端Activity配置如下:
<activity android:name=".PlayActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:mimeType="video/*" />
        <data android:mimeType="audio/*" />
        <data android:scheme="http" />
        <data android:scheme="file" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="video/*" />
    </intent-filter>
</activity>
  1. xml中使用videoView ```xml </com.mudu.yaguplayer.video.widget.media.IjkVideoView>

3. 获取VideoView,设置播放地址并播放
```java
mVideoView = findViewById(R.id.video_view);
mVideoView.setVideoPath(mVideoPath);
mVideoView.start();

重新播放

mVideoView.setVideoPath(inputVideoPath);
mVideoView.resume();
mVideoView.start();

结束播放

mVideoView.stopPlayback();
mVideoView.release(true);
mVideoView.stopBackgroundPlay();

错误码说明

1.推流错误码

字段说明
YAGUPUSH_MSG_CONNECT_SUCCESS268435456连接成功
YAGUPUSH_MSG_CONNECT_TIMEOUT268435461连接超时
YAGUPUSH_MSG_CONNECT_FAILED268435462连接失败,断流
YAGUPUSH_MSG_PUSH_FAILED268435463推送失败
YAGUPUSH_MSG_NETWORK_BLOCK268435464通信模块错误
YAGUPUSH_MSG_PUSH_URL268435465地址错误
YAGUPUSH_MSG_TUNED_BITRATE268435466码率变化
YAGUPUSH_MSG_VIDEO_FPS268435467帧率
YAGUPUSH_MSG_PUSH_SPEED268435468速度变化
YAGUPUSH_MSG_PUSH_DELAY268435469推流延时
YAGUPUSH_MSG_IPADDRESS268435470ip地址
YAGUPUSH_MSG_FACE_INFO268435473
YAGUPUSH_MSG_PKTSIZE268435474包大小
YAGUPUSH_MSG_FIRSTPKT_TIME268435475首包大小
YAGUPUSH_MSG_LOSTPKT_NUM268435476丢包数
YAGUPUSH_MSG_DOMAINANALYTIC_TIME268435477
YAGUPUSH_MSG_BITRATE_DOWN268435478码率降低
YAGUPUSH_MSG_BITRATE_UP268435479码率升高
YAGUPUSH_MSG_CAPTURE_SCREEN268435480

2.播放错误码

  • INFO
字段说明
MEDIA_INFO_UNKNOWN1未知信息
MEDIA_INFO_STARTED_AS_NEXT2播放下一条
MEDIA_INFO_VIDEO_RENDERING_START3视频开始整备中,准备渲染
MEDIA_INFO_VIDEO_TRACK_LAGGING700视频日志跟踪
MEDIA_INFO_BUFFERING_START701开始缓冲中 开始缓冲
MEDIA_INFO_BUFFERING_END702网络带宽,网速方面
MEDIA_INFO_NETWORK_BANDWIDTH703缓冲结束
MEDIA_INFO_BAD_INTERLEAVING800
MEDIA_INFO_NOT_SEEKABLE801不可设置播放位置,直播方面
MEDIA_INFO_METADATA_UPDATE802
MEDIA_INFO_TIMED_TEXT_ERROR900
MEDIA_INFO_UNSUPPORTED_SUBTITLE901不支持字幕
MEDIA_INFO_SUBTITLE_TIMED_OUT902字幕超时
MEDIA_INFO_VIDEO_INTERRUPT10000数据连接中断,一般是视频源有问题或者数据格式不支持,比如音频不是AAC之类的
MEDIA_INFO_VIDEO_ROTATION_CHANGED10001视频方向改变,视频选择信息
MEDIA_INFO_AUDIO_RENDERING_START10002音频开始整备中
MEDIA_INFO_AUDIO_DECODED_START10003
MEDIA_INFO_VIDEO_DECODED_START10004
MEDIA_INFO_OPEN_INPUT10005
MEDIA_INFO_FIND_STREAM_INFO10006
MEDIA_INFO_COMPONENT_OPEN10007
MEDIA_INFO_VIDEO_SEEK_RENDERING_START10008
MEDIA_INFO_AUDIO_SEEK_RENDERING_START10009
MEDIA_INFO_MEDIA_ACCURATE_SEEK_COMPLETE10100
MEDIA_INFO_NETWORK_SPEED10003
MEDIA_INFO_PKTSIZE10004
MEDIA_INFO_PLAY_DISCONTINUOUS704
MEDIA_INFO_SNAP705
MEDIA_INFO_DOMAINANALYTIC_TIME706
MEDIA_INFO_CONNECT_TIME707
  • ERROR
字段说明
MEDIA_ERROR_UNKNOWN1
MEDIA_ERROR_SERVER_DIED100服务挂掉,视频中断,一般是视频源异常或者不支持的视频类型。
MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK200数据错误没有有效的回收
MEDIA_ERROR_IO-1004IO 错误
MEDIA_ERROR_MALFORMED-1007
MEDIA_ERROR_UNSUPPORTED-1010数据不支持
MEDIA_ERROR_TIMED_OUT-110数据超时

添加混淆文件

在proguard-rules.pro文件下添加如下代码:

-keep class com.mudu.**{*;}
-dontwarn com.mudu.**

-keep class com.yagu.**{*;}
-dontwarn com.yagu.**

-keep class com.audioengine.**{*;}
-dontwarn com.audioengine.**

-keep class tv.danmaku.**{*;}
-dontwarn tv.danmaku.**

Android SDK 下载 Android Demo 下载