---
title: 启动引擎接口
description: "启动指令流引擎，接收并渲染云手机画面。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengcps/boostcph/instrucstreamengine/kunpengcpsinstruction_16_0018.html
sourcePath: /source/zh/kunpengcps/boostcph/instrucstreamengine/kunpengcpsinstruction_16_0018.html
indexId: 9055da39dee12a0e845aa1ca70f967390435bc9b2e09b7209c7db222659261ea85
---
# 启动引擎接口

#### 场景介绍

启动指令流引擎，接收并渲染云手机画面。


#### 前提条件

- 指令流引擎已创建并初始化。
- 逻辑线程已启动。
- SurfaceView控件已初始化。


#### 开发流程

1. 在SurfaceView控件回调中记录宽和高，并启动连接线程。
2. 在连接线程中调用指令流引擎的启动函数。


#### 编码实例

```
public class Activity implements BaseActivity {
    private VmiSurfaceView surfaceView;
    private Boolean newlyCreated = true;
    private int guestWidth = 1080;
    private int guestHeight = 1920;
    DisplayMetrics metric = new DisplayMetrics();

    protected void onCreate(Bundle savedInstanceState) {
        WindowManager wm = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
        wm.getDefaultDisplay().getRealMetrics(metric);
        Log.i(TAG, "width pixels:" + metric.widthPixels + ", height pixels:" + metric.heightPixels
            + ",densityDpi:" + metric.densityDpi);
        guestWidth = metric.widthPixels;
        guestHeight = metric.heightPixels;
        initSurfaceHolderCallback(surfaceView.getHolder());
    }
    private void initSurfaceHolderCallback(SurfaceHolder surfaceHolder) {
       surfaceHolder.addCallback(new SurfaceHolder.Callback() {
           @Override
           public void surfaceCreated(SurfaceHolder surfaceHolder) {
               // 首次创建
               newlyCreated = true;
           }
           @Override
           public void surfaceChanged(SurfaceHolder inSurfaceHolder, int format, 
              int width, int height) {
               if (!newlyCreated) {
                   return;
               }
               newlyCreated = false;
               // 由于启动接口涉及Socket连接，建议放到线程中调用
               threadPool.submit(new ConnectRunable());
           }
           @Override
           public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
               newlyCreated = false;
           }
      });
  }
  class ConnectRunable implements Runnable {
        @Override
        public void run() {
            // 调用指令流引擎启动接口
            final int startResult = engine.start(surfaceView.getHolder().getSurface(),
                guestWidth, guestHeight, metric.densityDpi);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (startResult == InstructionEngine.VMI_CLIENT_START_FAIL) {
                      // 连接失败
                    } else {
                        //  连接成功或者已经连接
                    }
                }
            });
       }
  }
```
