开发记录(游戏开黑)



实现核心组件:

1.http://gcloud.qq.com/ 游戏语音vioce

2.阿里云push (用于开黑成员加入 退出监听)



开黑语音核心代码

public class VoiceCore {
    private GCloudVoiceEngine voiceEngine;

    private boolean Going = true;

    private Context context;

    private LoadingDialog startDialog;

    private static VoiceCore _instance;

    public ChannelModel channelModel;

    public static synchronized VoiceCore getInstance(Context context) {
        if (_instance == null) {
            _instance = new VoiceCore(context);
        }
        return _instance;
    }


    private VoiceCore(Context context) {
        this.context = context;
        voiceEngine = GCloudVoiceEngine.getInstance();
        voiceEngine.init(App.getContext(), (Activity) context);
    }

    public void Start(ChannelModel channelModel) {

        if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO)) {
            this.channelModel = channelModel;
            Auth();
        } else {
            //提示用户开户权限音频
            String[] perms = {"android.permission.RECORD_AUDIO"};
            ActivityCompat.requestPermissions((Activity) context, perms, 1);
        }

    }


    private IGCloudVoiceNotify notify = new IGCloudVoiceNotify() {
        @Override
        public void OnJoinRoom(final int code, String s, int i1) {
            Log.e("code", code + " " + s + " " + i1);
            startDialog.cancel();
            if (code == com.tencent.gcloud.voice.IGCloudVoiceNotify.GCloudVoiceCompleteCode.GV_ON_JOINROOM_SUCC) {
                Drive.getInstance(context).JoinRoom(channelModel.getId(), new Drive.BooleanListener() {
                    @Override
                    public void CallBack(boolean state) {
                        if (state) {

                            voiceEngine.OpenMic();
                            voiceEngine.OpenSpeaker();
                            Intent intent = new Intent(context, FloatWindowService.class);
                            context.startService(intent);

                            Drive.getInstance(context).SaveGangUpHistory(channelModel.getId(), channelModel.getAvatar());

                        } else {
                            Show.getInstance(context).MsgShowSmall("提示", "进入房间失败01");

                            Going = false;
                            voiceEngine.QuitRoom(channelModel.getId(), 6000);
                        }
                    }
                });

            } else {
                Show.getInstance(context).MsgShowSmall("提示", "进入房间失败00");
            }

        }

        @Override
        public void OnStatusUpdate(int i, String s, int i1) {

        }

        @Override
        public void OnQuitRoom(int i, String s) {
            Going = false;
            //  startDialog.cancel();
        }

        @Override
        public void OnMemberVoice(int[] ints, int i) {
            Log.e("UserCount", String.valueOf(ints.length) + "  " + String.valueOf(i));

        }

        @Override
        public void OnUploadFile(int i, String s, String s1) {

        }

        @Override
        public void OnDownloadFile(int i, String s, String s1) {

        }

        @Override
        public void OnPlayRecordedFile(int i, String s) {

        }

        @Override
        public void OnApplyMessageKey(int i) {

        }

        @Override
        public void OnSpeechToText(int i, String s, String s1) {

        }

        @Override
        public void OnRecording(String s, int i) {

        }
    };

    private void Auth() {

        if (!UserManager.CheckUid(true)) {
            return;
        }

        voiceEngine.SetAppInfo(Config.GCloudID, Config.GCloudKEY, UserManager.GetUid());
        voiceEngine.Init();
        voiceEngine.SetNotify(notify);
        voiceEngine.SetMode(GCloudVoiceEngine.Mode.RealTime);

        startDialog = new LoadingDialog(context).builder();
        startDialog.setTitle("正在初始化").show();
        Going = true;

        handler.postDelayed(runnable, 3000);
        startDialog.setTitle("正在入房间").show();

        voiceEngine.JoinTeamRoom(channelModel.getId(), 6000);


    }


    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            //要做的事情
            Log.e("voiceCore", "Voide正在监听");

            if (Going) {
                voiceEngine.Poll();
                handler.postDelayed(this, 3000);
            }
        }
    };


    public void Close(CloseListener listener) {
        //startDialog.setTitle("正在退出房间").show();
        voiceEngine.QuitRoom(channelModel.getId(), 6000);
        voiceEngine.CloseMic();
        voiceEngine.CloseSpeaker();

        listener.Back();
    }

    public interface CloseListener {
        void Back();
    }

}


开黑成员监听核心代码

MyMessageReceiver.OnChannelUpdateListener(new MyMessageReceiver.ChannelUpdate() {
    @Override
    public void CallBack(String content) {
        ChannelRefrash();
    }
});

public void ChannelRefrash() {
    OkHttpUtils.post().url(Config.BaseApi)
            .addParams("action", "GetChannelUser")
            .addParams("roomid", VoiceCore.getInstance(context).channelModel.getId())
            .build()
            .execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {

                }

                @Override
                public void onResponse(String response, int id) {

                    Log.e("ChannelRefrash", response);
                    Result
    
    
     
      result = new Gson().fromJson(response, new TypeToken
     
     
      
      
       
       >() {
                    }.getType());
                    if (result.code == 1) {
                        userList.clear();
                        for (int i = 0; i < result.data.size(); i++) {
                            NetBeans.ChannelUser model = result.data.get(i);
                            userList.add(model);
                        }
                        adapter.notifyDataSetChanged();
                    } else {

                    }
                }
            });
}
      
      
     
     
    
    


MyMessageReceiver为阿里云push广播接收器 ,监听广播后执行刷新 成员列表




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值