I'm currently developing a video recorder with androidx because I need video files with their exact creation time as millisecond timestamp as title for another project. The problem is that I just finished the app, but the creation time is not exact because I name the file when the onVideoSaved() callback is triggered, which happens about 1-2 seconds before the video recording actually starts. Can anyone tell me how to rename the file when the first video sample is written by the VideoCapture?
It's quite possible that I'm just stupid, because I started developing apps with android studio only for this "companion app" to my actual project. Sorry if this is the case^^.
just use simple date format to set the format you want and format it with passing the current time in millis
val videoCapture = VideoCapture.Builder().build()
val outputDirectory = getOutputDirectory()
fun getOutputDirectory(): File {
val mediaDir = externalMediaDirs.firstOrNull()?.let {
File(it, resources.getString(R.string.app_name)).apply { mkdirs() } }
return if (mediaDir != null && mediaDir.exists())
mediaDir else filesDir
}
@SuppressLint("RestrictedApi")
private fun startRecording() {
val videoFile = File(
outputDirectory,
SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS", Locale.US
).format(System.currentTimeMillis()) + ".mp4")
val outputOptions = VideoCapture.OutputFileOptions.Builder(videoFile).build()
videoCapture?.startRecording(outputOptions, ContextCompat.getMainExecutor(this), object: VideoCapture.OnVideoSavedCallback {
override fun onError(videoCaptureError: Int, message: String, cause: Throwable?) {
Log.e(TAG, "Video capture failed: $message")
}
override fun onVideoSaved(outputFileResults: VideoCapture.OutputFileResults) {
val savedUri = Uri.fromFile(videoFile)
val msg = "Video capture succeeded: $savedUri"
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
Log.d(TAG, msg)
}
})
}
@SuppressLint("RestrictedApi")
private fun stopRecording() {
videoCapture?.stopRecording()
}