zoukankan      html  css  js  c++  java
  • 怎么和小伙伴在语音交友APP源码中语音连麦,你造吗?

    语音交友APP源码通过语音连麦技术的实现,优化了用户间的社交方式,不仅不再局限于没有感情的文字和图片,而且也不会收到颜值的印象,通过声音的传递,表达最真实、最诚挚的情感。怎么和小伙伴在语音交友APP源码中实现语音连麦,你造吗?

    1.注册登录

    我这边使用的杨振杰封装的HTTP:

    implementation 'com.yanzhenjie:okalle:0.1.7'                                       
    
    

    - 注册

    public void signUp(){
    	JSONObject params = new JSONObject();
    	try {
    		params.put("sex",0);
    		params.put("userName", Build.MODEL);
    	} catch (JSONException e) {
    		e.printStackTrace();
    	}
    	Kalle.post(String.format(Constants.SERVER,"user/signUp"))
    			.addHeader(Constants.CONTENT_TYPE,"application/json")
    			.body(new JsonBody(params.toString()))
    			.perform(new SimpleCallback<String>() {
    				@Override
    				public void onResponse(SimpleResponse<String, String> response) {
    					Log.i(TAG, "onResponse:signUp  response ="+response.succeed());
    					if (response.succeed().contains("success")){
    						signUpBean =gson.fromJson(response.succeed(),SignUpBean.class);
    						if (signUpBean !=null){
    							String uid =signUpBean.getData().getUid();
    							SpUtil.putString(Constants.UID,uid);
    							signIn(uid);
    						}
    					}
    				}
        
    				@Override
    				public void onException(Exception e) {
    					super.onException(e);
    					e.printStackTrace();
    				}
    			});
    }
    

    - 登录

    public void signIn(String uid){
    	JSONObject params = new JSONObject();
    	try {
    		params.put("cType",1);
    		params.put("pkg",ARApplication.the().getApplicationContext().getPackageName());
    		params.put("uid",uid);
    	} catch (JSONException e) {
    		e.printStackTrace();
    	}
    	Kalle.post(String.format(Constants.SERVER,"user/signIn"))
    			.addHeader(Constants.CONTENT_TYPE,"application/json")
    			.body(new JsonBody(params.toString()))
    			.perform(new SimpleCallback<String>() {
    				@Override
    				public void onResponse(SimpleResponse<String, String> response) {
    					Log.i(TAG, "onResponse: signIn success ="+response.succeed());
    					if (response.succeed().contains("success")){
    						signInBean =gson.fromJson(response.succeed(),SignInBean.class);
    						getRoomList(signInBean);
    						if (loginListener !=null){
    							loginListener.loginSuccess(signInBean);
    						}
    					}
    				}
        
    				@Override
    				public void onException(Exception e) {
    					super.onException(e);
    					e.printStackTrace();
    				}
    			});
    }
    
    

    2.主播在语音交友APP源码中创建和获取房间

    - 创建房间

    a.首先需要初始化Rtc和Rtm实例

    //创建RTC实例,appid 可以去anyRTC官网注册
    mRtcEngine =RtcEngine.create(mContext, appId,mEventHandler);
    
    //创建RTM实例,appid同上
    mRtmClient =RtmClient.createInstance(mContext, appId,mClientListener);
    
    

    b.然后登录AR RTM

    public void login(String rtmToken,String userId, final ResultCallback<Void> callback){
    	if (mRtmClient !=null){
    		if (mIsLogin) {
    			if (callback != null)
    				callback.onSuccess(null);
    			return;
    		}
    		mRtmClient.login(rtmToken, String.valueOf(userId), new ResultCallback<Void>() {
    			@Override
    			public void onSuccess(Void aVoid) {
    				Log.d(TAG, "rtm login success");
    				mIsLogin = true;
    				if (callback != null)
    					callback.onSuccess(aVoid);
    			}
    
    			@Override
    			public void onFailure(ErrorInfo errorInfo) {
    				Log.e(TAG, String.format("rtm join %s", errorInfo.getErrorDescription()));
    				mIsLogin = false;
    				if (callback != null)
    					callback.onFailure(errorInfo);
    			}
    		});
    	}
    }
    

    c.然后主播加入语音交友APP源码的房间

    加入RTC和RTM频道

    RTC:音视频通话

    RTM:实时消息

    //加入RTM频道,完成主播和游客实时消息的发送
    public void joinChannel(final String channelId, ResultCallback<Void> callback) {
    	if (mRtmClient != null) {
    		leaveChannel();
    		Log.w(TAG, String.format("joinChannel %s", channelId));
    		try {
    			final RtmChannel rtmChannel = mRtmClient.createChannel(channelId, mChannelListener);
    			if (rtmChannel == null) return;
    			rtmChannel.join(new ResultCallback<Void>() {
    				@Override
    				public void onSuccess(Void aVoid) {
    					Log.d(TAG, "rtm join success");
    					mRtmChannel = rtmChannel;
    					getMembers();
    				}
    
    				@Override
    				public void onFailure(ErrorInfo errorInfo) {
    					Log.e(TAG, String.format("rtm join %s", errorInfo.getErrorDescription()));
    					//AlertUtil.showToast("RTM login failed, see the log to get more info");
    					mRtmChannel = rtmChannel;
    				}
    			});
    		} catch (Exception ex) {
    			ex.printStackTrace();
    		}
    	}
    }
    
    //加入RTC频道,实现主播和游客语音连麦
    public void joinChannel(String rtcToken,String channelId, String userId) {
    	if (mRtcEngine != null){
    		mRtcEngine.setClientRole(Constants.CLIENT_ROLE_BROADCASTER);
    		mRtcEngine.joinChannel(rtcToken, channelId, null, userId);
    		mRtcEngine.muteLocalAudioStream(false);
    		//mRtcEngine.setEnableSpeakerphone(true);
    		createStreamingInstance();
    	}
    }
    
    

    - 获取语音交友APP源码中的房间列表

    //获取房间列表
    public void getRoomList(SignInBean signInBean){
    	JSONObject params = new JSONObject();
    	try {
    		params.put("count", Integer.MAX_VALUE);
    		params.put("rType", Constants.R_TYPE_LIVE);
    	} catch (JSONException e) {
    		e.printStackTrace();
    	}
    	Kalle.post(String.format(Constants.SERVER,"user/getRoomList"))
    			.addHeader(Constants.CONTENT_TYPE,"application/json")
    			.addHeader(Constants.AUTHORIZATION,"Bearer "+signInBean.getData().getUserToken())
    			.body(new JsonBody(params.toString()))
    			.perform(new SimpleCallback<String>() {
    				@Override
    				public void onResponse(SimpleResponse<String, String> response) {
    					Log.i(TAG, "onResponse: getRoomList ="+response.succeed());
    					if (response.succeed().contains("success")){
    						roomListBean =gson.fromJson(response.succeed(), RoomListBean.class);
    						if (roomListener !=null){
    							roomListener.showRoomList(roomListBean);
    						}
    					}
    				}
        
    				@Override
    				public void onException(Exception e) {
    					super.onException(e);
    					e.printStackTrace();
    					Log.i(TAG, "onException: ===> ="+e.getMessage());
    				}
    			});
    }
    
    

    注:上面展示语音连麦的部分功能,在语音交友APP源码中创建房间的是主播,下拉刷新出来加入房间的是游客

    3.游客加入语音交友APP源码中的房间

    加入房间第一步就是加入RTM频道,然后申请上麦后,主播同意后,再加入RTC频道,进行语音通话。

    - 加入房间

    //调用服务接口,加入房间,接入有人进入房间
    public void joinRoom(String roomId,String roomName,String pullRtmpUrl){
    	JSONObject params = new JSONObject();
    	try {
    		params.put("cType",1);
    		params.put("pkg",ARApplication.the().getApplicationContext().getPackageName());
    		params.put("roomId",roomId);
    	} catch (JSONException e) {
    		e.printStackTrace();
    	}
    	Kalle.post(String.format(Constants.SERVER,"user/joinRoom"))
    			.addHeader(Constants.CONTENT_TYPE,"application/json")
    			.addHeader(Constants.AUTHORIZATION,"Bearer "+signInBean.getData().getUserToken())
    			.body(new JsonBody(params.toString()))
    			.perform(new SimpleCallback<String>() {
    				@Override
    				public void onResponse(SimpleResponse<String, String> response) {
    					Log.i(TAG, "onResponse: joinRoom data ="+response.succeed());
    					if (response.succeed().contains("success")){
    						joinRoomBean =gson.fromJson(response.succeed(),JoinRoomBean.class);
    						if (roomListener !=null){
    							roomListener.joinRoom(joinRoomBean,roomId,roomName,pullRtmpUrl);
    						}
    					}
    				}
        
    				@Override
    				public void onException(Exception e) {
    					super.onException(e);
    					e.printStackTrace();
    					Log.i(TAG, "onException: e="+e.getMessage());
    				}
    			});
    }
    

    然后进行RTM加入房间,和上面主播加入语音交友APP源码的房间一样。

    - 申请上麦流程

    a.游客点击上麦,给主播发一条RTM消息,通知主播“我要上麦”

    //点击按钮事件,进行发送消息
    try {
    	jsonObject.put("cmd","apply");
    } catch (JSONException e) {
    	e.printStackTrace();
    }
    mChatRoomManager.getRtmManager().sendMessageToPeer(mAnchorId,jsonObject.toString(),null);
    
    //发送P to P消息
    public void sendMessageToPeer(final String userId, final String content, final ResultCallback<Void> callback) {
    	if (TextUtils.isEmpty(userId)) return;
    
    	if (mRtmClient != null) {
    		RtmMessage message = mRtmClient.createMessage(content);
    		mRtmClient.sendMessageToPeer(userId, message, null, new ResultCallback<Void>() {
    			@Override
    			public void onSuccess(Void aVoid) {
    				Log.d(TAG, String.format("sendMessageToPeer %s %s", userId, content));
    
    				if (callback != null)
    					callback.onSuccess(aVoid);
    			}
    
    			@Override
    			public void onFailure(ErrorInfo errorInfo) {
    				Log.e(TAG, String.format("sendMessageToPeer %s", errorInfo.getErrorDescription()));
    
    				if (callback != null)
    					callback.onFailure(errorInfo);
    			}
    		});
    	}
    }
    

    b.在语音交友APP源码的房间内主播收到消息,然后弹出对话框,然后进行:确定上麦、拒绝上麦的操作!

    //主播收到消息,进行的逻辑操作!
    applyDialog =new ApplyDialog(this, "连麦申请", mChannelData.getName(userId) + "申请连麦",
    		"拒绝", "确定", () -> {
    			try {
    				object.put("cmd",Constants.MIC_REJECT_LINE);
    			} catch (JSONException e) {
    				e.printStackTrace();
    			}
    			mChatRoomManager.getRtmManager().sendMessageToPeer(userId,object.toString(),null);
    		}, () -> {
    	try {
    		object.put("cmd",Constants.MIC_ACCEPT_LINE);
    	} catch (JSONException e) {
    		e.printStackTrace();
    	}
    	mChatRoomManager.getRtmManager().sendMessageToPeer(userId,object.toString(),null);
    });
    
    applyDialog.show();
    

    c.主播同意上麦后,游客加入RTC频道,进行连麦。

    //游客收到同意上麦的操作逻辑
    micState = MIC_STATE_DOWN;
    isJoinMic =true;
    autoTipDialog =new AutoTipDialog(this,R.drawable.success_tip,"同意上麦");
    autoTipDialog.show();
    mJoinMic.setImageResource(R.drawable.down_mic);
    String rtcToken = Optional.ofNullable(serverManager)
    		.map(ARServerManager::getJoinRoomBean)
    		.map(JoinRoomBean::getData)
    		.map(JoinRoomBean.DataBean::getRoom)
    		.map(JoinRoomBean.DataBean.RoomBeans::getRtcToken)
    		.orElse("");
    Log.i(TAG, "onAcceptLineUpdated: rtcToken ="+rtcToken);
    
    mChatRoomManager.joinRtcChannel(rtcToken,roomId); //j加入RTC频道
    
    mediaPlayerKit.mute(true);
    if (isMic){
    	mChatMic.setImageResource(R.drawable.mic_close);
    }else {
    	mChatMic.setImageResource(R.drawable.mic_open);
    }
    mChatRoomManager.getRtcManager().muteLocalAudioStream(isMic);
    if (isSpeaker){
    	mSpeaker.setImageResource(R.drawable.speaker_close);
    }else {
    	mSpeaker.setImageResource(R.drawable.speaker_open);
    }
    mChatRoomManager.getRtcManager().getRtcEngine().setEnableSpeakerphone(!isSpeaker);
    rlMusic.setVisibility(View.VISIBLE);
    mChatMic.setVisibility(View.VISIBLE);
    mSpeaker.setVisibility(View.GONE);
    mFrameLayout.setVisibility(View.VISIBLE);
    rvGuestLog.setVisibility(View.GONE);
    mLogList.add(new LogBean(R.drawable.circle,getDate(),"上麦成功"));
    

    这个时候就可以和你的小伙伴在语音交友APP源码中进行语音操作啦!最后释放RTC和RTM资源,主播退出删除房间,游客退出房间就可以啦!
    以上就是“怎么和小伙伴在语音交友APP源码中语音连麦,你造吗?”的全部内容,希望对大家有帮助,在语音交友APP源码的开发过程中并不是每一个环节都需要开发者亲自开发,可以借助技术更为成熟的三方服务,这样不仅省时省力,而且还能达到更好的使用效果。

    本文转载自网络,转载仅为分享干货知识,如有侵权欢迎联系云豹科技进行删除处理
    原文接连:https://blog.51cto.com/u_15232255/2825501

  • 相关阅读:
    (树的直径)第九届湘潭市大学生程序设计比赛 H-Highway
    (记忆化DFS)Codeforces Round #413 D-Field expansion
    (树状数组)Codeforces Round #413 C-Fountains
    (几何)LeetCode Weekly Contest 32 D-Erect the Fence
    LeetCode Weekly Contest 32 解题报告
    (贪心)华师大程序设计竞赛 F-丽娃河的狼人传说
    (最短路)AtCoder Beginner Contest 061 D
    Tinkoff Challenge
    Codeforces Round #410 (Div. 2) 解题报告
    (二叉树)UVA
  • 原文地址:https://www.cnblogs.com/yunbao/p/15162350.html
Copyright © 2011-2022 走看看