最近項目中有需要語音、視頻通話需求,看到這個像環信、融云等SDK都有具體Demo實現,但咋的領導對騰訊情有獨鐘啊,IM要用騰訊云IM,不妙的是騰訊云IM并不包含有音視頻通話都要自己實現,沒辦法深入了解騰訊云產品后,決定自己基于騰訊云實時音視頻做去語音、視頻通話功能。在這里把實現過程記錄下為以后用到便于查閱,另一方面也給有需要的人提供一個思路,讓大家少走彎路,有可能我的實現的方法不是最好,但是這或許是一個可行的方案,大家不喜勿噴。基于騰訊云實時音視頻SDK 6.5.7272版本,騰訊DEMO下載地址:鏈接: https://pan.baidu.com/s/1iJsVO3KBuhEiIUZcJPyv3g 提取碼: ueey
一、實現效果
二、實現思路
我把實現思路拆分為了兩步:1、視頻通話Activity的最小化。 2、視頻通話懸浮框的開啟
具體思路是這樣的:當用戶點擊左上角最小化按鈕的時候,最小化視頻通話Activity(這時Activity處于后臺狀態),于此同時開啟懸浮框,新建一個新的ViewGroup將全局Constents.mVideoViewLayout中用戶選中的最大View動態添加到懸浮框里面去,監聽懸浮框的觸摸事件,讓懸浮框可以拖拽移動;自定義點擊事件,如果用戶點擊了懸浮框,則移除懸浮框然后重新調起我們在后臺的視頻通話Activity。
1.Activity是如何實現最小化的?
Activity本身自帶了一個moveTaskToBack(boolean nonRoot),我們要實現最小化只需要調用moveTaskToBack(true)傳入一個true值就可以了,但是這里有一個前提,就是需要設置Activity的啟動模式為singleInstance模式,兩步搞定。(注:activity最小化后重新從后臺回到前臺會回調onRestart()方法)
@Override public boolean moveTaskToBack(boolean nonRoot) { return super.moveTaskToBack(nonRoot); }
2.懸浮框是如何開啟的?
懸浮框的實現方法最好寫在Service里面,將懸浮框的開啟關閉與服務Service的綁定解綁所關聯起來,開啟服務即相當于開啟我們的懸浮框,解綁服務則相當于關閉關閉的懸浮框,以此來達到更好的控制效果。
a. 首先我們聲明一個服務類,取名為FloatVideoWindowService:
public class FloatVideoWindowService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return new MyBinder(); } public class MyBinder extends Binder { public FloatVideoWindowService getService() { return FloatVideoWindowService.this; } } @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); }}
b. 為懸浮框建立一個布局文件float_video_window_layout,懸浮框大小我這里固定為長80dp,高120dp,id為small_size_preview的RelativeLayout主要是一個容器,可以動態的添加view到里面去
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:andro android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorComBg" android:orientation="vertical"> <com.tencent.rtmp.ui.TXCloudVideoView android: android:layout_width="80dp" android:layout_height="120dp" android:descendantFocusability="blocksDescendants" android:orientation="vertical" /> </LinearLayout>
新聞熱點
疑難解答
圖片精選