轉(zhuǎn)載請(qǐng)注明出處
作者:JYangkai
出處:簡(jiǎn)書
地址:Android 集成聲網(wǎng)Agora 音視頻實(shí)時(shí)通話(三)
正文
上一章我們介紹了聲網(wǎng)的具體初始化羽杰、加入房間、離開房間等方法乔外,但我還沒介紹具體應(yīng)該用,那么這一章我們就來看看如何使用AgoraManager
新建接口
/**
* 通話時(shí)的接口
*/
public interface OnMediaListener {
void onJoinSuccess(String channel, int uid, int elapsed);
void onRejoinSuccess(String channel, int uid, int elapsed);
void onLeaveChannel(IRtcEngineEventHandler.RtcStats stats);
void onUserJoined(int uid, int elapsed);
void onUserOffline(int uid, int reason);
}
此接口里的方法分別是
- onJoinSuccess():加入房間成功的回調(diào)
- onRejoinSuccess():重新加入房間的回調(diào)
- onLeaveChannel():離開房間的回調(diào)
- onUserJoined():有用戶加入房間的回調(diào)
- onUserOffline():有用戶離開的回調(diào)
其實(shí)還可以寫更多的方法一罩,比如靜音回調(diào)杨幼、關(guān)閉攝像頭回調(diào)等,但這里我們只是實(shí)現(xiàn)簡(jiǎn)單的音視頻通話擒抛,有這些方法就足夠了
我們?cè)趯懥诉@個(gè)接口后應(yīng)該怎么用呢推汽,首先我們要在AgoraManager類內(nèi)部定義此接口的實(shí)例
private OnMediaListener listener;
然后定義一個(gè)方法
/**
* 設(shè)置通話時(shí)的接口回調(diào)
*
* @param listener
*/
public void setOnMedialistener(OnMediaListener listener) {
this.listener = listener;
}
并在RTCEngine的IRtcEngineEventHandler中進(jìn)行調(diào)用
/**
* 回調(diào)接口
*/
private IRtcEngineEventHandler mHandler = new IRtcEngineEventHandler() {
@Override
public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
if (null != listener) {
listener.onJoinSuccess(channel, uid, elapsed);
}
}
@Override
public void onRejoinChannelSuccess(String channel, int uid, int elapsed) {
if (null != listener) {
listener.onRejoinSuccess(channel, uid, elapsed);
}
}
@Override
public void onLeaveChannel(RtcStats stats) {
if (null != listener) {
listener.onLeaveChannel(stats);
}
}
@Override
public void onUserJoined(int uid, int elapsed) {
if (null != listener) {
listener.onUserJoined(uid, elapsed);
}
}
@Override
public void onUserOffline(int uid, int reason) {
if (null != listener) {
listener.onUserOffline(uid, reason);
}
}
};
這樣我們就完成了AgoraManager的所有工作
下面是AgoraManager的完整代碼
AgoraManager.java
package com.yk.jchat.utils;
import android.content.Context;
import android.util.SparseArray;
import android.view.SurfaceView;
import com.yk.jchat.R;
import io.agora.rtc.Constants;
import io.agora.rtc.IRtcEngineEventHandler;
import io.agora.rtc.RtcEngine;
import io.agora.rtc.video.VideoCanvas;
import io.agora.rtc.video.VideoEncoderConfiguration;
public class AgoraManager {
private static AgoraManager instance;
private RtcEngine mRtcEngine;
private SparseArray<SurfaceView> mSurfaceViews;
private int localUid;
private int remoteUid;
private OnMediaListener listener;
private AgoraManager() {
}
public static synchronized AgoraManager getInstance() {
if (null == instance) {
synchronized (AgoraManager.class) {
if (null == instance) {
instance = new AgoraManager();
}
}
}
return instance;
}
/**
* 初始化
*
* @param context
*/
public void init(Context context) {
try {
mRtcEngine = RtcEngine.create(context, context.getResources().getString(R.string.agora_app_id), mHandler);
mRtcEngine.setChannelProfile(Constants.CHANNEL_PROFILE_COMMUNICATION);
mSurfaceViews = new SparseArray<>();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加入房間
*
* @param channelName
* @param uid
*/
public void joinChannel(String channelName, int uid) {
mRtcEngine.joinChannel(null, channelName, null, uid);
}
/**
* 設(shè)置本地視頻
*
* @param context
* @param uid
*/
public void setupLocalVideo(Context context, int uid) {
localUid = uid;
SurfaceView surfaceView = RtcEngine.CreateRendererView(context);
surfaceView.setZOrderMediaOverlay(true);
mRtcEngine.setupLocalVideo(new VideoCanvas(surfaceView, VideoCanvas.RENDER_MODE_FIT, uid));
mSurfaceViews.put(uid, surfaceView);
}
/**
* 設(shè)置遠(yuǎn)端視頻
*
* @param context
* @param uid
*/
public void setupRemoteVideo(Context context, int uid) {
remoteUid = uid;
SurfaceView surfaceView = RtcEngine.CreateRendererView(context);
mRtcEngine.setupRemoteVideo(new VideoCanvas(surfaceView, VideoCanvas.RENDER_MODE_FIT, uid));
mSurfaceViews.put(uid, surfaceView);
}
/**
* 離開房間
*/
public void leaveChannel() {
mRtcEngine.leaveChannel();
}
/**
* 配置適配
*/
public void setVideoConfig() {
mRtcEngine.enableVideo();
VideoEncoderConfiguration.ORIENTATION_MODE orientationMode =
VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_FIXED_PORTRAIT;
VideoEncoderConfiguration.VideoDimensions dimensions =
new VideoEncoderConfiguration.VideoDimensions(360, 640);
VideoEncoderConfiguration.FRAME_RATE frameRate =
VideoEncoderConfiguration.FRAME_RATE.FRAME_RATE_FPS_30;
VideoEncoderConfiguration videoEncoderConfiguration =
new VideoEncoderConfiguration(dimensions, frameRate, VideoEncoderConfiguration.STANDARD_BITRATE, orientationMode);
mRtcEngine.setVideoEncoderConfiguration(videoEncoderConfiguration);
}
/**
* 獲取本地視頻
*
* @return
*/
public SurfaceView getLocalSurfaceView() {
return mSurfaceViews.get(localUid);
}
/**
* 獲取遠(yuǎn)端視頻
*
* @return
*/
public SurfaceView getRemoteSurfaceView() {
return mSurfaceViews.get(remoteUid);
}
/**
* 回調(diào)接口
*/
private IRtcEngineEventHandler mHandler = new IRtcEngineEventHandler() {
@Override
public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
if (null != listener) {
listener.onJoinSuccess(channel, uid, elapsed);
}
}
@Override
public void onRejoinChannelSuccess(String channel, int uid, int elapsed) {
if (null != listener) {
listener.onRejoinSuccess(channel, uid, elapsed);
}
}
@Override
public void onLeaveChannel(RtcStats stats) {
if (null != listener) {
listener.onLeaveChannel(stats);
}
}
@Override
public void onUserJoined(int uid, int elapsed) {
if (null != listener) {
listener.onUserJoined(uid, elapsed);
}
}
@Override
public void onUserOffline(int uid, int reason) {
if (null != listener) {
listener.onUserOffline(uid, reason);
}
}
};
/**
* 通話時(shí)的接口
*/
public interface OnMediaListener {
void onJoinSuccess(String channel, int uid, int elapsed);
void onRejoinSuccess(String channel, int uid, int elapsed);
void onLeaveChannel(IRtcEngineEventHandler.RtcStats stats);
void onUserJoined(int uid, int elapsed);
void onUserOffline(int uid, int reason);
}
/**
* 設(shè)置通話時(shí)的接口回調(diào)
*
* @param listener
*/
public void setOnMedialistener(OnMediaListener listener) {
this.listener = listener;
}
}
接下來我們就去Activity中調(diào)用了
一般步驟:
- Application中初始化AgoraManager
- 給自己設(shè)置一個(gè)Uid,進(jìn)入音視頻通話Activity歧沪,注冊(cè)監(jiān)聽歹撒,并調(diào)用AgoraManager加入房間
- 在收到加入房間成功的回調(diào)后,設(shè)置本地圖像诊胞,并顯示
- 在收到有用戶加入房間的回調(diào)后怎爵,為對(duì)方設(shè)置遠(yuǎn)端圖像,并顯示
- 想要退出房間驯鳖,則調(diào)用AgoraManager的退出房間方法
- 在收到有用戶退出房間的回調(diào)后去枷,將其圖像銷毀
大概流程就是這樣。
今天是我日更的第100天邪码,也算是達(dá)到了一個(gè)小目標(biāo)裕菠,從今天開始,我就要從日更改為不定期更闭专,數(shù)量可能會(huì)少奴潘,但質(zhì)量絕對(duì)不會(huì)差旧烧,期待一個(gè)更成熟的我吧。