How to solve the need to watch movies with distant girlfriends? (source code attached)


1、 This is a sad story

I am a programmer, my girlfriend and I are long-distance love, so sad!!!

During a video, she suddenly said, “we haven’t seen a movie together for a long time…”

“Er… It seems to be.” after all, it’s so far away that it’s a little difficult to watch movies together.

Girlfriend “Oh”, we fell into silence.

At the end of the video, I feel a little sad. Yes, I haven’t been with her for a long time.

It suddenly occurred to me that there are many apps on the market that can watch movies together. After looking for it, there are a lot of apps like shimmer. They are very popular. I downloaded one and found it very interesting. It can really solve the demand of watching movies together in different places.

So “pa” for a moment, a flash of inspiration, there is a way to coax your girlfriend to be happy, oh yeah!!! I can implement a demo myself!

As a high-quality programmer, in view of the spirit of professional exploration and the experience in zego i.e. construction technology, it is quite possible to think of implementing a demo with similar functions.

Do what you say. Stay up late for a small case. You get a demo all night. The effects are as follows:

How to solve the need to watch movies with distant girlfriends? (source code attached)

I believe you didn’t come to see me bask in dog food after laying so long in front. So now come with me to see how I realize the function of “watching movies together” that makes my girlfriend happy. The article is a little long, but it is absolutely dry. You can finish it patiently.

2、 Start preparing

Before we start to implement the demo of watching movies together, we need to make some preparations before development:

01 development environment preparation

The first step is to prepare the development environment. You need to ensure that the development environment meets the following requirements:

  • Android studio 2.1 or above.
  • Android SDK 25, Android SDK build tools 25.0.2, Android SDK platform tools 25. X.x or above.
  • Android devices with Android 4.1 or above and support audio and video.
  • The Android device is already connected to the Internet.

02 account registration

After the development environment is set up, we need to register an account on the official website of zego, enter the management console, create applications and obtain appid and appsign for development.

The specific application creation process is detailed in this article: console creation project process.

3、 Introduction to implementation process

01 interface specification

Firstly, the API input and output specification of zego is analyzed. According to the API usage document, the calling interface format is as follows:

How to solve the need to watch movies with distant girlfriends? (source code attached)

Take the creation of zegoexpressengine single column object as the column, and each parameter analyzes its function in detail. Each interface describes its role and usage.

02 project development of watching movies together

Then everything is ready. Follow me to practice and see how to realize the project of watching movies together.

The project is divided into two parts: the server for playing local movies and the client for streaming movies. Each part will be described in detail below.

The project sets the maximum number of users to 3, and the server accounts for one, so the client can only watch movies online at the same time (quietly Mimi, make sure there is no third party between you two).

1. Server – implementation process of playing movie server

  • Step 1: create zegoexpressengine:
/**Define SDK engine objects*/
ZegoExpressEngine engine;  
/**Fill in appid and appsign*/
long appID = ;  /**  Please register on the official website in the format of 123456789l*/
String appSign = ;  /**  64 characters, please register on the official website and obtain the format as "012345677890123456789800123456789012345678900123456789012345678901234567890123"*/
/**Create engine, use test environment, and access common scenarios*/
engine = ZegoExpressEngine.createEngine(appID, appSign, true, ZegoScenario.GENERAL, getApplication(), null);
  • Step 2: enable the custom video capture function:
ZegoCustomVideoCaptureConfig videoCaptureConfig = new ZegoCustomVideoCaptureConfig();
//Select raw_ Data type video frame data
videoCaptureConfig.bufferType = ZegoVideoBufferType.RAW_DATA;  engine.enableCustomVideoCapture(true, videoCaptureConfig, ZegoPublishChannel.MAIN);
  • Step 3: set the custom video capture callback object and implement the corresponding method:
//Use itself as a custom video capture callback object SDK. Setcustomvideocapturehandler (New izegocustomvideocapturehandler()){    
 public void onStart(ZegoPublishChannel channel) {         
 //After receiving the callback, the developer needs to execute the business logic related to starting video acquisition, such as turning on the camera     
  public void onStop(ZegoPublishChannel channel) {         
  //After receiving the callback, the developer needs to execute the business logic related to stopping video acquisition, such as turning off the camera, etc     
  • Step 4: select the movie to play and set the room ID. Then log in to the room and receive a custom video collection callback notification to start collection:

/**Create user * / zegouser user = new zegouser ("user1"); 

/**Start logging into the room * / engine.loginroom ("Room1", user);
mZegoMediaPlayer.setVideoHandler(new IZegoMediaPlayerVideoHandler() {
    public void onVideoFrame(ZegoMediaPlayer zegoMediaPlayer, ByteBuffer[] byteBuffers, int[] ints, ZegoVideoFrameParam zegoVideoFrameParam) {
}, ZegoVideoFrameFormat.RGBA32);
  • Step 5: when the client sends a notification to start playing the movie, start streaming (movie streaming), preload the load movie, and start playing the start ()

/**   Start streaming  */  engine.startPublishingStream("stream1");
mZegoMediaPlayer.loadResource(path, new IZegoMediaPlayerLoadResourceCallback() {
    public void onLoadResourceCallback(int code) {
        if (code == 0) {
            if (callback != null) {
  • Step 6: call the send video frame method sendcustomvideocapturerawdata to provide video frame data to the SDK.
//Transfer the collected data to zego SDK
int totalDataLength = byteBuffers[0].capacity();
if (tempByteBuffer == null || tempByteBuffer.capacity() != totalDataLength) {
    tempByteBuffer = ByteBuffer.allocateDirect(byteBuffers[0].capacity()).put(byteBuffers[0]);
} else {
ZegoSDKManager.getInstance().getStreamService().sendCustomVideoCaptureRawData(tempByteBuffer, tempByteBuffer.capacity(), zegoVideoFrameParam);
  • Step 7: when the client has no audience in the room or the server exits the playing room, the end of streaming will receive a custom video collection callback notice to stop collection.

2. Client – implementation process of streaming movie client

First, the client for playing movies has the following functions:

  • A. B-end audience’s movie streaming rendering, movie playing \ pause.
  • A. Push and pull stream of audience audio and video at end B, flip switch of camera and switch of microphone.
  • A. Sending and receiving of real-time messages in the room of b-end audience.

Next, we will introduce you according to the specific functions mentioned above:

(1) Process introduction for movie playback function

  • The audience at terminal a successfully logs into the room through the roomid set in the background. If the entered roomid does not exist, it will judge that the current terminal A is the first person in the room and exit the room to prompt that the room does not exist; Or if the room is full, the login will fail and prompt that the room is full.
\*    Click the movie play button   Setroomextrainfo() notifies messages

 \*    The background mobile terminal receives the callback of onroomextrainfoupdate() to play the movie

 \*    The audience side will receive the movie streaming and obtain the movie streaming streamid for streaming
  • B end   Enter the playing room by inputting roomid to display the movie streaming:
*Start streaming and set the remote streaming rendering view. The view mode adopts the default mode of SDK, and the whole view is filled with proportional scaling 
*Play as follows_ View is the surfaceview / TextureView / surfacetexture object on the UI interface  
engine.startPlayingStream("stream1", new ZegoCanvas(play_view));
  • AB end viewers can play / pause video media through setroomextrainfo (string roomid, string key, string value, null).
  • AB end viewers can play / pause video media through setroomextrainfo (string roomid, string key, string value, null).

The parameter meaning of setroomextrainfo is as follows:

  1. key: roomInfo
  2. value:  0 、1、2、3
  3. 0: for load start movie
  4. 1: The movie is playing
  5. 2: The movie is paused
  6. 3: The room has been closed

(2) For the process of audio and video push-pull streaming

  • Push and pull streaming through zego audio and video services
  • After entering the room, the audience at end a needs to generate a unique streamid to stream to zego audio and video cloud service, and then start previewing and streaming. After the b-end audience enters the room, they will receive the stream update notification from the SDK, and filter out the b-end audience stream streamid to pull the stream
  • After the b-end audience enters the room, they push the stream to the zego audio and video cloud service. They need to generate their own unique streamid, and then start previewing and pushing the stream. At the same time, the streaming ID of the a-end audience stream is filtered for streaming
  • AB end viewers can control their own functions of flipping camera, turning camera on / off and turning microphone on / off:
//Switch whether front and rear cameras are front cameras; True indicates that the front camera is used; False means to use the rear camera expressengine.usefrontcamera (front)// On / off camera enable whether to turn on the camera; True indicates that the camera is turned on; False means to close the camera expressengine.enablecamera (enable)// Whether to turn on the microphone enable whether to turn on the microphone; True indicates that the microphone is turned on; False means mute (turn off) the microphone expressengine.mutemorphone (! Enable);

Both are related to push-pull flow. Take user a pulling user B’s flow as an example. The flow is as follows:

How to solve the need to watch movies with distant girlfriends? (source code attached)

For more specific implementation process, please refer to the official document audio and video implementation process.

(3) Sending and displaying real-time chat messages for rooms

  • AB end audience can send messages in the room;
  • Call sendbroadcastmessage interface to send broadcast message to b-end audience in the same room, and the length cannot exceed 1024 bytes. Get the message sending result through the onimsendbroadcastmessageresult callback;
//Send a broadcast message. Each user who logs in to the room will receive this message through onimrecvbroadcastmessage callback [the sender will not receive this callback] engine. Sendbroadcastmessage (roomid, MSG, new izegoimsendbroadcastmessagecallback() {/ * * send broadcast message result callback processing * / @ override public void onimsendbroadcastmessageresult (int errorcode, long messageid) {// successful or failed processing of message sending results}  });
  • Implement the onimrecvbroadcastmessage callback in izegoeventhandler. After the sender successfully sends the broadcast message, other users in the same room receive relevant information through this callback, including message content, message ID, sending time and sender information.
/*** receive room barrage message notification * * @ param roomid room ID * @ param messagelist received message list * / public void onimrecvbarragemessage (string roomid, ArrayList < zegobarragemessageinfo > messagelist) {// processing of messages sent by other users}
  • Send the received message to the UI interface.

4、 Effect rendering (with demo source code download)

01 effect drawing of playing movie end

How to solve the need to watch movies with distant girlfriends? (source code attached)

(movie player)

02 effect drawing of audience end

How to solve the need to watch movies with distant girlfriends? (source code attached)

(movie audience side)

How to solve the need to watch movies with distant girlfriends? (source code attached)

(schematic diagram of movie playing)

A demo is attached for everyone to experience. Watching a movie together is divided into two parts. The installation password is 1:

1) Server Demo:

2) Client Demo:

The installation method is as follows:

1) First start the server demo, select the movie and set the room ID;

2) The client demo enters the movie room by entering the set room ID.

Source code download:\_ B\_ Mq4wk4bzqt90xbrcq (password: qrep)

5、 Conclusion

Newton once said: I see far because I stand on the shoulders of giants!

Based on the audio and video function of zego technology, a demo of watching movies together can be developed simply after some operation. Generally speaking, it is still very simple and can be realized.

The way to make long-distance girlfriend happy is close at hand. Hurry up and experience the fun of audio and video development!

Scan the QR code in the drawing for more information

How to solve the need to watch movies with distant girlfriends? (source code attached)