通过阅读本文,您可以了解Android推流SDK的使用方法。
RTMP推流
说明 阿里云视频直播不允许同一时间向同一个推流URL进行多路推流(第二路推流会被拒绝)。
- 创建AliLiveEngine。
//创建RTMP相关配置对象
AliLiveRTMPConfig rtmpConfig = new AliLiveRTMPConfig();
//初始化码率配置
rtmpConfig.videoInitBitrate = 1000;
rtmpConfig.videoTargetBitrate = 1500;
rtmpConfig.videoMinBitrate = 600;
//创建直播推流配置
AliLiveConfig mAliLiveConfig = new AliLiveConfig(rtmpConfig);
//初始化分辨率、帧率、是否开启高清预览、暂停后默认显示图片
mAliLiveConfig.videoFPS = 20;
mAliLiveConfig.videoPushProfile = AliLiveConstants.AliLiveVideoPushProfile.AliLiveVideoProfile_540P;
mAliLiveConfig.enableHighDefPreview = false;
mAliLiveConfig.pauseImage = bitmap;
mAliLiveConfig.accountId = "";
AliLiveEngine mAliLiveEngine = AliLiveEngine.create(PushActivity.this, mAliLiveConfig);
- 开始预览。
//创建预览显示窗口
AliLiveRenderView mAliLiveRenderView = mAliLiveEngine.createRenderView(false);
//添加预览显示窗口到布局中
addSubView(mAliLiveRenderView);
//设置预览显示模式
mAliLiveEngine.setPreviewMode(AliLiveRenderModeAuto, AliLiveRenderMirrorModeOnlyFront);
//开始预览
mAliLiveEngine.startPreview(mAliLiveRenderView);
- 开始推流。
mAliLiveEngine.startPush(mPushUrl);
- 停止拉流。
//停止预览
mAliLiveEngine.stopPreview();
//停止推流
mAliLiveEngine.stopPush();
//销毁liveEngine
mAliLiveEngine.destroy();
mAliLiveEngine = null;
RTMP拉流
- 创建播放器。
//创建播放器
mAliPlayer = AliPlayerFactory.createAliPlayer(this.getApplicationContext());
//设置自动播放
mAliPlayer.setAutoPlay(true);
//设置mMaxDelayTime,建议范围500~5000,值越小,直播时延越小,卡顿几率越大,根据需要自行决定
PlayerConfig config = mAliPlayer.getConfig();
config.mMaxDelayTime = 1000;
mAliPlayer.setConfig(config);
- 添加显示的SurfaceView。
mSurfaceView.getHolder().addCallback(this);
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mAliPlayer!=null){
mAliPlayer.setSurface(holder.getSurface());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (mAliPlayer!=null){
mAliPlayer.surfaceChanged();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mAliPlayer!=null){
mAliPlayer.setSurface(null);
}
}
- 开始拉流。
//给播放器设置拉流地址
UrlSource source = new UrlSource();
source.setUri(mPullUrl);
mAliPlayer.setDataSource(source);
//开始拉流
mAliPlayer.prepare();
- 停止拉流。
mAliPlayer.stop();
mAliPlayer.setSurface(null);
mAliPlayer.release();
mAliPlayer = null;
主播PK
- 主播A发送请求至appserver,获取目前在线且符合要求的主播列表。
mSocketHandler.send(getSendMessage(CMD_GET_ROOM_LIST));
获取到主播列表后渲染到页面。
mOnlineRoomView.setRoomList(onlineRoomList);
- 主播A在列表中找到一个主播B,请求PK。
mSocketHandler.send(getSendMessage(CMD_APPLY_PK,room.getUserId(),room.getRoomId()));
- 主播B收到主播A的申请,并弹出提示。
showApplyPkDialog(applyPkNoticeBean);
- 主播B同意了主播A的申请后,开始拉RTC流,并发送同意PK信令。
//开始订阅主播A
startPk(mLastApplyPkNotice.getFromRtcPullUrl());
//开始发送同意PK信令
mSocketHandler.send(getSendMessage(CMD_APPROVE_PK,true ,mLastApplyPkNotice.getFromUserId(),mLastApplyPkNotice.getFromRoomId() ));
- 主播A收到主播B的同意申请后,开始拉RTC流。
//开始PK
startPk(approvePkNoticeBean.getRtcPullUrl());
onSubscribeResult
回调中收到订阅成功的通知,调整预览和订阅画面的布局。
- 观众端播放的RTMP地址有变化,收到NotifyPublish信令后,重新构建播放器播放新地址。
//判断是否和上次的主播拉流地址一致,不一致则重新拉流
if (!mRoomInfo.getRtmpPullUrl().equals(noticePublish.getAnchorRtmpPullUrl())){
mRoomInfo.setRtmpPullUrl(noticePublish.getAnchorRtmpPullUrl());
startPlayRTMP();
}
- 主播B停止PK,发送停止PK信令,停止拉RTC流,并恢复PK前画面。
//发送停止PK信令
mSocketHandler.send(getSendMessage(CMD_CANCEL_PK));
//恢复PK之前的UI
showPkSurfaceView(false);
- 观众端播放的RTMP地址有变化,收到NotifyPublish信令后,重新构建播放器播放新地址。
//判断是否和上次的主播拉流地址一致,不一致则重新拉流
if (!mRoomInfo.getRtmpPullUrl().equals(noticePublish.getAnchorRtmpPullUrl())){
mRoomInfo.setRtmpPullUrl(noticePublish.getAnchorRtmpPullUrl());
startPlayRTMP();
}