在线教育平台开发过程中,有些功能并不是通过公司一手打造的,而是通过接入第三方实现的,因为相比于重新开发,接入第三方反而更加保险和稳定,接下来,小编将以声网sdk接入为例,来说明在线教育平台中的小班课是如何实现的。

1.定义接口类别
public interface ILivePresenter  {
    /*切换角色*/
    public void changeRole(int role);
    /*是否禁音*/
    public void muteAudio(boolean isMute);
    /*是否禁视频*/
    public void muteVideo(boolean openVideo);
    /*获取当前直播状态*/
    public LiveState getLiveState();
    /*离开课堂*/
    public void leaveRoom();
    /*进入课堂*/
    public void joinRoom(String roomId);
    /*销毁课堂*/
    public void destroy();
    /*更新视图*/
    public void updateSuface(String uid);
    /*设置远程内容更新*/
    public void setRemoteUserStateChangeListner(IRemoteUserStateChangeListner changeListner);

}

2.利用声网sdk实现接口

public class AgoraLivePresenter  implements ILivePresenter {

    private ILiveView<SurfaceView> mILiveView;
    private RtcEngine mRtcEngine;
    private IRtcEngineEventHandler mIRtcEngineEventHandler;
    private int mUid;
    private LiveState mLiveState;
    private LinkedHashSet<Integer>mprePareUserList;

    private CurrentHandler mCurrentHandler=new CurrentHandler();
    private IRemoteUserStateChangeListner mIRemoteUserStateChangeListner;

    public AgoraLivePresenter(@Nullable ILiveView liveView,String appId,int id ) {
        this.mILiveView=liveView;
        mprePareUserList=new LinkedHashSet<>();
        mUid=id;
        mLiveState=new LiveState();
        try {
            Context context= CommonAppContext.sInstance;
            mIRtcEngineEventHandler=new EngineEventHandler(this);
            mRtcEngine = RtcEngine.create(context,appId, mIRtcEngineEventHandler);
            mRtcEngine.setChannelProfile(io.agora.rtc.Constants.CHANNEL_PROFILE_LIVE_BROADCASTING);
            mRtcEngine.setParameters("{\"che.audio.live_for_comm\":true}");
            mRtcEngine.setLogFile(AFileUtil.initializeLogFile(context));
            mRtcEngine.setChannelProfile(Constants.CHANNEL_PROFILE_LIVE_BROADCASTING);
            mRtcEngine.setBeautyEffectOptions(true, new BeautyOptions(LIGHTENING_CONTRAST_NORMAL, 0.5F, 0.5F, 0.5F));
            setupVideoConfig();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } 
………

3.加入房间:

public void joinRoom(String roomId){
    mAgoraLivePresenter.joinRoom(roomId);
}

4.监听直播房间内视频流的变化,即时添加视图:

@Override
public void onFirstRemoteVideoDecoded(final int uid, final int width,final int height, int elapsed)
mAgoraLivePresenter.mCurrentHandler.post(new Runnable() {
    @Override
    public void run() {
        if(mAgoraLivePresenter!=null){
          mAgoraLivePresenter.readyVideoRemote(uid,width,height);
        }
    }
});

5.退出直播课堂,及时销毁相关资源避免内存泄漏:

if (mRtcEngine != null) {
    mRtcEngine.leaveChannel();
    mRtcEngine.setupLocalVideo(null);
    mRtcEngine.setupRemoteVideo(null);
}
Observable.just(true).observeOn(Schedulers.computation()).subscribe(new DefaultObserver<Boolean>() {
    @Override
    public void onNext(Boolean aBoolean) {
        SystemUtil.logCurrentThread();
        RtcEngine.destroy();
    }
});

以上在线教育平台开发中,如何利用声网sdk实现小班课。