Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我已经在Android应用程序中通过webrtc完成了摄像头流共享,但现在我需要与远程对等方共享屏幕_Android_Webrtc_Screen Capture - Fatal编程技术网

我已经在Android应用程序中通过webrtc完成了摄像头流共享,但现在我需要与远程对等方共享屏幕

我已经在Android应用程序中通过webrtc完成了摄像头流共享,但现在我需要与远程对等方共享屏幕,android,webrtc,screen-capture,Android,Webrtc,Screen Capture,我已经能够使用WEBRTC开发摄像头流媒体应用程序。两个对等点是连接的。一个是android应用程序,另一个是浏览器。我是在这本指南的帮助下完成的。https://vivekc.xyz/getting-started-with-webrtc-for-android-daab1e268ff4(如果有人想要良好的web rtc文档,请共享此文档)。现在我想共享屏幕而不是相机流 我试着用媒体投影API来做这件事。但使用此API后,整个屏幕变黑,sdp也不正确。 有人能帮我吗。我试着用手来做这件事 @O

我已经能够使用WEBRTC开发摄像头流媒体应用程序。两个对等点是连接的。一个是android应用程序,另一个是浏览器。我是在这本指南的帮助下完成的。https://vivekc.xyz/getting-started-with-webrtc-for-android-daab1e268ff4(如果有人想要良好的web rtc文档,请共享此文档)。现在我想共享屏幕而不是相机流

我试着用媒体投影API来做这件事。但使用此API后,整个屏幕变黑,sdp也不正确。 有人能帮我吗。我试着用手来做这件事

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    setContentView(R.layout.webrtc_layout);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
    sDeviceWidth = metrics.widthPixels;
    sDeviceHeight = metrics.heightPixels;

    try{
        context = this;

        SignallingClient.getInstance().setChannelId("GINGER_1594035215_551_6336_THINK_1");

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,
                    Manifest.permission.RECORD_AUDIO}, ALL_PERMISSIONS_CODE);
        }
        else if  (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            startScreenCapture();
        }
        else {
            // all permissions already granted
            start();
        }
    } catch (Exception e){
        e.printStackTrace();
    }



}

@TargetApi(21)
private void startScreenCapture() {
    MediaProjectionManager mediaProjectionManager =
            (MediaProjectionManager) getApplication().getSystemService(
                    Context.MEDIA_PROJECTION_SERVICE);
    startActivityForResult(
            mediaProjectionManager.createScreenCaptureIntent(), CAPTURE_PERMISSION_REQUEST_CODE);
}

@TargetApi(21)
private VideoCapturer createScreenCapturer() {
    if (mMediaProjectionPermissionResultCode != Activity.RESULT_OK) {
        showToast("User didn't give permission to capture the screen.");
        return null;
    }
    return new ScreenCapturerAndroid(
            mMediaProjectionPermissionResultData, new MediaProjection.Callback() {
        @Override
        public void onStop() {
            showToast("User revoked permission to capture the screen.");
        }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode != CAPTURE_PERMISSION_REQUEST_CODE)
        return;
    mMediaProjectionPermissionResultCode = resultCode;
    mMediaProjectionPermissionResultData = data;
    start();
}
然后,我将screencaptureandroid添加到视频捕获器中,其中添加了早期的摄像头流。但这毫无帮助

    //Now create a VideoCapturer instance.
        VideoCapturer videoCapturerAndroid;
        videoCapturerAndroid=createScreenCapturer();
        //videoCapturerAndroid = createCameraCapturer(new Camera1Enumerator(false));

有人能帮我吗?

我已经能够自己回答这个问题了。我正在分享代码。希望这将有助于其他任何人在同一个项目的工作

  Add this code in Oncreate
  
   requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    setContentView(R.layout.webrtc_layout);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
    sDeviceWidth = metrics.widthPixels;
    sDeviceHeight = metrics.heightPixels;
然后使用此创建屏幕捕获器,并在权限检查后在开头调用它

 @TargetApi(21)
private void startScreenCapture() {
    MediaProjectionManager mediaProjectionManager =
            (MediaProjectionManager) getApplication().getSystemService(
                    Context.MEDIA_PROJECTION_SERVICE);
    startActivityForResult(
            mediaProjectionManager.createScreenCaptureIntent(), CAPTURE_PERMISSION_REQUEST_CODE);
}

@TargetApi(21)
private VideoCapturer createScreenCapturer() {
    if (mMediaProjectionPermissionResultCode != Activity.RESULT_OK) {
        showToast("User didn't give permission to capture the screen.");
        return null;
    }
    return new ScreenCapturerAndroid(
            mMediaProjectionPermissionResultData, new MediaProjection.Callback() {
        @Override
        public void onStop() {
            showToast("User revoked permission to capture the screen.");
        }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode != CAPTURE_PERMISSION_REQUEST_CODE)
        return;
    mMediaProjectionPermissionResultCode = resultCode;
    mMediaProjectionPermissionResultData = data;
    start();
}
将screencepturer返回到视频捕获器

  videoCapturerAndroid=createScreenCapturer();
        //videoCapturerAndroid = createCameraCapturer(new Camera1Enumerator(false));

        //Create MediaConstraints - Will be useful for specifying video and audio constraints.
        audioConstraints = new MediaConstraints();
        videoConstraints = new MediaConstraints();



        //Create a VideoSource instance
        if (videoCapturerAndroid != null) {
            surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", rootEglBase.getEglBaseContext());
            videoSource = peerConnectionFactory.createVideoSource(videoCapturerAndroid.isScreencast());
            videoCapturerAndroid.initialize(surfaceTextureHelper, this, videoSource.getCapturerObserver());
        }

你现在可以走了。它可以完美地运行到Android 9。如果你想在安卓10中使用它,不要紧张。只需将此功能添加到前台服务中,并在屏幕镜像打开时始终显示通知,即可完美运行。

您好,您是否能够实现此功能?是的。我将在一两天内发布答案。如果有任何更新,请共享代码。请检查。我已发布了答案。如果可能,您可以共享摄像头提要捕获的代码吗?您可以共享如何在摄像头和屏幕共享之间切换的代码吗?那真的很有帮助。谢谢