Android SDK
优质
小牛编辑
130浏览
2023-12-01
手机推流Android SDK文档
目录
如何接入
环境需求:
- JDK版本1.8
- Android SDK API Level ≥ 18
- App要求 Android 4.3及以上设备
- cpu架构支持 ARM、ARMV7、ARM64
接入步骤:
- 将
yagupusher.aar
、yaguplayer.aar
全部拖入app目录下的libs文件夹中。 - 在
build.gradle
中的dependencies
下修改如下代码后同步工程:
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
- 下
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);
拉流步骤
- 在
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>
- 在
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_SUCCESS | 268435456 | 连接成功 |
YAGUPUSH_MSG_CONNECT_TIMEOUT | 268435461 | 连接超时 |
YAGUPUSH_MSG_CONNECT_FAILED | 268435462 | 连接失败,断流 |
YAGUPUSH_MSG_PUSH_FAILED | 268435463 | 推送失败 |
YAGUPUSH_MSG_NETWORK_BLOCK | 268435464 | 通信模块错误 |
YAGUPUSH_MSG_PUSH_URL | 268435465 | 地址错误 |
YAGUPUSH_MSG_TUNED_BITRATE | 268435466 | 码率变化 |
YAGUPUSH_MSG_VIDEO_FPS | 268435467 | 帧率 |
YAGUPUSH_MSG_PUSH_SPEED | 268435468 | 速度变化 |
YAGUPUSH_MSG_PUSH_DELAY | 268435469 | 推流延时 |
YAGUPUSH_MSG_IPADDRESS | 268435470 | ip地址 |
YAGUPUSH_MSG_FACE_INFO | 268435473 | |
YAGUPUSH_MSG_PKTSIZE | 268435474 | 包大小 |
YAGUPUSH_MSG_FIRSTPKT_TIME | 268435475 | 首包大小 |
YAGUPUSH_MSG_LOSTPKT_NUM | 268435476 | 丢包数 |
YAGUPUSH_MSG_DOMAINANALYTIC_TIME | 268435477 | |
YAGUPUSH_MSG_BITRATE_DOWN | 268435478 | 码率降低 |
YAGUPUSH_MSG_BITRATE_UP | 268435479 | 码率升高 |
YAGUPUSH_MSG_CAPTURE_SCREEN | 268435480 |
2.播放错误码
- INFO
字段 | 值 | 说明 |
---|---|---|
MEDIA_INFO_UNKNOWN | 1 | 未知信息 |
MEDIA_INFO_STARTED_AS_NEXT | 2 | 播放下一条 |
MEDIA_INFO_VIDEO_RENDERING_START | 3 | 视频开始整备中,准备渲染 |
MEDIA_INFO_VIDEO_TRACK_LAGGING | 700 | 视频日志跟踪 |
MEDIA_INFO_BUFFERING_START | 701 | 开始缓冲中 开始缓冲 |
MEDIA_INFO_BUFFERING_END | 702 | 网络带宽,网速方面 |
MEDIA_INFO_NETWORK_BANDWIDTH | 703 | 缓冲结束 |
MEDIA_INFO_BAD_INTERLEAVING | 800 | |
MEDIA_INFO_NOT_SEEKABLE | 801 | 不可设置播放位置,直播方面 |
MEDIA_INFO_METADATA_UPDATE | 802 | |
MEDIA_INFO_TIMED_TEXT_ERROR | 900 | |
MEDIA_INFO_UNSUPPORTED_SUBTITLE | 901 | 不支持字幕 |
MEDIA_INFO_SUBTITLE_TIMED_OUT | 902 | 字幕超时 |
MEDIA_INFO_VIDEO_INTERRUPT | 10000 | 数据连接中断,一般是视频源有问题或者数据格式不支持,比如音频不是AAC之类的 |
MEDIA_INFO_VIDEO_ROTATION_CHANGED | 10001 | 视频方向改变,视频选择信息 |
MEDIA_INFO_AUDIO_RENDERING_START | 10002 | 音频开始整备中 |
MEDIA_INFO_AUDIO_DECODED_START | 10003 | |
MEDIA_INFO_VIDEO_DECODED_START | 10004 | |
MEDIA_INFO_OPEN_INPUT | 10005 | |
MEDIA_INFO_FIND_STREAM_INFO | 10006 | |
MEDIA_INFO_COMPONENT_OPEN | 10007 | |
MEDIA_INFO_VIDEO_SEEK_RENDERING_START | 10008 | |
MEDIA_INFO_AUDIO_SEEK_RENDERING_START | 10009 | |
MEDIA_INFO_MEDIA_ACCURATE_SEEK_COMPLETE | 10100 | |
MEDIA_INFO_NETWORK_SPEED | 10003 | |
MEDIA_INFO_PKTSIZE | 10004 | |
MEDIA_INFO_PLAY_DISCONTINUOUS | 704 | |
MEDIA_INFO_SNAP | 705 | |
MEDIA_INFO_DOMAINANALYTIC_TIME | 706 | |
MEDIA_INFO_CONNECT_TIME | 707 |
- ERROR
字段 | 值 | 说明 |
---|---|---|
MEDIA_ERROR_UNKNOWN | 1 | |
MEDIA_ERROR_SERVER_DIED | 100 | 服务挂掉,视频中断,一般是视频源异常或者不支持的视频类型。 |
MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK | 200 | 数据错误没有有效的回收 |
MEDIA_ERROR_IO | -1004 | IO 错误 |
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.**