我要评分
获取效率
正确性
完整性
易理解

Starting the recvData Logic Thread

Application Scenario

Start the logic thread for receiving data in advance and register the audio callback object.

Prerequisites

A video stream engine object has been created.

Development Process

  1. Encapsulate the logic thread object UpstreamReceiveDispatcher and cyclically call the recvData interface function of the video stream engine.
  2. Registers the audio data callback function.
  3. Start the logic thread.

Encoding Instance

public class Activity implements BaseActivity {
   protected void onCreate(Bundle savedInstanceState) {
        upstreamReceiveDispatcher = UpstreamReceiveDispatcher.getInstance();
        upstreamReceiveDispatcher.setVideoEngine(engine);
        // Audio data callback
        audioPlayerCallback = new AudioPlayerCallback();
        // Register the audio data callback function.
        upstreamReceiveDispatcher.addNewPacketCallback((byte) VideoWrapper.AUDIO, audioPlayerCallback);
        // Start the logic thread to receive audio data.
        upstreamReceiveDispatcher.start();
    }
}
public class UpstreamReceiveDispatcher {
    @Override
    public void run() {
        byte[] recvBuf = new byte[MAX_BUF_LEN];
        while (!stopFlag) {
            // Call recvData cyclically to receive data.
            int packetLen = engine.recvData(mtype, recvBuf, recvBuf.length);
            if (packetLen > 0) {
                byte[] copyData = new byte[packetLen];
                System.arraycopy(recvBuf, 0, copyData, 0, packetLen);
                // Call back the data to the hook function.
                callback.onNewPacket(copyData);
                continue;
            }
        }
    }
}