update 视频、音频
This commit is contained in:
@@ -2,22 +2,23 @@ package com.shayu.onetoone.manager;
|
||||
|
||||
import android.Manifest;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
import com.blankj.utilcode.util.PermissionUtils;
|
||||
import com.shayu.onetoone.listener.OnCallStatusListener;
|
||||
import com.yunbao.common.CommonAppContext;
|
||||
import com.yunbao.common.manager.IMLoginManager;
|
||||
import com.yunbao.common.utils.ToastUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import io.rong.callkit.util.CallKitUtils;
|
||||
import io.rong.calllib.CallUserProfile;
|
||||
import io.rong.calllib.IRongCallListener;
|
||||
import io.rong.calllib.IRongReceivedCallListener;
|
||||
import io.rong.calllib.RongCallClient;
|
||||
@@ -31,11 +32,16 @@ import io.rong.imlib.RongIMClient;
|
||||
import io.rong.imlib.model.Conversation;
|
||||
|
||||
public class CallClientManager {
|
||||
public static final String VIDEO_RECEIVED_CALL = "receivedCall";
|
||||
public static final String VIDEO_CALL = "call";
|
||||
public static final String VIDEO_RECEIVED_CALL = "receivedCall";//接听
|
||||
public static final String VIDEO_CALL = "call";//拨打
|
||||
public static final String VIDEO_FLOAT = "floatWindow";//浮窗
|
||||
public static final String AUDIO_RECEIVED_CALL = "receivedCall";//接听
|
||||
public static final String AUDIO_CALL = "call";//拨打
|
||||
public static final String AUDIO_FLOAT = "floatWindow";//浮窗
|
||||
private static CallClientManager manager;
|
||||
// private SurfaceView localVideo, remoteVideo;
|
||||
private SurfaceView localVideo, remoteVideo;
|
||||
private List<OnCallStatusListener> listeners;
|
||||
private CallTimeTask timeTask = null;
|
||||
|
||||
public static CallClientManager getManager() {
|
||||
if (manager == null) {
|
||||
@@ -50,26 +56,11 @@ public class CallClientManager {
|
||||
}
|
||||
|
||||
public SurfaceView getLocalVideo() {
|
||||
RongCallSession session = RongCallClient.getInstance().getCallSession();
|
||||
String userId= IMLoginManager.get(CommonAppContext.getTopActivity()).getUserInfo().getId()+"";
|
||||
for (CallUserProfile profile : session.getParticipantProfileList()) {
|
||||
if(profile.getUserId().equals(userId)){
|
||||
return profile.getVideoView();
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
return localVideo;
|
||||
}
|
||||
|
||||
public SurfaceView getRemoteVideo(String id) {
|
||||
RongCallSession session = RongCallClient.getInstance().getCallSession();
|
||||
for (CallUserProfile profile : session.getParticipantProfileList()) {
|
||||
if(profile.getUserId().equals(id)){
|
||||
return profile.getVideoView();
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
public SurfaceView getRemoteVideo() {
|
||||
return remoteVideo;
|
||||
}
|
||||
|
||||
private void init() {
|
||||
@@ -84,6 +75,21 @@ public class CallClientManager {
|
||||
listeners.remove(statusListener);
|
||||
}
|
||||
|
||||
private void startTimer() {
|
||||
if (timeTask != null) {
|
||||
timeTask.cancel();
|
||||
}
|
||||
timeTask = new CallTimeTask();
|
||||
new Timer().schedule(timeTask, 0, 1000);
|
||||
}
|
||||
|
||||
private void endTimer() {
|
||||
if (timeTask != null) {
|
||||
timeTask.cancel();
|
||||
}
|
||||
timeTask = null;
|
||||
}
|
||||
|
||||
public void callVideo(String targetId) {
|
||||
RongCallClient.getInstance().setVoIPCallListener(new CallStatusListener(new OnCallStatusListener() {
|
||||
@Override
|
||||
@@ -91,6 +97,7 @@ public class CallClientManager {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallWait(localVideo);
|
||||
}
|
||||
ToastUtil.show("等待对方接受邀请...");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -98,6 +105,8 @@ public class CallClientManager {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallStart(userId, remoteVideo);
|
||||
}
|
||||
ToastUtil.show("连接成功");
|
||||
startTimer();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -105,6 +114,7 @@ public class CallClientManager {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallEnd();
|
||||
}
|
||||
endTimer();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -119,6 +129,45 @@ public class CallClientManager {
|
||||
RongCallClient.getInstance().startCall(Conversation.ConversationType.PRIVATE, targetId, userIds, null, RongCallCommon.CallMediaType.VIDEO, null);
|
||||
}
|
||||
|
||||
public void callAudio(String targetId) {
|
||||
RongCallClient.getInstance().setVoIPCallListener(new CallStatusListener(new OnCallStatusListener() {
|
||||
@Override
|
||||
public void onCallWait(SurfaceView localVideo) {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallWait(localVideo);
|
||||
}
|
||||
ToastUtil.show("等待对方接受邀请...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCallStart(String userId, SurfaceView remoteVideo) {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallStart(userId, remoteVideo);
|
||||
}
|
||||
ToastUtil.show("连接成功");
|
||||
startTimer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCallEnd() {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallEnd();
|
||||
}
|
||||
endTimer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartFirstFrame() {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onStartFirstFrame();
|
||||
}
|
||||
}
|
||||
}));
|
||||
List<String> userIds = new ArrayList<>();
|
||||
userIds.add(targetId);
|
||||
RongCallClient.getInstance().startCall(Conversation.ConversationType.PRIVATE, targetId, userIds, null, RongCallCommon.CallMediaType.AUDIO, null);
|
||||
}
|
||||
|
||||
public void acceptCall(String callId) {
|
||||
RongCallClient.getInstance().setVoIPCallListener(new CallStatusListener(new OnCallStatusListener() {
|
||||
@Override
|
||||
@@ -133,6 +182,7 @@ public class CallClientManager {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallStart(userId, remoteVideo);
|
||||
}
|
||||
startTimer();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -140,6 +190,7 @@ public class CallClientManager {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onCallEnd();
|
||||
}
|
||||
endTimer();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -162,6 +213,50 @@ public class CallClientManager {
|
||||
return RongCallClient.getInstance() != null && RongCallClient.getInstance().getCallSession() != null;
|
||||
}
|
||||
|
||||
public boolean isCallVideo(RongCallSession callSession) {
|
||||
return callSession.getMediaType().equals(RongCallCommon.CallMediaType.VIDEO);
|
||||
}
|
||||
|
||||
public long getTime(long activeTime) {
|
||||
return activeTime == 0 ? 0 : (System.currentTimeMillis() - activeTime) / 1000;
|
||||
}
|
||||
|
||||
private class CallTimeTask extends TimerTask {
|
||||
Handler handler = new Handler(Looper.getMainLooper());
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
RongCallSession callSession = RongCallClient.getInstance().getCallSession();
|
||||
if (callSession == null) {
|
||||
cancel();
|
||||
timeTask = null;
|
||||
return;
|
||||
}
|
||||
long time = getTime(callSession.getActiveTime());
|
||||
String extra;
|
||||
if (time > 0) {
|
||||
if (time >= 3600) {
|
||||
extra =
|
||||
String.format(
|
||||
Locale.ROOT,
|
||||
"%d:%02d:%02d",
|
||||
time / 3600,
|
||||
(time % 3600) / 60,
|
||||
(time % 60));
|
||||
} else {
|
||||
extra = String.format(Locale.ROOT, "%02d:%02d", (time % 3600) / 60, (time % 60));
|
||||
}
|
||||
handler.post(() -> {
|
||||
for (OnCallStatusListener listener : listeners) {
|
||||
listener.onTime(extra);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static class CallMeListener implements IRongReceivedCallListener {
|
||||
|
||||
@Override
|
||||
@@ -171,7 +266,11 @@ public class CallClientManager {
|
||||
bundle.putString("targetId", callSession.getTargetId());
|
||||
bundle.putString("callId", callSession.getCallId());
|
||||
bundle.putString("sessionId", callSession.getSessionId());
|
||||
RouteManager.forwardActivity(RouteManager.ACTIVITY_CALL_VIDEO, bundle);
|
||||
if (callSession.getMediaType() == RongCallCommon.CallMediaType.VIDEO) {
|
||||
RouteManager.forwardActivity(RouteManager.ACTIVITY_CALL_VIDEO, bundle);
|
||||
} else {
|
||||
RouteManager.forwardActivity(RouteManager.ACTIVITY_CALL_AUDIO, bundle);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -191,7 +290,6 @@ public class CallClientManager {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class CallStatusListener implements IRongCallListener {
|
||||
OnCallStatusListener statusListener;
|
||||
private long time = 0;
|
||||
@@ -220,8 +318,11 @@ public class CallClientManager {
|
||||
*/
|
||||
@Override
|
||||
public void onCallOutgoing(RongCallSession callSession, SurfaceView localVideo) {
|
||||
localVideo.setZOrderOnTop(true);
|
||||
localVideo.setZOrderMediaOverlay(true);
|
||||
if (isCallVideo(callSession)) {
|
||||
localVideo.setZOrderOnTop(true);
|
||||
localVideo.setZOrderMediaOverlay(true);
|
||||
CallClientManager.this.localVideo = localVideo;
|
||||
}
|
||||
statusListener.onCallWait(localVideo);
|
||||
System.out.println("CallStatusListener.onCallOutgoing");
|
||||
}
|
||||
@@ -235,8 +336,11 @@ public class CallClientManager {
|
||||
*/
|
||||
@Override
|
||||
public void onCallConnected(RongCallSession callSession, SurfaceView localVideo) {
|
||||
localVideo.setZOrderOnTop(true);
|
||||
localVideo.setZOrderMediaOverlay(true);
|
||||
if (isCallVideo(callSession)) {
|
||||
localVideo.setZOrderOnTop(true);
|
||||
localVideo.setZOrderMediaOverlay(true);
|
||||
CallClientManager.this.localVideo = localVideo;
|
||||
}
|
||||
statusListener.onCallWait(localVideo);
|
||||
}
|
||||
|
||||
@@ -274,6 +378,7 @@ public class CallClientManager {
|
||||
extra = String.format(Locale.ROOT, "%02d:%02d", (time % 3600) / 60, (time % 60));
|
||||
}
|
||||
}
|
||||
|
||||
if (!TextUtils.isEmpty(senderId)) {
|
||||
CallSTerminateMessage message = new CallSTerminateMessage();
|
||||
message.setReason(reason);
|
||||
@@ -305,6 +410,8 @@ public class CallClientManager {
|
||||
}
|
||||
}
|
||||
statusListener.onCallEnd();
|
||||
CallClientManager.this.remoteVideo = null;
|
||||
CallClientManager.this.localVideo = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -328,17 +435,26 @@ public class CallClientManager {
|
||||
* 如果对端调用{@link RongCallClient#startCall(int, boolean, Conversation.ConversationType, String, List, List, RongCallCommon.CallMediaType, String, StartCameraCallback)} 或
|
||||
* {@link RongCallClient#acceptCall(String, int, boolean, StartCameraCallback)}开始的音视频通话,则可以使用如下设置改变对端视频流的镜像显示:<br />
|
||||
* <pre class="prettyprint">
|
||||
* public void onRemoteUserJoined(String userId, RongCallCommon.CallMediaType mediaType, int userType, SurfaceView remoteVideo) {
|
||||
* if (null != remoteVideo) {
|
||||
* ((RongRTCVideoView) remoteVideo).setMirror( boolean);//观看对方视频流是否镜像处理
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
* public void onRemoteUserJoined(String userId, RongCallCommon.CallMediaType mediaType, int userType, SurfaceView remoteVideo) {
|
||||
* if (null != remoteVideo) {
|
||||
* ((RongRTCVideoView) remoteVideo).setMirror( boolean);//观看对方视频流是否镜像处理
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
@Override
|
||||
public void onRemoteUserJoined(String userId, RongCallCommon.CallMediaType mediaType, int userType, SurfaceView remoteVideo) {
|
||||
if (mediaType == RongCallCommon.CallMediaType.AUDIO) {
|
||||
statusListener.onCallStart(userId, null);
|
||||
return;
|
||||
}
|
||||
if (CallClientManager.this.remoteVideo != null) {
|
||||
statusListener.onCallStart(userId, CallClientManager.this.remoteVideo);
|
||||
return;
|
||||
}
|
||||
remoteVideo.setZOrderOnTop(false);
|
||||
remoteVideo.setZOrderMediaOverlay(false);
|
||||
CallClientManager.this.remoteVideo = remoteVideo;
|
||||
statusListener.onCallStart(userId, remoteVideo);
|
||||
System.out.println("CallStatusListener.onRemoteUserJoined");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user