Android realizes call minimization floating box effect

When you use mainstream video software and live broadcast software, you often see that after opening video minimization, it is not closed directly, but a small window in the lower right corner of the screen. This small series brings you the effect of using Android to realize video or voice calls. Minimization also has the effect of a floating box.

There are many similar articles on the network about the realization of the function of minimizing the audio and video call into a floating frame, but it seems that I haven't seen a clear explanation. Here, because the project needs to realize such a function, I record it today. On the one hand, it is convenient for me to use it in the future, and on the other hand, it also provides an idea for people in need, Let's avoid detours. Here, I also refer to some articles about Android suspension box, combined with my own understanding, the possible implementation method is not the best, but this may also be a feasible scheme.

1、 Achieve effect (GIF effect may not be recorded very well)

2、 Realization idea

In fact, it is not difficult to realize this function. Here I split the implementation idea into two steps: 1. Minimize video call activity. 2. Opening of video call suspension frame

The specific idea is as follows: when the user clicks the minimize button, minimize our video call activity (at this time, the activity is in the background state), remove the original video canvas in the activity (because I use Netease Yunxin, here they can only allow one video canvas to exist, depending on the situation. At the same time, delay for hundreds of milliseconds, Open the suspension box, create a new video canvas, and then dynamically add it to the suspension box to monitor the touch events of the suspension box so that the suspension box can be dragged and moved; Listen to the click event of the hover box. If the user clicks the hover box, remove the new video canvas in the hover box, and then restart our video call activity in the background, and then create a new video canvas and dynamically add it to the activity again. For the method of adding and removing the video canvas, here we need to look at the connected third-party SDK. For example, if the SDK of Netease Yunxin is used, their method is as follows (excerpted from their SDK description document below), that is, to remove the canvas, I just need to pass in null.

1. How is activity minimized?

You may not have heard of activity minimization, but as long as the posture is right, the implementation is very simple, because the activity itself comes with a movetasktoback (Boolean nonroot). If we want to minimize, we only need to call movetasktoback (true) to pass in a true value, but there is a premise, You need to set the startup mode of activity to singleinstance mode, which is done in two steps. (Note: one small point to remember here is that the onrestart () method will be called back when you return from the background to the foreground after the activity is minimized.)

2. How does the suspension frame open?

Here, I write the implementation method of the suspension frame in a service, and associate the opening and closing of the suspension frame with the binding and unbinding of the service service. Opening the service is equivalent to opening our suspension frame, and unbinding the service is equivalent to closing the closed suspension frame, so as to achieve better control effect.

a. First, we declare a service class named floatvideowindowservice:

b. Create a layout file alert for the hover box_ float_ video_ Layout is written according to the requirements. If you just need to display each other's video canvas in a floating box like GIF above, the layout file can be as follows: (the size of the suspension frame here is fixed as 80dp long, 110dp high, and the LinearLayout with ID small_size_preview is mainly a container, which can dynamically add views to it, that is, our video canvas)

c. After the layout is defined, we need to perform some initialization operations on the floating box. The initialization operations are executed in the oncreate () life cycle of the service, because it only needs to be executed once. The initialization here mainly includes setting the basic parameters (position, width and height) of the suspension box, the click event of the suspension box and the touch event (i.e. drag range) of the suspension box. The code comments are clear. Look at the code directly, as shown below:

d. After the hover box is successfully initialized and relevant parameters are set, we need to add the other party's video canvas to the hover box so that we can see the other party's video picture. Similarly, we complete this operation in the oncreate life cycle of service. Here, the video canvas is added by using Netease Yunxin SDK, The specific adding method depends on different SDKs. The code is as follows:

e. We mentioned above that we need to combine the binding and unbinding of the service with the opening and closing of the suspension frame. Therefore, since we have opened the suspension frame in the oncreate() method of the service, we should close the suspension frame in its ondestroy() method. The essence of closing the suspension frame is to remove the relevant views and then clear our video canvas, Execute the following code in the ondestroy () method of the service:

f. There are two binding methods for services: bindservice and startservice. Different binding methods will have different life cycles. It is known that we need to turn off the suspension box when the video call activity finishes. Of course, we should use bind to start the service and let its life cycle follow its initiator, That is, follow the activity lifecycle that opens it.

3、 Complete process

Now let's connect the above to make the idea clearer. Suppose I'm making a video call and click the video minimize button, we should perform the following steps in sequence: (if you pose right, a suspension box should appear now)

When we click the hover box, we can use startactivity (intent) to open our activity again. At this time, the video call activity will call back the onrestart () method. We unbind the hover box service in the onrestart () life cycle and reset the new video canvas to the activity

The above is another function implementation method of Android development shared for you this time. I hope our sorting can help you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>