首页 > 系统 > Android > 正文

Android基于腾讯云实时音视频仿微信视频通话最小化悬浮

2020-07-28 14:12:24
字体:
来源:转载
供稿:网友

最近项目中有需要语音、视频通话需求,看到这个像环信、融云等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>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表