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

Starting the recvData Logic Thread

Application Scenario

Start the recvData logic thread in advance and register callback objects of audio and recording data.

Prerequisites

An instruction stream engine object has been created.

Development Process

  1. Encapsulate the logic thread object UpstreamReceiveDispatcher and cyclically call the recvData API of the instruction stream engine.
  2. Register the audio data callback function and recording callback function.
  3. Start the logic thread.

Encoding Instance

public class Activity implements BaseActivity {
   protected void onCreate(Bundle savedInstanceState) {
        upstreamReceiveDispatcher = UpstreamReceiveDispatcher.getInstance();
        upstreamReceiveDispatcher.setInstructionEngine(engine);
        // Call back audio data.
        audioPlayerCallback = new AudioPlayerCallback();
        // Register the audio data callback function.
        upstreamReceiveDispatcher.addNewPacketCallback((byte) InstructionWrapper.AUDIO, audioPlayerCallback);
        // Start the logic thread to receive audio data.
        upstreamReceiveDispatcher.start();
    }
}
public class UpstreamReceiveDispatcher extends Thread {
    @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;
            }
        }
    }
}