腾讯云通信音视频通话最小化悬浮小窗
程序员文章站
2022-07-04 22:26:54
基础7.4.9200效果图首先是显示悬浮窗的服务/** * 视频悬浮窗服务 */public class FloatVideoWindowService extends Service { private String currentBigUserId; //浮动布局view private View mFloatingLayout; //容......
基础7.4.9200
首先是显示悬浮窗的服务
/**
* 视频悬浮窗服务
*/
public class FloatVideoWindowService extends Service {
private String currentBigUserId;
//浮动布局view
private View mFloatingLayout;
//容器父布局
private TXCloudVideoView mTXCloudVideoView;
private String TAG = getClass().getSimpleName();
@Override
public void onCreate() {
super.onCreate();
initWindow();//设置悬浮窗基本参数(位置、宽高等)
EventBus.getDefault().register(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(Event<String> event) {
if (event.getCode() == 1000) {
findVideoView(event.getData());
}
}
@Override
public IBinder onBind(Intent intent) {
currentBigUserId = intent.getStringExtra("userId");
initFloating();//悬浮框点击事件的处理
return new MyBinder();
}
public class MyBinder extends Binder {
public FloatVideoWindowService getService() {
return FloatVideoWindowService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
FloatWindow.destroy();
EventBus.getDefault().unregister(this);
if (mFloatingLayout != null) {
// 移除悬浮窗口
mFloatingLayout = null;
Constents.isShowFloatWindow = false;
}
}
/**
* 设置悬浮框基本参数(位置、宽高等)
*/
private void initWindow() {
mFloatingLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.alert_float_video_layout, null);
}
private void initFloating() {
mTXCloudVideoView = mFloatingLayout.findViewById(R.id.float_videoview);
findVideoView(currentBigUserId);
Constents.isShowFloatWindow = true;
mFloatingLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//在这里实现点击重新回到Activity
Intent intent = new Intent(FloatVideoWindowService.this, TRTCVideoCallActivity.class);
intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
FloatWindow
.with(getApplicationContext())
.setView(mFloatingLayout)
.setWidth((int) DensityUtil.dip2px(96)) //设置控件宽高
.setHeight((int) DensityUtil.dip2px(136))
.setMoveType(MoveType.slide,0,0)
.setX(100) //设置控件初始位置
.setY(Screen.height,0.3f)
.setDesktopShow(true) //桌面显示
.setViewStateListener(mViewStateListener) //监听悬浮控件状态改变
// .setPermissionListener(mPermissionListener) //监听权限申请结果
.build();
}
ViewStateListener mViewStateListener = new ViewStateListener() {
@Override
public void onPositionUpdate(int i, int i1) {
if(mFloatingLayout == null){
return;
}
int screenWidth = ScreenUtil.getScreenWidth(getApplicationContext());
if(i == 0){
mFloatingLayout.setBackgroundResource(R.drawable.white_radius_left_10);
} else {
if(i == screenWidth - mFloatingLayout.getWidth()){
mFloatingLayout.setBackgroundResource(R.drawable.white_radius_right_10);
} else {
mFloatingLayout.setBackgroundResource(R.drawable.white_radius_10);
}
}
}
@Override
public void onShow() {
}
@Override
public void onHide() {
}
@Override
public void onDismiss() {
}
@Override
public void onMoveAnimStart() {
}
@Override
public void onMoveAnimEnd() {
}
@Override
public void onBackToDesktop() {
}
};
private void findVideoView(String userId) {
mTXCloudVideoView.removeVideoView();
TRTCVideoLayoutManager mTRTCVideoViewLayout = Constents.mVideoViewLayout;
TRTCVideoLayout lVideoViewLayout = mTRTCVideoViewLayout.findCloudViewView(userId);
TXCloudVideoView mLocalVideoView =lVideoViewLayout.getVideoView();
if (mLocalVideoView == null) {
mLocalVideoView = mTRTCVideoViewLayout.allocCloudVideoView(userId).getVideoView();
}
if (Constents.currentUserID.equals(userId)) {
TXCGLSurfaceView mTXCGLSurfaceView = mLocalVideoView.getGLSurfaceView();
if (mTXCGLSurfaceView != null && mTXCGLSurfaceView.getParent() != null) {
((ViewGroup) mTXCGLSurfaceView.getParent()).removeView(mTXCGLSurfaceView);
mTXCloudVideoView.addVideoView(mTXCGLSurfaceView);
}
} else {
TextureView mTextureView = mLocalVideoView.getVideoView();
if (mTextureView != null && mTextureView.getParent() != null) {
((ViewGroup) mTextureView.getParent()).removeView(mTextureView);
mTXCloudVideoView.addVideoView(mTextureView);
}
}
}
}
布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/small_size_frame_layout"
android:layout_width="96dp"
android:layout_height="136dp"
android:background="@drawable/white_radius_10"
android:orientation="vertical"
android:padding="8dp">
<com.tencent.rtmp.ui.TXCloudVideoView
android:id="@+id/float_videoview"
android:layout_width="80dp"
android:layout_height="120dp"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" />
</FrameLayout>
这里我用了一个悬浮窗的库,比自己写的好用
implementation 'com.github.yhaolpz:FloatWindow:1.0.9'
在TRTCVideoCallActivity视频界面开启悬浮窗
public void startFloat() {
String lastBigUserId = mLayoutManagerTrtc.getLastTRTCLayoutEntity();
isStartService = true;
moveTaskToBack(true);
Constents.mVideoViewLayout = mLayoutManagerTrtc;
Intent intent = new Intent(this, FloatVideoWindowService.class);//开启服务显示悬浮框
intent.putExtra("userId", lastBigUserId);
bindService(intent, mVideoServiceConnection, Context.BIND_AUTO_CREATE);
}
悬浮窗点击返回TRTCVideoCallActivity时需要在onRestart里关闭服务并重新赋值TXCloudVideoView
@Override
protected void onRestart() {
super.onRestart();
if (isStartService) {
isStartService = false;
unbindService(mVideoServiceConnection);
}
String userId = mLayoutManagerTrtc.getLastTRTCLayoutEntity();
TXCloudVideoView txCloudVideoView = mLayoutManagerTrtc.findCloudViewView(userId).getVideoView();
if (txCloudVideoView == null) {
txCloudVideoView = mLayoutManagerTrtc.allocCloudVideoView(userId).getVideoView();
}
if (Constents.currentUserID.equals(userId)) {
TXCGLSurfaceView mTXCGLSurfaceView = txCloudVideoView.getGLSurfaceView();
if (mTXCGLSurfaceView != null && mTXCGLSurfaceView.getParent() != null) {
((ViewGroup) mTXCGLSurfaceView.getParent()).removeView(mTXCGLSurfaceView);
txCloudVideoView.addVideoView(mTXCGLSurfaceView);
}
} else {
TextureView mTextureView = txCloudVideoView.getVideoView();
if (mTextureView != null && mTextureView.getParent() != null) {
((ViewGroup) mTextureView.getParent()).removeView(mTextureView);
txCloudVideoView.addVideoView(mTextureView);
}
}
}
本文地址:https://blog.csdn.net/SolarLove/article/details/107200952
下一篇: VBS脚本病毒原理分析与防范