因为用户的网络环境各不相同,所以推荐您在用户首次通话前先进行一次测速,这将有助于我们选择最佳的服务器。具体实现代码如下:
1.初始化
创建一个 TRTCCloud 的实例对象,并注册监听 SDK 事件的回调
Android:
代码语言:txt复制private TRTCCloud trtcCloud; /// TRTC SDK 实例对象
private TRTCCloudListener trtcListener; /// TRTC SDK 回调监听
// 创建 trtcCloud 实例
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
trtcListener = new TRTCCloudListenerImpl(this);
trtcCloud = TRTCCloud.sharedInstance(this);
trtcCloud.setListener(trtcListener);
}
// 继承 TRTCCloudListener 回调
static class TRTCCloudListenerImpl extends TRTCCloudListener {
private WeakReference<TRTCMainActivity> mContext;
public TRTCCloudListenerImpl(TRTCMainActivity activity) {
super();
mContext = new WeakReference<>(activity);
}
....
// 错误通知是要监听的,错误通知意味着 SDK 不能继续运行了
@Override
public void onError(int errCode, String errMsg, Bundle extraInfo) {
Log.d(TAG, "sdk callback onError");
}
}
2.获取进视频通话房间的参数
获取网络测速的三个必要字段: SDKAppid,userId,userSig
Android:
代码语言:txt复制// 创建 trtcCloud 实例
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
trtcListener = new TRTCCloudListenerImpl(this);
trtcCloud = TRTCCloud.sharedInstance(this);
trtcCloud.setListener(trtcListener);
// TRTC SDK 视频通话房间进入所必须的参数
Intent intent = getIntent();
int sdkAppId = intent.getIntExtra("sdkAppId", 0);
int roomId = intent.getIntExtra("roomId", 0);
String selfUserId = intent.getStringExtra("userId");
String userSig = intent.getStringExtra("userSig");
// 测速
trtcCloud.startSpeedTest(sdkAppId, selfUserId, userSig);
}
3. 监听回调
实现 TRTCCloudListener 回调中的方法 onSpeedTest()
Android:
代码语言:txt复制// 继承 TRTCCloudListener 回调
static class TRTCCloudListenerImpl extends TRTCCloudListener {
private WeakReference<TRTCMainActivity> mContext;
public TRTCCloudListenerImpl(TRTCMainActivity activity) {
super();
mContext = new WeakReference<>(activity);
}
....
// 错误通知是要监听的,错误通知意味着 SDK 不能继续运行了
@Override
public void onError(int errCode, String errMsg, Bundle extraInfo) {
Log.d(TAG, "sdk callback onError");
}
/**
* 服务器测速的回调,SDK 对多个服务器IP做测速,每个IP的测速结果通过这个回调通知
* @param currentResult 当前完成的测速结果
* @param finishedCount 已完成测速的服务器数量
* @param totalCount 需要测速的服务器总数量
*/
public void onSpeedTest(TRTCCloudDef.TRTCSpeedTestResult currentResult, int finishedCount, int totalCount) {
Log.i(TAG, "ip = " currentResult.ip " | quality = " currentResult.quality
" | upLostRate = " currentResult.upLostRate " | upLostRate = " currentResult.upLostRate
" | rtt = " currentResult.rtt " ||| finishedCount = " finishedCount " | totalCount = " totalCount);
}
}
具体细节见文档: 通话前网络测速