尝试使用wowza在空对象引用android studio上调用虚拟方法

时间:2018-10-04 06:58:53

标签: java android nullpointerexception wowza

我正在尝试使用wowza开始广播。我正在使用一个示例应用程序,当我进行编译时,它可以完美运行。

但是,当我尝试在自己的项目中使用它时,尝试开始广播时会出现此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.caper.teststream, PID: 26004
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.wowza.gocoder.sdk.api.broadcast.WOWZBroadcastConfig.setStreamName(java.lang.String)' on a null object reference
    at com.caper.teststream.WowzaCamera.start(WowzaCamera.java:80)
    at com.caper.teststream.BroadcastActivity.onClick(BroadcastActivity.java:59)
    at android.view.View.performClick(View.java:6891)
    at android.widget.TextView.performClick(TextView.java:12651)
    at android.view.View$PerformClick.run(View.java:26083)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

相机也不会显示在预览中

这是我的代码

public class BroadcastActivity extends AppCompatActivity
    implements  View.OnClickListener {

WOWZCameraView mWowzaPreview;
WowzaCamera mWowza = null;
boolean mIsBroadcastStarted = false;

private boolean mPermissionsGranted = true;
private static int mPermissionCount = 0;
private String[] mRequiredPermissions = new String[]{
        Manifest.permission.CAMERA,
        Manifest.permission.RECORD_AUDIO,
        Manifest.permission.READ_EXTERNAL_STORAGE
};
private static final int PERMISSIONS_REQUEST_WOWZA = 367;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_broadcast);

    // Don't turn off screen while in this activity
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    mWowzaPreview =  findViewById(R.id.camera_preview);

    Button broadcastBtn = findViewById(R.id.broadcast_button);
    broadcastBtn.setOnClickListener(this);


}


@Override
public void onClick(View view) {
    Button btn = (Button)view;

    if(!mIsBroadcastStarted)
    {

        // Name of stream is assigned here
        mWowza.start("myStream2");
        mIsBroadcastStarted = true;
        btn.setText("STOP");
    } else {
        mWowza.stop();
        btn.setText("START");
        mIsBroadcastStarted = false;
    }



}

@Override
protected void onResume() {
    super.onResume();

    //Check appropriate permissions
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        mPermissionsGranted = hasPermissions(this, mRequiredPermissions);
        if (!mPermissionsGranted)
            ActivityCompat.requestPermissions(this, mRequiredPermissions, PERMISSIONS_REQUEST_WOWZA);
        else {
            initializeWowzaCamera();
        }
    } else {
        mPermissionsGranted = true;
        initializeWowzaCamera();
    }
}

private boolean isInit = false;

private void initializeWowzaCamera(){
    if (!isInit) {
        mWowza = new WowzaCamera(BroadcastActivity.this, mWowzaPreview);
        isInit = true;

    }
    if (mWowza != null) {
        mWowza.onResume();
        mWowzaPreview.onResume();

    }
}

private static boolean hasPermissions(Context context, String[] permissions) {
    for (String permission : permissions)
        if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)
            return false;

    return true;
}


@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSIONS_REQUEST_WOWZA: {
            // If request is cancelled, the result arrays are empty.
            // Check the result of each permission granted

            mPermissionCount++;

            for (int grantResult : grantResults) {
                if (grantResult == PackageManager.PERMISSION_DENIED) {
                    mPermissionsGranted = false;
                }
            }

            if (mPermissionsGranted && mPermissionCount == 3) {
                initializeWowzaCamera();
            }


            if (!mPermissionsGranted && mPermissionCount == 3) {
                mPermissionCount = 0;
                finish();
            }

            return;
        }
    }
}


@Override
protected void onPause() {
    if(mWowza!=null)
    mWowza.onPause();
    super.onPause();
}


@Override
protected void onDestroy() {
    mWowza.stop();
    super.onDestroy();
}
}

请帮助

1 个答案:

答案 0 :(得分:0)

我认为您在检查是否已授予权限时不会调用initializeWowzaCamera()。你是说吗?

if (mPermissionsGranted && grantResults.length == 3) {
    initializeWowzaCamera();
}