3

I'm trying to record the video and store it in DCIM directory

I have tried to use MediaStore but the problem is the media store returns Uri and MediaRecorder take File, FileDescreptor or file path and I don't have any of these options so I tried to convert Uri to FileDescreptor like this resolver.openFileDescriptor(uri!!, "rw")?.fileDescriptor but exception happens java.lang.RuntimeException: Error retrieving camcorder profile params

private fun setUpTimeLapseMediaRecorder(): MediaRecorder {
    val resolver = context.contentResolver
    val contentValues = ContentValues()
    val videoName = "time_lapse_video_" + System.currentTimeMillis() + ".mp4"
    val directoryName = "${Environment.DIRECTORY_DCIM}"
    val filePath = "${directoryName}Videos${File.separator}$videoName"

    contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, filePath)
    val uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues)

    val timeLapseMediaRecorder = MediaRecorder()
    timeLapseMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE)
    timeLapseMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_720P))
    timeLapseMediaRecorder.setOutputFile(/* How i can get file path from media store?*/)
    timeLapseMediaRecorder.setVideoFrameRate(60)
    timeLapseMediaRecorder.setCaptureRate(1.8)

    timeLapseMediaRecorder.prepare()

    return timeLapseMediaRecorder
}

How I can use MediaStore with MediaRecorder

Mohamed Hamdan
  • 147
  • 2
  • 16
  • refer to this question it might help you: https://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore – Ahmed Awad Oct 06 '19 at 11:12

1 Answers1

0

i think i got solution in your manifest add the following

<application android:requestLegacyExternalStorage="true"

i think this will resolve your error

//this is my previous answer I am having same problem but it is working on android 9 or below I think the problem is with audioformat It can be MPEG_2_TS or OGG or something else