Skip to content

v3.x_Permission_check

xiaoxiaochupei edited this page May 25, 2017 · 2 revisions

v3.x

没有录音权限时,可能上报如下错误:
KSYVIDEO_AUDIO_START_FAILED -2003

没有摄像头权限时,android版本不同,可能上报如下三个错误中的一种:
KSYVIDEO_OPEN_CAMERA_FAIL -2001
KSYVIDEO_CAMERA_PARAMS_ERROR -2002

出现错误后,SDK内部会主动停止推流,开发者需要再打开相应权限后再重新开始推流

权限检查

建议开发者在推流开始前主动进行权限检查。 特别是Android 6.0之后系统的权限管理机制有了较大的改动,具体可以参考:
https://blog.coding.net/blog/understanding-marshmallow-runtime-permission

Demo中已提供了适应不同Android版本进行权限检测、申请的方法:
Activiy需要实现ActivityCompat.OnRequestPermissionsResultCallback接口:

参考代码如下:

public class CameraActivity extends Activity implements
        ActivityCompat.OnRequestPermissionsResultCallback {

    // ...

    private void startCameraPreviewWithPermCheck() {
        int cameraPerm = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
        int audioPerm = ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
        if (cameraPerm != PackageManager.PERMISSION_GRANTED ||
                audioPerm != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                Log.e(TAG, "No CAMERA or AudioRecord permission, please check");
                Toast.makeText(this, "No CAMERA or AudioRecord permission, please check",
                        Toast.LENGTH_LONG).show();
            } else {
                String[] permissions = {Manifest.permission.CAMERA,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.READ_EXTERNAL_STORAGE};
                ActivityCompat.requestPermissions(this, permissions,
                        PERMISSION_REQUEST_CAMERA_AUDIOREC);
            }
        } else {
            mStreamer.startCameraPreview();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
                                           @NonNull int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CAMERA_AUDIOREC: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    mStreamer.startCameraPreview();
                } else {
                    Log.e(TAG, "No CAMERA or AudioRecord permission");
                    Toast.makeText(this, "No CAMERA or AudioRecord permission",
                            Toast.LENGTH_LONG).show();
                }
                break;
            }
        }
    }

    // ...

}
Clone this wiki locally