Skip to content

Instantly share code, notes, and snippets.

@ThomasGorisse
Created August 15, 2022 18:08
Show Gist options
  • Select an option

  • Save ThomasGorisse/8a092a1b5fd26e79ea22de11a964546c to your computer and use it in GitHub Desktop.

Select an option

Save ThomasGorisse/8a092a1b5fd26e79ea22de11a964546c to your computer and use it in GitHub Desktop.
    fun startVideoRecord(fragment: MainFragment) {
        verifyPermission(activity = fragment.activity,
            permissions = listOf(
                Manifest.permission.RECORD_AUDIO
            ),
            fallBack = {
                fragment.isRecording = false
            },
            success = {
                fragment.isRecording = true
                val context = fragment.requireContext()
                val fileName = "${context.string(R.string.app_name)}-${
                    SimpleDateFormat(
                        "yyyyMMdd_HHmmss",
                        Locale.US
                    ).format(Date())
                }.mp4"
                val values = ContentValues().apply {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        put(
                            MediaStore.Video.Media.RELATIVE_PATH,
                            "${Environment.DIRECTORY_MOVIES}/ARCamera"
                        )
                    }
                    put(MediaStore.Video.Media.DISPLAY_NAME, fileName)
                    put(MediaStore.Video.Media.MIME_TYPE, "video/mp4")
                    put(MediaStore.Video.Media.IS_PENDING, 1)
                }
                recordUri = context.contentResolver.insert(
                    MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                    values
                )!!
                recordFileDescriptor =
                    context.contentResolver.openFileDescriptor(recordUri!!, "w")!!
                val supportedVideoQuality =
                    (fragment.videoQuality downTo 0).firstOrNull { CamcorderProfile.hasProfile(it) }
                if (supportedVideoQuality != null) {
                    val profile = CamcorderProfile.get(supportedVideoQuality)
                    var (width, height) = 0 to 0
                    val isLandscape =
                        context.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
                    if (isLandscape) {
                        width = min(
                            max(profile.videoFrameWidth, profile.videoFrameHeight),
                            max(fragment.binding.sceneView.width, fragment.binding.sceneView.height)
                        )
                        height = min(
                            min(profile.videoFrameWidth, profile.videoFrameHeight),
                            min(fragment.binding.sceneView.width, fragment.binding.sceneView.height)
                        )
                    } else {
                        width = min(
                            min(profile.videoFrameWidth, profile.videoFrameHeight),
                            min(fragment.binding.sceneView.width, fragment.binding.sceneView.height)
                        )
                        height = min(
                            max(profile.videoFrameWidth, profile.videoFrameHeight),
                            max(fragment.binding.sceneView.width, fragment.binding.sceneView.height)
                        )
                    }
                    mediaRecorder = MediaRecorder().apply {
                        val displayManager: DisplayManager =
                            context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
                        val rotation = when (displayManager.displays[0].rotation) {
                            Surface.ROTATION_90 -> 270
                            Surface.ROTATION_180 -> 180
                            Surface.ROTATION_270 -> 90
                            else -> 0
                        }
                        setOrientationHint(rotation)
                        setAudioSource(MediaRecorder.AudioSource.MIC)
                        setVideoSource(MediaRecorder.VideoSource.SURFACE)
                        setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
                        setOutputFile(recordFileDescriptor!!.fileDescriptor)
                        setVideoEncodingBitRate(profile.videoBitRate)
                        setVideoFrameRate(profile.videoFrameRate)
                        setVideoSize(width, height)
                        setVideoEncoder(profile.videoCodec)
                        setAudioChannels(2)
                        setAudioSamplingRate(44100)
                        setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
                        setAudioEncodingBitRate(128000)
                    }.also {
                        it.prepare()
                        it.start()
                    }
                    fragment.binding.sceneView.startMirroringToSurface(
                        mediaRecorder!!.surface,
                        0, 0,
                        width,
                        height
                    )
                }
            })
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment