Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 基於anyrtc的sdk實現直播連麥互動,anyrtcsdk直播

基於anyrtc的sdk實現直播連麥互動,anyrtcsdk直播

編輯:關於android開發

基於anyrtc的sdk實現直播連麥互動,anyrtcsdk直播


基於anyrtc的sdk實現直播連麥互動

前言

1.由於粘貼了較大的代碼,造成內容比較長,可能會花費您較長的時間。 
一.有圖有真相

主播端 : http://d2.freep.cn/3tb_160831194207x8fa572812.gif
二.集成步驟,教你實現直播連麥互動

從github上https://github.com/AnyRTC/RTMPCHybirdEngine-Android下載下來,導入AS,結構如下圖所示。

1.初始化RTMP引擎

MainActivity.java

RTMPCHybird.Inst().Init(getApplicationContext());
"你的開發者ID", "liveDemo", "LslMfUyvcW8hv6NgY0n8FOWf5t1K+YWpoMzRsqwkBiE", "d6ff4b7d1b549f497a0b94cf500fd549");
以上的四個參數可以在https://www.anyrtc.io注冊開發者賬號並創建一個應用可得,如下圖所示。你可以把它放在繼承Application的onCreate()方法中。

2.獲取直播列表,隨時隨地觀看主播

MainActivity.java

RTMPCHttpSDK.GetLiveList(this, RTMPCHybird.Inst().GetHttpAddr(), 你的開發者ID", "liveDemo",     "d6ff4b7d1b549f497a0b94cf500fd549", mRTMPCHttpCallback);
");
");
"));
"));
"));
"));
"));
以上代碼可以獲取所有正在直播列表名單

3.發起直播,成就自己的直播夢
a.你可以自己設置推流地址和拉流地址,這裡我直接用他提供的地址了

RTMPUrlHelper.java

public class RTMPUrlHelper {
/**
public static final String RTMP_PUSH_URL = "rtmp://192.168.7.207:1935/live/%s";
/**
public static final String RTMP_PULL_URL = "rtmp://192.168.7.207:1935/live/%s";
/**
public static final String HLS_URL = "http://192.169.7.207/live/%s.m3u8";
/**
public static final String SHARE_WEB_URL = "http://123.59.68.21/rtmpc-demo/?%s";
b.這裡要使用到你創建應用的anyrtcid、推流拉流hls地址、直播名稱。到這一步你已經離你的直播夢完成了一半了<( ̄︶ ̄)>。

PreStartLiveActivity.java

String anyrtcId = RTMPCHttpSDK.getRandomString(12);
new JSONObject();
try {
"hosterId", "hostID");
"rtmp_url", rtmpPullUrl);
"hls_url", hlsUrl);
"topic", topic);
"anyrtcId", anyrtcId);
catch (JSONException e) {
new Bundle();
"hosterId", "hostID");
"rtmp_url", rtmpPushUrl);
"hls_url", hlsUrl);
"topic", topic);
"andyrtcId", anyrtcId);
"userData", item.toString());
new Intent(this, HosterActivity.class);
4.你的直播夢就在此就要實現了,打起精神(⊙o⊙)哦
a.從上個Activity拿到傳過來的數據

HosterActivity.java

 mHosterId = getIntent().getExtras().getString("hosterId");
"rtmp_url");
"andyrtcId");
"userData");
"hls_url");
"topic");
這裡的R.id.rl_rtmpc_videos表示打開手機攝像頭捕獲的像呈現的地方,如下圖所示。 
mVideoView = new RTMPCVideoView((RelativeLayout) findViewById(R.id.rl_rtmpc_videos), RTMPCHybird.Inst().Egl(), true);
new RTMPCHosterKit(this, mHosterListener);
true);

b.這裡是創建和音頻管理器,將照顧音頻路由,音頻模式,音頻設備枚舉等、存儲現有的音頻設置更改音頻模式達到最佳的VoIP性能。
           {
// Create and audio manager that will take care of audio routing,
// audio modes, audio device enumeration etc.
this, new Runnable() {
// This method will be called each time the audio state (number
// and
// type of devices) has been changed.
@Override
public void run() {
// Store existing audio settings and change audio mode to
// MODE_IN_COMMUNICATION for best possible VoIP performance.
true;
/**
/**
c.主播的直播回調接口

mHosterKit = new RTMPCHosterKit(this, mHosterListener); 
/**
private RTMPCAbstractHoster mHosterListener = new RTMPCAbstractHoster() {
/**
@Override
public void OnRtmpStreamOKCallback() {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
/**
@param times 重連次數
@Override
public void OnRtmpStreamReconnectingCallback(final int times) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
/**
@param delayMs 推流延時
@param netBand 推流碼流
@Override
public void OnRtmpStreamStatusCallback(final int delayMs, final int netBand) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
/**
@param code
@Override
public void OnRtmpStreamFailedCallback(int code) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
/**
@Override
public void OnRtmpStreamClosedCallback() {
/**
@param code 0: 連接成功
@param strErr 原因
@Override
public void OnRTCOpenLineResultCallback(final int code, String strErr) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (code == 0) {
else {
/**
@param strLivePeerID
@param strCustomID
@param strUserData
@Override
public void OnRTCApplyToLineCallback(final String strLivePeerID, final String strCustomID, final String strUserData) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
this, strLivePeerID, strCustomID);
/**
@param strLivePeerID
@param strCustomID
@param strUserData
@Override
public void OnRTCLineFullCallback(final String strLivePeerID, String strCustomID, String strUserData) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
this, getString(R.string.str_connect_full), Toast.LENGTH_LONG).show();
true);
/**
@param strLivePeerID
@Override
public void OnRTCCancelLineCallback(String strLivePeerID) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
this, getString(R.string.str_line_disconnect), Toast.LENGTH_LONG).show();
/**
@param code 207:請去AnyRTC官網申請賬號,如有疑問請聯系客服!
@param strReason
@Override
public void OnRTCLineClosedCallback(final int code, String strReason) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (code == 207) {
this, getString(R.string.str_apply_anyrtc_account), Toast.LENGTH_LONG).show();
/**
@param strLivePeerID
@Override
public void OnRTCOpenVideoRenderCallback(final String strLivePeerID) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
final VideoRenderer render = mVideoView.OnRtcOpenRemoteRender(strLivePeerID);
if (null != render) {
/**
@param strLivePeerID
@Override
public void OnRTCCloseVideoRenderCallback(final String strLivePeerID) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
0);
/**
@param strCustomID 消息的發送者id
@param strCustomName 消息的發送者昵稱
@param strCustomHeader 消息的發送者頭像url
@param strMessage 消息內容
@Override
public void OnRTCUserMessageCallback(final String strCustomID, final String strCustomName, final String strCustomHeader, final String strMessage) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
new ChatMessageBean(strCustomID, strCustomName, "", strMessage));
/**
@param strCustomID 彈幕的發送者id
@param strCustomName 彈幕的發送者昵稱
@param strCustomHeader 彈幕的發送者頭像url
@param strBarrage 彈幕的內容
@Override
public void OnRTCUserBarrageCallback(final String strCustomID, final String strCustomName, final String strCustomHeader, final String strBarrage) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
new ChatMessageBean(strCustomID, strCustomName, "", strBarrage));
new DanmakuItem(HosterActivity.this, new SpannableString(strBarrage), mDanmakuView.getWidth(), 0, R.color.yellow_normol, 18, 1);
/**
@param totalMembers 觀看總人數
@Override
public void OnRTCMemberListWillUpdateCallback(final int totalMembers) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
/**
@param strCustomID
@param strUserData
@Override
public void OnRTCMemberCallback(final String strCustomID, final String strUserData) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
new JSONObject(strUserData);
new ChatMessageBean(userData.getString("nickName"), "", userData.getString("headUrl"), ""));
catch (JSONException e) {
@Override
public void OnRTCMemberListUpdateDoneCallback() {
d.一些對應直播回調的方法

接下來就要說對應這些回調的方法了,這裡可以做關閉直播、直播分享、切換攝像頭和發送消息。 
public void OnBtnClicked(View btn) {
if (btn.getId() == R.id.btn_close) {
false;
else if (btn.getId() == R.id.btn_copy_hls) {
int index = mHlsUrl.lastIndexOf("/");
int lastIndex = mHlsUrl.lastIndexOf(".");
1, lastIndex));
else if (btn.getId() == R.id.btn_switch_camera) {
else if (btn.getId() == R.id.btn_send_message) {
"");
if (message.equals("")) {
return;
if (mCheckBarrage.isChecked()) {
"", message);
new DanmakuItem(HosterActivity.this, new SpannableString(message), mDanmakuView.getWidth(), 0, R.color.yellow_normol, 18, 1);
else {
"", message);
new ChatMessageBean(mNickname, mNickname, "", message));
else if (btn.getId() == R.id.iv_host_text) {
1);
this, editMessage);
5.主播端的已經完成了,現在該輪到你的小伙伴們看你直播~(≧▽≦)/~啦啦啦

a.前面已經說了,我們可以實時獲取開啟直播的主播,點擊列表就可以觀看主播直播。

MainActivity.java

@Override
public void onItemChildClick(ViewGroup viewGroup, View view, int i) {
new Intent(this, GuestActivity.class);
new Bundle();
"hls_url", listLive.get(i).getmHlsUrl());
"rtmp_url", listLive.get(i).getmRtmpPullUrl());
"anyrtcId", listLive.get(i).getmAnyrtcId());
"userData", new JSONObject().toString());
"topic", listLive.get(i).getmLiveTopic());
b.再實現游客端這個項目基本上就完成了,是不是有點小激動?

與主播端一樣,拿上個Activity傳過來的數據,其實這裡代碼與主播端的代碼基本上可以說是一模一樣,看完之後你應該會有我這樣的體會。 
mNickname = ((HybirdApplication)HybirdApplication.app()).getmNickname();
"rtmp_url");
"anyrtcId");
"hls_url");
//getIntent().getExtras().getString("guestId");
"topic");
mVideoView = new RTMPCVideoView((RelativeLayout) findViewById(R.id.rl_rtmpc_videos), RTMPCHybird.Inst().Egl(), false);
/**
new RTMPCGuestKit(this, mGuestListener);
// Create and audio manager that will take care of audio routing,
// audio modes, audio device enumeration etc.
this, new Runnable() {
// This method will be called each time the audio state (number
// and
// type of devices) has been changed.
@Override
public void run() {
// Store existing audio settings and change audio mode to
// MODE_IN_COMMUNICATION for best possible VoIP performance.
new JSONObject();
try {
"nickName", mNickname);
"headUrl", "");
catch (JSONException e) {
/**
/**
c.觀看直播的回調接口

mGuestKit = new RTMPCGuestKit(this, mGuestListener); 
/**
private RTMPCAbstractGuest mGuestListener = new RTMPCAbstractGuest() {
/**
@Override
public void OnRtmplayerOKCallback() {
/**
@param cacheTime 當前緩存時間
@param curBitrate 當前播放器碼流
@Override
public void OnRtmplayerStatusCallback(int cacheTime, int curBitrate) {
/**
@param time 緩沖時間
@Override
public void OnRtmplayerCacheCallback(int time) {
/**
@param errcode
@Override
public void OnRtmplayerClosedCallback(int errcode) {
/**
@param code 回調響應碼:0:正常;101:主播未開啟直播;
@param strReason 原因描述
@Override
public void OnRTCJoinLineResultCallback(final int code, String strReason) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (code == 0) {
else if (code == 101) {
this, R.string.str_hoster_not_live, Toast.LENGTH_LONG).show();
2000);
/**
@param code 0:申請連線成功;-1:主播拒絕連線
@Override
public void OnRTCApplyLineResultCallback(final int code) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (code == 0) {
"LocalCameraRender");
true);
else if (code == -1) {
this, R.string.str_hoster_refused, Toast.LENGTH_LONG).show();
false;
/**
@param strLivePeerID
@param strCustomID
@param strUserData
@Override
public void OnRTCOtherLineOpenCallback(String strLivePeerID, String strCustomID, String strUserData) {
/**
@param strLivePeerID
@Override
public void OnRTCOtherLineCloseCallback(String strLivePeerID) {
/**
@Override
public void OnRTCHangupLineCallback() {
//主播連線斷開
this.runOnUiThread(new Runnable() {
@Override
public void run() {
"LocalCameraRender");
false;
/**
@param code
@param strReason
@Override
public void OnRTCLineLeaveCallback(int code, String strReason) {
//主播關閉直播
this.runOnUiThread(new Runnable() {
@Override
public void run() {
this, R.string.str_hoster_leave, Toast.LENGTH_LONG).show();
2000);
/**
@param strLivePeerID
@Override
public void OnRTCOpenVideoRenderCallback(final String strLivePeerID) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
final VideoRenderer render = mVideoView.OnRtcOpenRemoteRender(strLivePeerID);
/**
@param strLivePeerID
@Override
public void OnRTCCloseVideoRenderCallback(final String strLivePeerID) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
0);
/**
@param strCustomID 消息的發送者id
@param strCustomName 消息的發送者昵稱
@param strCustomHeader 消息的發送者頭像url
@param strMessage 消息內容
@Override
public void OnRTCUserMessageCallback(final String strCustomID, final String strCustomName, final String strCustomHeader, final String strMessage) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
new ChatMessageBean(strCustomID, strCustomName, "", strMessage));
/**
@param strCustomID 彈幕的發送者id
@param strCustomName 彈幕的發送者昵稱
@param strCustomHeader 彈幕的發送者頭像url
@param strBarrage 彈幕的內容
@Override
public void OnRTCUserBarrageCallback(final String strCustomID, final String strCustomName, final String strCustomHeader, final String strBarrage) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
new ChatMessageBean(strCustomID, strCustomName, "", strBarrage));
new DanmakuItem(GuestActivity.this, new SpannableString(strBarrage), mDanmakuView.getWidth(), 0, R.color.yellow_normol, 18, 1);
/**
@param totalMembers 觀看直播的總人數
@Override
public void OnRTCMemberListWillUpdateCallback(final int totalMembers) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
/**
@param strCustomID
@param strUserData
@Override
public void OnRTCMemberCallback(final String strCustomID, final String strUserData) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
new JSONObject(strUserData);
new ChatMessageBean(userData.getString("nickName"), "", userData.getString("headUrl"), ""));
catch (JSONException e) {
@Override
public void OnRTCMemberListUpdateDoneCallback() {
d.一些對應觀看直播回調的方法

接下來就要說對應這些回調的方法了,這裡可以做直播分享、申請連麥、斷開連麥和發送消息。 
public void OnBtnClicked(View btn) {
if(btn.getId() == R.id.btn_copy_hls) {
int index = mHlsUrl.lastIndexOf("/");
int lastIndex = mHlsUrl.lastIndexOf(".");
1, lastIndex));
else if (btn.getId() == R.id.btn_line) {
if (!mStartLine) {
new JSONObject();
try {
"guestId", mNickname);
catch (JSONException e) {
/**
true;
else {
/**
"LocalCameraRender");
false;
else if (btn.getId() == R.id.btn_send_message) {
"");
if (message.equals("")) {
return;
if (mCheckBarrage.isChecked()) {
"", message);
new DanmakuItem(GuestActivity.this, new SpannableString(message), mDanmakuView.getWidth(), 0, R.color.yellow_normol, 18, 1);
else {
"", message);
new ChatMessageBean(mNickname, mNickname, "", message));
else if (btn.getId() == R.id.iv_host_text) {
1);
this, editMessage);
結束語

對於這個基於Anyrtc sdk的直播RTMPCHybirdEngine-Android核心的幾個類基本上介紹完成,我在此僅僅是大概介紹了幾點,可以讓你大概了解這個SDK的直播流程,方便您快速上手。

轉載請注明出處,謝謝!

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved