Skip to content

Instantly share code, notes, and snippets.

@nieldeokar
Last active December 9, 2019 05:41
Show Gist options
  • Save nieldeokar/43d2f67bc38ce60fdc3c88325d480401 to your computer and use it in GitHub Desktop.
Save nieldeokar/43d2f67bc38ce60fdc3c88325d480401 to your computer and use it in GitHub Desktop.
Android has default filesize limit of 4GB while recording video. To overcome it use this build method.
/*
* Created by Nilesh Deokar<nieldeokar@gmail.com> on 18/4/19 2:43 PM
*/
package me.umoove.busCounterApp.models;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import androidx.annotation.RequiresApi;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;
import java.util.Locale;
class ContinueVideoRecorder {
private static String TAG = "ContinueVideoRecorder";
private static String VIDEO_ALBUM_NAME = "MyDirectory";
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void build() throws IOException {
MediaRecorder mRecorder = new MediaRecorder();
mRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
String recordFilePath = getVideoFilePath(getPublicAlbumStorageDir(VIDEO_ALBUM_NAME));
mRecorder.setOutputFile(recordFilePath);
mRecorder.setVideoEncodingBitRate(10_00_000);
mRecorder.setVideoFrameRate(18);
mRecorder.setVideoSize(720,1280);
mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
// setMaxFileSize
mRecorder.setMaxFileSize(100000000); // 10000000 = 10 mb | 100000000 = 100 mb
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setOnInfoListener((mr, what, extra) -> {
Log.i(TAG,String.format(Locale.US,"onInfoListen what : %d | extra %d", what, extra));
// upon receiving MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING callback specify next file on which video recording would be continued
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING) {
try {
mRecorder.setNextOutputFile((new RandomAccessFile(getVideoFilePath(getPublicAlbumStorageDir(VIDEO_ALBUM_NAME)),"rw").getFD()));
} catch (IOException e) {
e.printStackTrace();
}
}
});
mRecorder.setOnErrorListener((mr, what, extra) -> {
Log.i(TAG,String.format(Locale.US,"onErrorListen what : %d | extra %d", what, extra));
});
mRecorder.prepare();
mRecorder.start();
}
/**
* Get directory path to store media in gallery
*
* @param albumName
* @return absolute directory path
*/
public static String getPublicAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File dir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), albumName);
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.e("Main -->", "Directory not created");
return "";
}
}
return dir == null ? "" : dir.getAbsolutePath();
}
/**
* @param basePath
* @return
*/
public static String getVideoFilePath(String basePath) {
return (basePath == null ? "" : (basePath + "/" + "QT_"))
+ getDate() + ".mp4";
}
/**
* Get formatted date
*
* @return current formatted date
*/
public static String getDate() {
return android.text.format.DateFormat.format("yyyy-MM-dd hh:mm:ss a",new Date(System.currentTimeMillis())).toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment