我正在使用 https://github.com/Karumi/Dexter 库向我的相机授予权限。在使用普通相机进行测试时,一切工作都很好,但是当我使用 https://github.com/pedroSG94/rtmp-rtsp-stream-client-java 库进行rtmp流传输时,出现错误并且无法访问相机。有人知道如何解决此问题吗?
在“主要活动”中,我定义了“ dexter”权限和访问摄像头活动的按钮
MainActivity
public class MainActivity extends AppCompatActivity {
private Button btnCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCamera = findViewById(R.id.btn_camera);
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestCameraPermission();
}
});
}
private void requestCameraPermission(){
Dexter.withActivity(this)
.withPermission(Manifest.permission.CAMERA)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
openCamera();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
showSettingsDialog();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
token.continuePermissionRequest();
}
}).check();
}
private void showSettingsDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need Permission");
builder.setMessage("Need permission to use this feature");
builder.setPositiveButton("GOTO SETTINGS", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
openSettings();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
private void openSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
private void openCamera(){
Intent intent = new Intent(this, CameraActivity.class);
startActivityForResult(intent,100);
}
}
CameraActivity
public class CameraActivity extends AppCompatActivity
implements ConnectCheckerRtmp, SurfaceHolder.Callback {
private RtmpCamera1 rtmpCamera1;
private Button streamBtn;
private EditText etUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
SurfaceView surfaceView = findViewById(R.id.surfaceView);
streamBtn = findViewById(R.id.b_start_stop);
etUrl = findViewById(R.id.et_rtp_url);
rtmpCamera1 = new RtmpCamera1(surfaceView, this);
surfaceView.getHolder().addCallback(this);
//rtmpCamera1.setPreviewOrientation(90);
streamBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!rtmpCamera1.isStreaming()) {
if (rtmpCamera1.prepareVideo()) {
streamBtn.setText(R.string.stop_button);
rtmpCamera1.startStream(etUrl.getText().toString());
} else {
Toast.makeText(CameraActivity.this, "Error preparing stream, This device cant do it", Toast.LENGTH_SHORT).show();
}
} else {
streamBtn.setText(R.string.start_button);
rtmpCamera1.stopStream();
}
}
});
}
@Override
public void onConnectionSuccessRtmp() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(CameraActivity.this, "Connection success", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onConnectionFailedRtmp(final String reason) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(CameraActivity.this, "Connection failed. " + reason, Toast.LENGTH_SHORT)
.show();
rtmpCamera1.stopStream();
streamBtn.setText(R.string.start_button);
}
});
}
@Override
public void onDisconnectRtmp() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(CameraActivity.this, "Disconnected", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onAuthErrorRtmp() {
}
@Override
public void onAuthSuccessRtmp() {
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
rtmpCamera1.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
if (rtmpCamera1.isStreaming()) {
rtmpCamera1.stopStream();
streamBtn.setText(getResources().getString(R.string.start_button));
}
rtmpCamera1.stopPreview();
}
}
堆栈跟踪
09-18 20:29:33.680 9638-9638/? E/Zygote: isWhitelistProcess - Process is Whitelisted
09-18 20:29:33.681 9638-9638/? E/libpersona: scanKnoxPersonas
Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory
09-18 20:30:04.911 9638-10442/com.stream.ivan.streamapp008 E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
09-18 20:30:04.912 9638-10442/com.stream.ivan.streamapp008 E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
09-18 20:30:04.912 9638-10443/com.stream.ivan.streamapp008 E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
09-18 20:30:04.912 9638-10443/com.stream.ivan.streamapp008 E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
09-18 20:30:08.730 9638-9638/com.stream.ivan.streamapp008 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.stream.ivan.streamapp008, PID: 9638
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stream.ivan.streamapp008/com.stream.ivan.streamapp008.CameraActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.SurfaceView.getContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.SurfaceView.getContext()' on a null object reference
at com.pedro.rtplibrary.base.Camera1Base.<init>(Camera1Base.java:72)
at com.pedro.rtplibrary.rtmp.RtmpCamera1.<init>(RtmpCamera1.java:28)
at com.stream.ivan.streamapp008.CameraActivity.onCreate(CameraActivity.java:37)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
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)