public void stopDisplayService(){ if (mediaProjection != null) { mediaProjection.stop(); mediaProjection = null; } if (glInterface != null) { glInterface.removeMediaCodecSurface(); glInterface.stop(); videoEncoder.stop(); } } public void stopRtspFromFile() { if (streaming) { streaming = false; } if (!recordController.isRecording()) { if (glInterface != null) { glInterface.removeMediaCodecSurface(); glInterface.stop(); } if (videoEnabled) videoDecoder.stop(); if (videoEnabled) videoEncoder.stop(); recordController.resetFormats(); videoEnabled = false; } } public void startDisplayService(){ videoEncoder.start(); if (glInterface != null) { glInterface.setFps(videoEncoder.getFps()); glInterface.start(); glInterface.addMediaCodecSurface(videoEncoder.getInputSurface()); } Surface surface = (glInterface != null) ? glInterface.getSurface() : videoEncoder.getInputSurface(); if (mediaProjection == null) { mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data); } if (glInterface != null && videoEncoder.getRotation() == 90 || videoEncoder.getRotation() == 270) { mediaProjection.createVirtualDisplay("Stream Display", videoEncoder.getHeight(), videoEncoder.getWidth(), dpi, 0, surface, null, null); } else { mediaProjection.createVirtualDisplay("Stream Display", videoEncoder.getWidth(), videoEncoder.getHeight(), dpi, 0, surface, null, null); } } //switch from displayService to rtspFromFile displayBase!!.stopDisplayService() rtspFromFile!!.setVideoEncoder(DisplayBase.getVideoEncoder()) rtspFromFile!!.setOnlyVideo(true) rtspFromFile!!.prepareVideo(path) rtspFromFile!!.setLoopMode(true) rtspFromFile!!.startStream(rtsp_server) //Switch from rtspFromFile to displayService rtspFromFile!!.stopRtspFromFile() displayBase!!.prepareVideo() displayBase!!.startDisplayService()