Jelajahi Sumber

双向互通

liuzhenxing1118 8 bulan lalu
induk
melakukan
0af41e420a

+ 106 - 95
app/src/main/java/com/xplora/xpvideo/activity/MainActivity.java

@@ -6,8 +6,10 @@ import android.Manifest;
 import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.content.pm.PackageManager;
+import android.media.MediaPlayer;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Handler;
 import android.util.Log;
 import android.view.View;
 import android.view.WindowManager;
@@ -22,13 +24,19 @@ import com.juphoon.cloud.JCCallItem;
 import com.juphoon.cloud.JCMediaDevice;
 import com.juphoon.cloud.JCMediaDeviceVideoCanvas;
 import com.xplora.xpvideo.R;
+import com.xplora.xpvideo.manager.DatabaseUtils;
+import com.xplora.xpvideo.manager.MediaPlayerUtils;
 import com.xplora.xpvideo.manager.VideoManager;
 import com.xplora.xpvideo.model.ContactBean;
 import com.xplora.xpvideo.observer.Listener;
+import com.xplora.xpvideo.utils.Constant;
+import com.xplora.xpvideo.utils.Macros;
 import com.xplora.xpvideo.utils.ToolsUtils;
 
 import org.w3c.dom.Text;
 
+import java.io.IOException;
+import java.sql.Time;
 import java.util.List;
 import java.util.Timer;
 import java.util.TimerTask;
@@ -37,6 +45,8 @@ public class MainActivity extends Activity implements Listener.VideoListener {
     public final String TAG = "losion / xpvideo :" + getClass().getSimpleName();
 
     public VideoManager mVideoManager = new VideoManager();
+    public MediaPlayerUtils mMediaPlayerUtils = new MediaPlayerUtils();
+
     private JCMediaDeviceVideoCanvas mLocalCanvas = null;
     private JCMediaDeviceVideoCanvas mRemoteCanvas = null;
 
@@ -52,7 +62,8 @@ public class MainActivity extends Activity implements Listener.VideoListener {
 
     //0:呼入 1:呼出
     private int mVideoType = 0;
-    private String mVideoId = "f471b2a1-0e5e-4796-8949-7a4cfeac5df3";
+    private String mVideoId = "";
+    private String mTicket = "TK_1ONFZ8pNwa";
     private ContactBean mContactBean = new ContactBean();
 
     @Override
@@ -62,7 +73,7 @@ public class MainActivity extends Activity implements Listener.VideoListener {
         requestPermission();
         initView();
         initData();
-        mVideoManager.init(this, "TK_1ONFZ8pNwa");  //TK_QjZ0vC1tBa
+        mVideoManager.init(this, mTicket);
     }
 
     private void initView() {
@@ -88,18 +99,25 @@ public class MainActivity extends Activity implements Listener.VideoListener {
     }
 
     private void initData() {
-        //mVideoType = getIntent().getIntExtra(Constant.EXTRA_VIDEO_TYPE, 0);
-        //mVideoId = getIntent().getStringExtra(Constant.EXTRA_VIDEO_ID);
-        //mContactBean = new DatabaseUtils().loadContact(mVideoId);
+        if (Macros.DEBUG) {
+            mVideoType = 1;
+            mVideoId = "3523b1d1-8543-43d9-92c9-df663a3f147d";
+        } else {
+            mVideoType = getIntent().getIntExtra(Constant.EXTRA_VIDEO_TYPE, 0);
+            mVideoId = getIntent().getStringExtra(Constant.EXTRA_VIDEO_ID);
+        }
+        mContactBean = new DatabaseUtils().loadContact(mVideoId);
 
-        ToolsUtils.setImageView(mIconView, "", 0, R.drawable.default_avatar);
-        mNameText.setText("这显示名字");
+        ToolsUtils.setImageView(mIconView, mContactBean.profilePath, 0, R.drawable.default_avatar);
+        mNameText.setText(mContactBean.name);
         mHintText.setText(mVideoType == 0 ? R.string.invite : R.string.calling);
     }
 
     @Override
     public void onLoginCallback(boolean isOK) {
+        Log.d(TAG, "onLoginCallback: " + isOK);
         if (isOK) {
+            //呼出
             if (mVideoType == 1) {
                 mVideoManager.call(mVideoId);
             }
@@ -109,20 +127,19 @@ public class MainActivity extends Activity implements Listener.VideoListener {
     @Override
     public void onCallItemAdd(JCCallItem item) {
         if (item.getDirection() == JCCall.DIRECTION_IN && item.getVideo()) {
-            // 2. 做出相应的处理,如在界面上显示“振铃中”
+            Log.d(TAG, "onCallItemAdd: " + item.getUserId());
         }
+
+        // 2. 做出相应的处理,如在界面上显示“振铃中”
+        mMediaPlayerUtils.playSound(this, R.raw.video_call, true);
     }
 
     @Override
     public void onCallItemUpdate() {
         List<JCCallItem> callItems = mVideoManager.mCall.getCallItems();
         if (callItems.size() == 0) {
-            stopTimeoutTimer();
-            stopTalkingTimer();
-            removeCanvas();
-            finish();
+
         } else {
-            startTimeoutTimer();
             JCCallItem item = mVideoManager.getActiveCall();
             if (item == null) {
                 return;
@@ -141,37 +158,40 @@ public class MainActivity extends Activity implements Listener.VideoListener {
         int state = activeItem.getState();
         Log.d(TAG, "updateLayout: " + state);
         if (state == JCCall.STATE_INIT || state == JCCall.STATE_PENDING) {
-            if (mLocalCanvas == null && item.getUploadVideoStreamSelf()) {
-                mLocalCanvas = mVideoManager.mMediaDevice.startCameraVideo(JCMediaDevice.RENDER_FULL_SCREEN);
-                if (mLocalCanvas != null) {
-                    mLocalCanvas.getVideoView().setZOrderMediaOverlay(true);
-                    mLocalCanvas.getVideoView().setId(View.generateViewId());
-                    mVideoLayout.addView(mLocalCanvas.getVideoView(), 0);
-                }
-            } else if (mLocalCanvas != null && !item.getUploadVideoStreamSelf()) {
-                mVideoManager.mMediaDevice.stopVideo(mLocalCanvas);
-                mVideoLayout.removeView(mLocalCanvas.getVideoView());
-                mLocalCanvas = null;
-            }
+            updateLayout_pending(item);
         } else if (state == JCCall.STATE_CONNECTING) {
             mHintText.setText(R.string.connecting);
         } else if (state == JCCall.STATE_TALKING) {
+            updateLayout_talking(item);
+        }
+    }
+
+    private void updateLayout_pending(JCCallItem item) {
+        if (mLocalCanvas == null && item.getUploadVideoStreamSelf()) {
+            mLocalCanvas = mVideoManager.mMediaDevice.startCameraVideo(JCMediaDevice.RENDER_FULL_SCREEN);
+            startTimeoutTimer();
             if (mLocalCanvas != null) {
-                mVideoLayout.removeView(mLocalCanvas.getVideoView());
-                mLocalCanvas = null;
+                mLocalCanvas.getVideoView().setZOrderMediaOverlay(true);
+                mLocalCanvas.getVideoView().setId(View.generateViewId());
+                mVideoLayout.addView(mLocalCanvas.getVideoView(), 0);
             }
-            if (mRemoteCanvas == null && item.getUploadVideoStreamOther()) {
-                startTalkingTimer();
-                mVideoManager.mMediaDevice.enableSpeaker(true);
-                mRemoteCanvas = mVideoManager.mMediaDevice.startVideo(item.getRenderId(), JCMediaDevice.RENDER_FULL_CONTENT);
-                if (mRemoteCanvas != null) {
-                    mRemoteCanvas.getVideoView().setId(View.generateViewId());
-                    mVideoLayout.addView(mRemoteCanvas.getVideoView(), 0);
-                }
-            } else if (mRemoteCanvas != null && !item.getUploadVideoStreamOther()) {
-                mVideoManager.mMediaDevice.stopVideo(mRemoteCanvas);
-                mVideoLayout.removeView(mRemoteCanvas.getVideoView());
-                mRemoteCanvas = null;
+        }
+    }
+
+    private void updateLayout_talking(JCCallItem item) {
+        if (mLocalCanvas != null) {
+            mVideoLayout.removeView(mLocalCanvas.getVideoView());
+            mLocalCanvas = null;
+        }
+        if (mRemoteCanvas == null && item.getUploadVideoStreamOther()) {
+            stopTimeoutTimer();
+            startTalkingTimer();
+            mMediaPlayerUtils.stopSound();
+            mVideoManager.mMediaDevice.enableSpeaker(true);
+            mRemoteCanvas = mVideoManager.mMediaDevice.startVideo(item.getRenderId(), JCMediaDevice.RENDER_FULL_CONTENT);
+            if (mRemoteCanvas != null) {
+                mRemoteCanvas.getVideoView().setId(View.generateViewId());
+                mVideoLayout.addView(mRemoteCanvas.getVideoView(), 0);
             }
         }
     }
@@ -191,31 +211,13 @@ public class MainActivity extends Activity implements Listener.VideoListener {
 
     @Override
     public void onCallItemRemove() {
-        finish();
-    }
-
-    private void moveView(View view, Float rawX, Float rawY) {
-        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams)view.getLayoutParams();
-        WindowManager wm = (WindowManager)this.getSystemService(WINDOW_SERVICE);
-        int width = wm.getDefaultDisplay().getWidth();
-        int height = wm.getDefaultDisplay().getHeight();
-        int viewwidth = ToolsUtils.dip2px(this, 128f);
-        int viewheight = ToolsUtils.dip2px(this, 184f);
-        if (rawX < viewwidth / 2) {
-            rawX = (float)(viewwidth / 2 + 8);
-        }
-        if (rawY < viewheight / 2) {
-            rawY = (float)(viewwidth / 2 + 24);
-        }
-        if (rawX > width - viewwidth / 2) {
-            rawX = (float)(width - viewwidth / 2);
-        }
-        if (rawY > height - viewheight / 2) {
-            rawY = (float)(height - viewheight / 2);
-        }
-        params.leftMargin = (int)(rawX - viewwidth / 2);
-        params.topMargin = (int)(rawY - viewheight / 2);
-        view.setLayoutParams(params);
+        mMediaPlayerUtils.playSound(this, R.raw.video_cancel, false);
+        new Handler().postDelayed(new Runnable() {
+            @Override
+            public void run() {
+                finish();
+            }
+        }, 2000);
     }
 
     /*动态申请权限*/
@@ -229,25 +231,8 @@ public class MainActivity extends Activity implements Listener.VideoListener {
         }
     }
 
-    private void initSound() {
-//        //设置音效池的属性,设置音效使用场景,设置音效类型
-//        val audioAttributes = AudioAttributes.Builder()
-//                .setLegacyStreamType(AudioManager.STREAM_MUSIC)
-//                .build()
-//        soundPool = SoundPool.Builder() //设置音效池属性
-//                .setAudioAttributes(audioAttributes) //设置音效类型
-//                .setMaxStreams(10)
-//                .build()
-//        soundMap[1] = soundPool!!.load(this, R.raw.video_call, 1)
-//        soundMap[2] = soundPool!!.load(this, R.raw.video_cancel, 1)
-//        soundPool!!.setOnLoadCompleteListener { soundPool, sampleId, status ->
-//            if (sampleId == soundMap[1] && status == 0) {
-//                soundPool.play(soundMap[1]!!, 0.6f, 0.6f, 1, -1, 1f)
-//            }
-//        }
-    }
-
     private void startTimeoutTimer() {
+        Log.d(TAG, "startTimeoutTimer: ");
         if (mTimeoutTimer != null) {
             stopTimeoutTimer();
         }
@@ -255,12 +240,14 @@ public class MainActivity extends Activity implements Listener.VideoListener {
         mTimeoutTimer.schedule(new TimerTask() {
             @Override
             public void run() {
-
+                stopTimeoutTimer();
+                onTimeoutTimerCallback();
             }
-        }, 1000 );
+        }, 1000*60);
     }
 
     private void stopTimeoutTimer() {
+        Log.d(TAG, "stopTimeoutTimer: ");
         if (mTimeoutTimer != null) {
             mTimeoutTimer.cancel();
             mTimeoutTimer = null;
@@ -268,38 +255,62 @@ public class MainActivity extends Activity implements Listener.VideoListener {
     }
 
     private void startTalkingTimer() {
+        Log.d(TAG, "startTalkingTimer: ");
         if (mTalkingTimer != null) {
             stopTalkingTimer();
         }
+        mTimeCount = 0;
         mTalkingTimer = new Timer();
         mTalkingTimer.schedule(new TimerTask() {
             @Override
             public void run() {
-                refreshTimeLabel();
+                onTalkingTiemrCallback();
             }
-        }, 1000);
+        }, 1000, 1000);
     }
 
     private void stopTalkingTimer() {
+        Log.d(TAG, "stopTalkingTimer: ");
         if (mTalkingTimer != null) {
             mTalkingTimer.cancel();
             mTalkingTimer = null;
         }
+        mTimeCount = 0;
+    }
+
+    private void onTimeoutTimerCallback() {
+        showToast(getString(R.string.timeout));
+        mVideoManager.hangup();
     }
 
     @SuppressLint("DefaultLocale")
-    private void refreshTimeLabel() {
-        long hours = mTimeCount / 3600;
-        mTimeCount %= 3600;
-        long minutes = mTimeCount / 60;
-        long second = mTimeCount %= 60;
+    private void onTalkingTiemrCallback() {
+        mTimeCount ++;
+        Log.d(TAG, "onTalkingTiemrCallback: " + mTimeCount);
+
+        int count = mTimeCount;
+        long hours = count / 3600;
+        count %= 3600;
+        long minutes = count / 60;
+        long second = count %= 60;
         String text = hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, second) : String.format("%02d:%02d", minutes, second);
-        mHintText.setText(text);
+        Log.d(TAG, "onTalkingTiemrCallback: text:" + text);
+        runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                mHintText.setText(text);
+            }
+        });
     }
 
-    private void toast(String content) {
-        Toast t = Toast.makeText(this, content, Toast.LENGTH_SHORT);
-        t.show();
+    private void showToast(String content) {
+        runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                Toast t = Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT);
+                t.show();
+            }
+        });
     }
 
     @Override

+ 40 - 24
app/src/main/java/com/xplora/xpvideo/manager/DatabaseUtils.java

@@ -7,10 +7,14 @@ import android.util.Log;
 
 import com.xplora.xpvideo.VideoApplication;
 import com.xplora.xpvideo.model.ContactBean;
+import com.xplora.xpvideo.utils.Macros;
 import com.xplora.xpvideo.utils.MetaData;
 import com.xplora.xpvideo.utils.ToolsUtils;
 
 import java.util.ArrayList;
+import java.util.Objects;
+
+import javax.crypto.Mac;
 
 public class DatabaseUtils {
     public String TAG = "losion / xpvideo :" + getClass().getSimpleName();
@@ -18,31 +22,43 @@ public class DatabaseUtils {
 
     @SuppressLint("Range")
     public ContactBean loadContact(String userId) {
-        ArrayList<ContactBean> arrayList = new ArrayList<>();
-        Uri uri = Uri.parse(MetaData.DB_CONTACTS + "id" + "/" + userId);
-        Cursor cursor = VideoApplication.getsContext().getContentResolver().query(uri, null, null, null, null);
-        if (cursor == null) {
-            Log.d(TAG, "loadContact: cursor == null");
-            return null;
-        }
-        if (!cursor.moveToFirst()) {
-            Log.d(TAG, "loadContact: cursor.moveToFirst() false");
+        if (Macros.DEBUG) {
+            ContactBean bean = new ContactBean();
+            bean.userId = userId;
+            return bean;
+        } else {
+            Uri uri = Uri.parse(MetaData.DB_CONTACTS);
+            Cursor cursor = VideoApplication.getsContext().getContentResolver().query(uri, null, null, null, null);
+            if (cursor == null) {
+                Log.d(TAG, "loadContact: cursor == null");
+                return null;
+            }
+            if (!cursor.moveToFirst()) {
+                Log.d(TAG, "loadContact: cursor.moveToFirst() false");
+                cursor.close();
+                return null;
+            }
+
+            ContactBean bean = new ContactBean();
+            do {
+                String uId = cursor.getString(cursor.getColumnIndex("userId"));
+                if (uId.equals(userId)) {
+                    Log.d(TAG, "loadContact: find contact");
+                    bean._id = cursor.getLong(cursor.getColumnIndex("_id"));
+                    bean.id = cursor.getString(cursor.getColumnIndex("id"));
+                    bean.userId = cursor.getString(cursor.getColumnIndex("userId"));
+                    bean.name = cursor.getString(cursor.getColumnIndex("name"));
+                    bean.profile = cursor.getString(cursor.getColumnIndex("profile"));
+                    bean.profilePath = cursor.getString(cursor.getColumnIndex("profilePath"));
+                    bean.phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber"));
+                    bean.countryPN = cursor.getString(cursor.getColumnIndex("countryPN"));
+                    bean.type = ToolsUtils.parseInt(cursor.getString(cursor.getColumnIndex("type")));
+                    bean.rate = ToolsUtils.parseInt(cursor.getString(cursor.getColumnIndex("rate")));
+                    bean.unRead = ToolsUtils.parseInt(cursor.getString(cursor.getColumnIndex("unRead")));
+                }
+            } while (cursor.moveToNext());
             cursor.close();
-            return null;
+            return bean;
         }
-        ContactBean bean = new ContactBean();
-        bean._id = cursor.getLong(cursor.getColumnIndex("_id"));
-        bean.id = cursor.getString(cursor.getColumnIndex("id"));
-        bean.userId = cursor.getString(cursor.getColumnIndex("userId"));
-        bean.name = cursor.getString(cursor.getColumnIndex("name"));
-        bean.profile = cursor.getString(cursor.getColumnIndex("profile"));
-        bean.profilePath = cursor.getString(cursor.getColumnIndex("profilePath"));
-        bean.phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber"));
-        bean.countryPN = cursor.getString(cursor.getColumnIndex("countryPN"));
-        bean.type = ToolsUtils.parseInt(cursor.getString(cursor.getColumnIndex("type")));
-        bean.rate = ToolsUtils.parseInt(cursor.getString(cursor.getColumnIndex("rate")));
-        bean.unRead = ToolsUtils.parseInt(cursor.getString(cursor.getColumnIndex("unRead")));
-        cursor.close();
-        return bean;
     }
 }

+ 43 - 0
app/src/main/java/com/xplora/xpvideo/manager/MediaPlayerUtils.java

@@ -0,0 +1,43 @@
+package com.xplora.xpvideo.manager;
+
+import android.content.Context;
+import android.media.MediaPlayer;
+import android.net.Uri;
+
+import java.io.IOException;
+
+public class MediaPlayerUtils {
+
+    private MediaPlayer mediaPlayer;
+
+    public void playSound(Context context, int resourceId, boolean isLoop) {
+        stopSound();
+
+        // 创建MediaPlayer对象
+        mediaPlayer = new MediaPlayer();
+        try {
+            Uri path = Uri.parse("android.resource://" + context.getPackageName() + "/" + resourceId);
+
+            mediaPlayer.reset();
+
+            // 设置音频文件的数据源
+            mediaPlayer.setDataSource(context, path);
+            // 设置循环播放
+            mediaPlayer.setLooping(isLoop);
+            // 准备播放
+            mediaPlayer.prepare();
+            // 开始播放
+            mediaPlayer.start();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public void stopSound() {
+        // 释放资源
+        if (mediaPlayer!= null) {
+            mediaPlayer.release();
+            mediaPlayer = null;
+        }
+    }
+}

+ 6 - 4
app/src/main/java/com/xplora/xpvideo/manager/VideoManager.java

@@ -57,15 +57,16 @@ public class VideoManager implements JCClientCallback, JCCallCallback, JCMediaDe
 
     public void call(String callId) {
         // 发起语音呼叫
-        Log.d(TAG, "call: " + callId);
-        mCall.call(callId, true , new JCCall.CallParam("extraParam", "ticket"));
+        boolean isOK = mCall.call(callId, true , new JCCall.CallParam("extraParam", "ticket"));
+        Log.d(TAG, "call: " + callId + " isOK: " + isOK);
     }
 
     public void answer() {
         Log.d(TAG, "answer: ");
         JCCallItem item = getActiveCall();
         if (mCall != null && item != null) {
-            mCall.answer(item, true);
+            boolean isOK = mCall.answer(item, true);
+            Log.d(TAG, "answer: " + isOK);
         }
     }
 
@@ -73,7 +74,8 @@ public class VideoManager implements JCClientCallback, JCCallCallback, JCMediaDe
         Log.d(TAG, "hangup: ");
         JCCallItem item = getActiveCall();
         if (mCall != null && item != null) {
-            mCall.term(item, JCCall.REASON_NONE, "");
+            boolean isOK = mCall.term(item, JCCall.REASON_NONE, "");
+            Log.d(TAG, "hangup: " + isOK);
         }
     }
 

+ 5 - 0
app/src/main/java/com/xplora/xpvideo/utils/Macros.java

@@ -0,0 +1,5 @@
+package com.xplora.xpvideo.utils;
+
+public class Macros {
+    public static boolean DEBUG = true;
+}

TEMPAT SAMPAH
app/src/main/res/raw/video_call.aac


TEMPAT SAMPAH
app/src/main/res/raw/video_cancel.mp3


+ 1 - 0
app/src/main/res/values/strings.xml

@@ -3,4 +3,5 @@
     <string name="invite">邀请你视频通话</string>
     <string name="calling">正在呼叫中</string>
     <string name="connecting">连接中…</string>
+    <string name="timeout">超时</string>
 </resources>